实现LRU Cache(java版)

2 篇文章 0 订阅

LRU算法,也叫最近最少使用算法,我们可以使用该算法来实现cache机制,简单地说就是缓存一定量的数据,当超过设定阀值的时候就把一些过期的数据删除掉,本篇文章我们来看看如何实现LRU Cache,下面我们使用两种方式来实现。

基于LinkedHashMap

在java的集合类中有LinkedHashMap类,当参数accessOrder为true的时候,就会按照访问顺序排序,最后访问的放在最前面,最早访问的放在后面,这样我们就可以实现LRU算法了。这种方式比较简单,代码如下:

public class LRU {
	private final int CAPACITY=0;
	private LinkedHashMap<Integer,Integer> cache;
	
	public LRU(int capacity){
		cache=new LinkedHashMap<Integer,Integer>(capacity,0.75f,true){
			@Override
			protected boolean removeEldestEntry(java.util.Map.Entry<Integer, Integer> eldest) {
				return size()>CAPACITY;
			}
		};
	}
	
	public int get(int key) {
        return cache.getOrDefault(key, -1);
    }
    
    public void put(int key, int value) {
        cache.put(key, value);
    }
	
}
链表+HashMap

第二种方式是使用链表+HashMap的方式来实现,需要我们自己实现一系列方法,代码如下:

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
/**
 * LRUCache链表+hashmap的实现方式
 * @author shinelon
 *
 */
public class LRUCache2<K,V>{
	
	public final int CACHE_SIZE;
	public Entry frist;
	public Entry last;
	public HashMap<K,Entry<K,V>> hashMap;
	
	public LRUCache2(int cacheSize){
		this.CACHE_SIZE=cacheSize;
		hashMap=new HashMap<K,Entry<K,V>>();
	}
	
	public void put(K key,V value){
		Entry entry=getEntry(key);
		if(entry==null){
			if(hashMap.size()>=CACHE_SIZE){
				hashMap.remove(last.key);
				removeLast();
			}
			entry=new Entry();
			entry.key=key;
		}
		entry.value=value;
		moveToFrist(entry);
		hashMap.put(key, entry);
	}
	
	public V get(K key){
		Entry<K,V> entry=getEntry(key);
		if(entry==null)
			return null;
		moveToFrist(entry);
		return entry.value;
	}
	

	private void moveToFrist(Entry entry) {
		if(entry==frist)
			return;
		if(entry.pre!=null)
			entry.pre.next=entry.next;
		if(entry.next!=null)
			entry.next.pre=entry.pre;
		if(last==entry)
			last=last.pre;
		
		if(frist==null||last==null){
			frist=last=entry;
			return;
		}
		
		entry.next=frist;
		frist.pre=entry;
		frist=entry;
		frist.pre=null;
	}

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

	public Entry getEntry(K key){
		return hashMap.get(key);
	}
	
	public void print(HashMap<K,Entry<K,V>> map){
		Collection<Entry<K,V>> entry=map.values();
		Iterator it=entry.iterator();
		while(it.hasNext()){
			Entry<K,V> e=(LRUCache2<K, V>.Entry<K, V>) it.next();
			System.out.println(e.key+"  :  "+e.value);
		}
	}

	class Entry<K,V>{
		Entry pre;
		Entry next;
		K key;
		V value;
	}
	
	
	public static void main(String[] args) {
		LRUCache2<String, String> cache=new LRUCache2<>(3);
		cache.put("one", "1");
		cache.put("tow", "2");
		cache.put("three", "3");
		System.out.println("first entry: "+cache.frist.key);
		cache.print(cache.hashMap);
		cache.put("four", "4");
		System.out.println("============================");
		System.out.println("first entry: "+cache.frist.key);
		cache.print(cache.hashMap);
		cache.put("five", "5");
		System.out.println("============================");
		System.out.println("first entry: "+cache.frist.key);
		cache.print(cache.hashMap);
	}
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LRU Cache是一种常见的缓存淘汰策略,LRU代表最近最少使用。在Java中,可以使用不同的方式来实现LRU Cache。 引用\[1\]中的代码展示了一种自定义的LRU Cache实现,使用了一个自定义的LRUCache类,并在main方法中进行了测试。在这个实现中,LRUCache类继承了LinkedHashMap,并重写了removeEldestEntry方法来实现缓存的淘汰策略。 引用\[2\]中的代码展示了另一种自定义的LRU Cache实现,同样使用了一个自定义的LRUCache类,并在main方法中进行了测试。这个实现中,LRUCache类同样继承了LinkedHashMap,并重写了removeEldestEntry方法来实现缓存的淘汰策略。 引用\[3\]中的代码展示了使用ArrayList实现LRU Cache的方式。在这个实现中,LRUCache类使用了一个ArrayList来存储缓存数据,并通过get和put方法来操作缓存。 总结来说,LRU CacheJava实现可以使用自定义的类继承LinkedHashMap并重写removeEldestEntry方法,或者使用ArrayList来存储缓存数据。具体的实现方式可以根据需求和偏好选择。 #### 引用[.reference_title] - *1* *2* [Java——LRUCache](https://blog.csdn.net/m0_60867520/article/details/128361042)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [LRUCacheJava实现](https://blog.csdn.net/qq_39383118/article/details/103535985)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值