java缓存逻辑结构图_GitHub - chongtianfeiyu/SimpleCache: 用Java实现的缓存框架,使用HashMap进行存储,你可以实现Cache和CacheFactoryS...

SimpleCache

A Cache framework for java and android application, objects are store in key-value pair in memory,value can be any java object like String, List, Map etc. you can define your store logic by implement Cache interface and CacheFactoryStrategy,when cache is full ,LRU is used .

用Java实现的轻量级缓存框架,你可以实现Cache和CacheFactoryStrategy接口来自定义你的缓存逻辑。使用HashMap进行存储,键为String,值可以为任意对象类型,如String,List,Map等。当缓存的大小大于最大缓存容量的0.97倍时,会对缓存中的数据进行清理。缓存使用了两个LinkedList保存插入的数据,一个按照对象的创建顺序来保存,对象被创建之后放在链表最前面,当空间不足时从链表尾部开始清理那些过期的数据。另一个LinkedList按照缓存的访问顺序来排序,每当对象被访问就把对象插入到链表的最前面,同时框架使用了Public的LinkedListNode,Cache中保存对链表节点的引用,可以快速删除数据。

how to use

Cache objectCache = CacheFactory.createCache("objects");

//set max cache size

objectCache.setMaxCacheSize(1024 * 1024);

//set max lifetime for cache objects

objectCache.setMaxLifetime(60 * 60 * 1000);

objectCache.put("object1", new Object());

objectCache.put("object2", new Object());

objectCache.put("object3", new Object());

Object o1 = objectCache.get("object1");

if(o1 == null){

//get data from file or datebase

}

//get cache hits

long cacheHits = objectCache.getCacheHits();

//get cache miss

long cacheMiss = objectCache.getCacheMisses();

//remove cache

Object o2 = objectCache.remove("object1");

if(o2 != null) {o2 = null;}

//you can also use Collection as cache value

Cache> collectionCache = CacheFactory.createCache("collections");

collectionCache.put("collection1", Arrays.asList("hello", "simple cache"));

//you can get lock for this collection

Lock lock = CacheFactory.getLock("collection1", collectionCache);

lock.lock();

//do something

lock.unlock();

//delete cache

CacheFactory.destroyCache("object1");

CacheFactory.destroyCache("collection1");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值