首页 > 系统服务 > 详细

rankable cache class

时间:2017-12-02 10:28:11      阅读:426      评论:0      收藏:0      [点我收藏+]

 

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

  

rankable cache class

原文:http://www.cnblogs.com/apanda009/p/7945036.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!