码迷,mamicode.com
首页 > 系统相关 > 详细

rankable cache class

时间:2017-12-02 11:24:22      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:har   rom   out   ranking   res   remove   sizeof   contain   can   

 

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

标签:har   rom   out   ranking   res   remove   sizeof   contain   can   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!