您的位置:首页 > 编程语言

LRU 缓存编程实现

2018-02-11 10:34 183 查看
感觉这个话题,包括代码已经烂大街了。不过写这个还是需要对HashMap系和链表的基础很了解的,最近重新练了练手,附上代码和注释吧。

整体可以理解为是链表的结构-根据访问顺序调整节点节点的位置,但存储的时候是K_V的形式存储-添加查询删除的时候首先链表操作

然后在HashMap中添加查询删除-总体结构和LindedHashMap类似,异曲同工,但具体的区别还待继续学习研究, 不过一般都用的是LinkedHashMap。了解的大神可以留言,指教

Cache的链表+HashMap实现:

import java.util.HashMap;
/**
* 整体可以理解为是链表的结构-根据访问顺序调整节点节点的位置,但存储的时候是K_V的形式存储-添加查询删除的时候首先链表操作
* 然后在HashMap中添加查询删除-总体结构和LindedHashMap类似,异曲同工,但具体的区别还待继续学习研究,
* 不过一般都用的是LinkedHashMap
*/
public class Lru<K,V> {                                     //节点是hashMap的链表
class Entry<K,V>{                                       //链表Entry节点结构
public K Key;
public V Value;
public Entry  pre;
public Entry  next;
}
private Entry first;
private Entry last;
private HashMap<K,Entry<K,V>> hashMap;
private final int CacheSize;
public Lru(int cacheSize){
this.CacheSize=cacheSize;
hashMap=new HashMap<K, Entry<K, V>>();
}
public void set(K key,V value){
Entry entry=getEntry(key);
if(entry==null){
if(hashMap.size()>CacheSize){
hashMap.remove(last.Key);
removeLast(last);
}
entry = new Entry();
entry.Key = key;
}
entry.Value = value;
moveToFirst(entry);
hashMap.put(key, entry);
}

public V get(K key) {
Entry<K, V> entry = getEntry(key);
if (entry == null) return null;
moveToFirst(entry);
return entry.Value;
}

private void moveToFirst(Entry entry) {
if (entry == first) return;
if (entry.pre != null) entry.pre.next = entry.next;
if (entry.next != null) entry.next.pre = entry.pre;
if (entry == last) last = last.pre;

if (first == null || last == null) {
first = last = entry;
return;
}
entry.next=first;
first.pre=entry;
first=entry;
entry.pre=null;
}

private void removeLast(Entry last) {
if(last!=null) {
last = last.pre;
if(last==null){
first=null;
}else{
last.next=null;
}
}
}

private Entry<K,V> getEntry(K key) {
return hashMap.get(key);
}
private void remove(K key){
Entry entry=getEntry(key);
if(entry!=null){
if(entry.pre!=null){
entry.pre.next=entry.next;
}
if(entry.next!=null){
entry.next.pre=entry.pre;
}
if(entry==first){
first=entry.next;
}
if(entry==last){
last=entry.pre;
}
}
hashMap.remove(key);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Entry entry = first;
while (entry != null) {
sb.append(String.format("%s:%s ", entry.Key, entry.Value));
entry = entry.next;
}
return sb.toString();
}
}


关于使用LinkedHashMap实现的晚上补上…..

补:

import java.util.LinkedHashMap;
import java.util.Map;
/**
*
* @author MaiBenBen
*/
public class Lru<K,V> extends LinkedHashMap<K,V>{

/**
* @param args the command line arguments
*/
private final int Max_Size;
public Lru(int cacheSize){
super((int)Math.ceil(cacheSize/0.75)+1,0.75f,true);
this.Max_Size=cacheSize;
}
@Override
protected boolean removeEldestEntry(Map.Entry eldest){
return this.size()>Max_Size;
}
@Override
public String toString(){
StringBuilder sb=new StringBuilder();
for(Map.Entry<K,V> e:entrySet()){
sb.append(String.format("%s:%s",e.getKey(),e.getValue()));
}
return sb.toString();
}
public static void main(String[] args) {
// TODO code application logic here
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LRU 链表 java 缓存机制