http://stackoverflow.com/questions/224868/easy-simple-to-use-lru-cache-in-java
Easy, simple to use LRU cache in java
I know it's simple to implement, but I want to reuse something that already exist.
Problem I want to solve is that I load configuration (from XML so I want to cache them) for different pages, roles, ... so the combination of inputs can grow quite much (but in 99% will not). To handle this 1%, I want to have some max number of items in cache...
Till know I have found org.apache.commons.collections.map.LRUMap in apache commons and it looks fine but want to check also something else. Any recommendations?
You can use a LinkedHashMap (Java 1.4+) :
The code from exampledepot.com:
// Create cache
final int MAX_ENTRIES = 100;
Map cache = new LinkedHashMap(MAX_ENTRIES+1, .75F, true) {
//true 以访问顺序排列;false 以插入顺序排列
// This method is called just after a new entry has been added
public boolean removeEldestEntry(Map.Entry eldest) {
return size() > MAX_ENTRIES;//删除最老的键值对的条件
}
};
// Add to cache
Object key = "key";
cache.put(key, object);
// Get object
Object o = cache.get(key);
if (o == null && !cache.containsKey(key)) {
// Object not in cache. If null is not a possible value in the cache,
// the call to cache.contains(key) is not needed
}
// If the cache is to be used by multiple threads,
// the cache must be wrapped with code to synchronize the methods
cache = (Map)Collections.synchronizedMap(cache);
分享到:
相关推荐
深入Java集合学习系列(四): LinkedHashMap的实现原理
·课程中,Eclipse和IDEA这两种企业一线开发环境都使用到了 3.技术讲解更深入、更全面: ·课程共30天,715个知识视频小节,涉及主流Java使用的方方面面,全而不冗余 ·全程内容涵盖数据结构、设计模式、JVM内存...
·课程中,Eclipse和IDEA这两种企业一线开发环境都使用到了 3.技术讲解更深入、更全面: ·课程共30天,715个知识视频小节,涉及主流Java使用的方方面面,全而不冗余 ·全程内容涵盖数据结构、设计模式、JVM内存...
java软件技术文档
RiteLinked-类似HashMap的容器,以用户可控制的顺序保存其键值对 提供了LinkedHashMap和... let mut lru_cache = LinkedHashMap :: new (); let key = "key" . to_owned (); let _cached_val = lru_cache .
LinkedHashMap源代码,Java中Map的一种实现子类。
将LinkedHashMap转换为json,反之亦然 如何使用 LinkedHashMap requestData = new LinkedHashMap(); LinkedHashMap auth =新的LinkedHashMap(); auth.put(“ ServiceName”,“ Login”); auth.put(“ ...
这是关于Java学习的主要针对LinkedHashMap的实现原理
首先还是类似的,我们写一个简单的LinkedHashMap的程序: LinkedHashMap<String> lmap = new LinkedHashMap(); lmap.put(语文, 1); lmap.put(数学, 2); lmap.put(英语, 3); lmap.put(历史, 4); ...
今天说一下LinkedHashMap的主要点,因为有同学不太清楚它和HashMap的区别。今天大概总结一下,也是方便自己进行学习。 写在前面 LinkedHashMap的内部维护了一个双向链表。可以按照元素的插入顺序进行访问,也可以...
java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
Set是使用LinkedHashMap在Go(Golang)中简单的Set数据结构实现。 该库允许您获取一组int64或string而没有重复的项目。 用法 package main import ( "fmt" "github.com/StudioSol/set" ) func main () { ...
这个demo主要讲解了LinkedHashmap的使用,希望可以帮助需要的同学.
HashMap,HashTable,LinkedHashMap,TreeMap的区别
LinkedHashMap是Java中的一种特殊类型的HashMap,它保留了插入顺序,即按照元素插入的先后顺序进行排序
(当然也可以用hashbrown )用法将ritelinked添加到Cargo.toml :ritelinked =" x.y.z"写一些这样的代码:letmut lru_cache= LinkedHashMap::new ();let key="key" .to_owned ();let _cached_val= lru_cache .raw_...
Java提供了LinkedHashMap,可以用该类很方便的实现LRU算法,Java的LRULinkedHashMap是直接继承了LinkedHashMap,进行了极少的改动后可以实现LRU算法。 二、Java的LRU算法 Java的LRU算法的基础是LinkedHashMap...
Java提供了LinkedHashMap,可以用该类很方便的实现LRU算法,Java的LRULinkedHashMap就是直接继承了LinkedHashMap,进行了极少的改动后就可以实现LRU算法。 二、Java的LRU算法 Java的LRU算法的基础是LinkedHashMap,...
基于JavaLinkedHashMap实现的 LRU 算法 algorithm.consistentHashing ConsistentHash: 一致性Hash算法 algorithm.cap algorithm.subset 给一个set打印出所有子集 jdk jdk 知识 jdk.autoboxing 自动装箱拆箱 jdk....