• rankable cache class


    public class RetainBestCache<K, T extends Rankable> {
    
         private Map<K, T> cache;. 鍥磋�鎴戜滑@1point 3 acres
         private Map<Long, Set<K>> rankingOfObject;.鏈�枃鍘熷垱鑷�1point3acres璁哄潧
         private DataSource<K, T> dataSource;
         private int maxSizeOfCache;
    
        /* Constructor with a data source (assumed to be slow) and a cache size */
        public RetainBestCache(DataSource<K,T> ds, int entriesToRetain) {. From 1point 3acres bbs
            // Implementation here
            cache = new HashMap<>();
            rankingOfObject = new TreeMap<>();
            dataSource = ds;
            maxSizeOfCache = entriesToRetain;
        }
    
        /* Gets some data. If possible, retrieves it from cache to be fast. If the data is not cached,
         * retrieves it from the data source. If the cache is full, attempt to cache the returned data,
         * evicting the T with lowest rank among the ones that it has available
         * If there is a tie, the cache may choose any T with lowest rank to evict.
         */
        public T get(K key) {
            // Implementation here
            if(cache.containsKey(key)) {
              return cache.get(key);
            }
            return fetchDataFromDs(key);
        }
    . visit 1point3acres.com for more.
        private T fetchDataFromDs(K key) {
           if(cache.size() >= maxSizeOfCache) {
              evictElement();
            }
            T object = dataSource.get(key);
            cache.put(key, object);
            long rankOfObject = object.getRank();
            if(!rankingOfObject.containsKey(rankOfObject)) {. 鐗涗汉浜戦泦,涓€浜╀笁鍒嗗湴
              rankingOfObject.put(rankOfObject, new HashSet<>());
            }
            rankingOfObject.get(rankOfObject).add(key);
            return object;
        }
    
        private void evictElement() {
          Entry<Long, Set<K>> entry = rankingOfObject.topEntry();
          K key = entry.getValue().getIterator().next();
          entry.getValue().remove(key);
          cache.remove(key)
          if(entry.getValue().size() == 0) {
            rankingOfObject.remove(entry.getKey());
          }.鏈�枃鍘熷垱鑷�1point3acres璁哄潧
        }
    
    
    }
    . visit 1point3acres.com for more.
    // What if rank is defined as number of reads of element in cache?
    // LRU. from: 1point3acres.com/bbs 
    // Let's assume that rank can change dynamicly. It is not immutable, but it is not LRU. We do not know how it is changed
    
    
    /*
    * For reference, here are the Rankable and DataSource interfaces.
    * You do not need to implement them, and should not make assumptions
    * about their implementations.
    */
    
    public interface Rankable {. from: 1point3acres.com/bbs 
        /**.鐣欏�璁哄潧-涓€浜�-涓夊垎鍦�
         * Returns the Rank of this object, using some algorithm and potentially
         * the internal state of the Rankable.
         */
        long getRank();
    }
    
    public interface DataSource<K, T extends Rankable> {
        T get (K key);
    }. W
    

      

  • 相关阅读:
    Node.js REPL(交互式解释器)
    Node.js NPM 使用介绍
    Node.js 创建第一个应用
    Node.js 安装配置
    Node.js 教程
    Go语言-通道类型
    golang 线程与通道
    Go 语言 goto 语句
    Go 语言指向指针的指针
    session效率
  • 原文地址:https://www.cnblogs.com/apanda009/p/7945036.html
Copyright © 2020-2023  润新知