zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    python中zip函数
    python中创建列表、元组、字符串、字典
    python中enumerate函数
    python中字符串的拼接
    python中格式化浮点数及科学计数法
    python中tuple函数
    python中可迭代对象的排序
    python中变量类型转换
    python中可迭代对象反转
    python中list函数
  • 原文地址:https://www.cnblogs.com/apanda009/p/7945036.html
Copyright © 2011-2022 走看看