首页 > 学院 > 开发设计 > 正文

LRUCache的实现原理

2019-11-09 18:44:56
字体:
来源:转载
供稿:网友

简介

LRU(Least Recently Used)最近最少使用,最近有时间和空间最近的歧义,所以我更喜欢叫它近期最少使用算法。它的核心思想是,如果一个数据被访问过,我们有理由相信它在将来被访问的概率就越高。于是当LRU缓存达到设定的最大值时将缓存中近期最少使用的对象移除。LRUCache内部使用LinkedHashMap来存储key-value键值对,并将LinkedHashMap设置为访问顺序来体现LRU算法。

简单使用

// 拿到当前的最大内存long maxMemory = Runtime.getRuntime().maxMemory();int cacheSize = (int) (maxMemory / 8);// 将LruCache的缓存容量设置为cacheSize,并重写sizeOf方法。lruCache = new LruCache<String, Bitmap>(cacheSize) { @Override PRotected int sizeOf(String key, Bitmap value) { return value.getByteCount(); }};// put一条数据lruCache.put(key, bitmap);// get一条数据lruCache.get(key);// remove一条数据lruCache.remove(key);

数据结构

public LruCache(int maxSize) { if (maxSize <= 0) { throw new IllegalArgumentException("maxSize <= 0"); } this.maxSize = maxSize; this.map = new LinkedHashMap<K, V>(0, 0.75f, true); }

可以看到,除了初始化maxSize,还初始化了一个全局的LinkedHashMap,当第三个参数设为true时,map被设置为 访问顺序,也就是每操作一次map的某个Entry,它就会被放到双向链表的队尾。也就是说LRU算法的任务,完全交给了LinkedHashMap。

put方法

public final V put(K key, V value) { if (key == null || value == null) { throw new NullPointerException("key == null || value == null"); } V previous; synchronized (this) { putCount++; size += safeSizeOf(key, value); // 如果key不冲突就返回null // 如果key冲突就用value替换odlValue,并返回oldValue previous = map.put(key, value); if (previous != null) { size -= safeSizeOf(key, previous); } } if (previous != null) { entryRemoved(false, key, previous, value); } trimToSize(maxSize); return previous; }

数据通过key-value的形式被put到了map里,在put之前通过sizeOf方法计算value的size,并累加到全局变量size里,它用来记录当前缓存的数据大小。sizeOf原则上必须重写,用来定义自己的计算规则。然后调用了trimToSize方法,这个方法用以保证缓存大小不会超过maxSize。

trimToSize方法

public void trimToSize(int maxSize) { while (true) { K key; V value; synchronized (this) { if (size < 0 || (map.isEmpty() && size != 0)) { throw new IllegalStateException(getClass().getName() + ".sizeOf() is reporting inconsistent results!"); } if (size <= maxSize || map.isEmpty()) { break; } Map.Entry<K, V> toEvict = map.entrySet().iterator().next(); key = toEvict.getKey(); value = toEvict.getValue(); map.remove(key); size -= safeSizeOf(key, value); evictionCount++; } entryRemoved(true, key, value, null); } }

一进来就是一个死循环,然后就迭代删除位于队首的数据。只有当前缓存容量小于maxSize的时候才break。

综上 LruCache自己主要是实现maxSize的判断,以及通过trimToSize对缓存的裁剪。其他存储、提取数据的方法以及LRU的算法都是借助LinkedHashMap来实现的。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表