LRU(多线程)缓存的实现 |
您所在的位置:网站首页 › java缓存实现 › LRU(多线程)缓存的实现 |
(多线程)缓存的实现 模仿jdk1.7Concurrenthashmap结构的实现,实现⾃⼰的⼀个同步lru缓存,实现如下 package Inter.other; /** * 缓存通⽤实现接⼝接⼝ * Created by lin on 2018/9/19. */ public interface Cache { V get(K key); void set(K key, V value); void clear(); int size(); /** * 该⽅法,专门为 segmentCache 设计, * * @return */ void removeLast(); }
package Inter.other; import java.util.HashMap; import java.util.Map; /** * 缓存算法的具体实现 * Created by lin on 2018/9/16. * 时间复杂度为 O(1) 的⼀个缓存 */ public class LRUCache implements Cache { // private KeyGenerationStrategy keyGenerationStrategy; // 默认容量⼤⼩ private static final int DEFAULT_CAPACITY = 8; /* 缓存容量的⼤⼩ */ private volatile int capacity; /* 缓存已使⽤的容量 */ private volatile int size; /* 为了实现快速寻找,这⾥使⽤ map, 查找时间复杂度为 O(1)*/ private volatile Map map = new HashMap(); /* 为了实现快速替换,这⾥使⽤链表,删除或者加⼊时间复杂度为 O(1)*/ private volatile Node head; private volatile Node tail; /** * 初始化 * * @param capacity |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |