Jodd cache提供了一组cache的实现,其层次如下:

其中,

AbstractCacheMap是一个具有计时和大小的缓存map的默认实现,它的实现类必须:

  创建一个新的缓存map。

  实现自己的删除(prune)策略。

内部使用ReentranReadWriteLock来同步。因为从一个读锁升级到一个写锁是不可能的,因此在get(Object)方法内要注意。

FIFOCach:先进先出缓存。优点是简单高效。缺点是不灵活,没有在内存中保存常用的缓存对象。

/*** Creates a new LRU cache.*/public FIFOCache(int cacheSize, long timeout) {this.cacheSize = cacheSize;this.timeout = timeout;cacheMap = new LinkedHashMap<K,CacheObject<K,V>>(cacheSize + 1, 1.0f, false);}// ---------------------------------------------------------------- prune/*** Prune expired objects and, if cache is still full, the first one.*/@Overrideprotected int pruneCache() {int count = 0;CacheObject<K,V> first = null;Iterator<CacheObject<K,V>> values = cacheMap.values().iterator();while (values.hasNext()) {CacheObject<K,V> co = values.next();if (co.isExpired() == true) {values.remove();count++;}if (first == null) {first = co;}}if (isFull()) {if (first != null) {cacheMap.remove(first.key);count++;}}return count;}

LFUCache:最少访问次数缓存。优点是常用缓存保留在内存中,偶然会使扫描算法失效。缺点是大的获取消耗即这个算法不能快速适应变化的使用模式,特别是集群的临时获取是无效的。

public LFUCache(int maxSize, long timeout) {this.cacheSize = maxSize;this.timeout = timeout;cacheMap = new HashMap<K, CacheObject<K,V>>(maxSize + 1);}// ---------------------------------------------------------------- prune/*** Prunes expired and, if cache is still full, the LFU element(s) from the cache.* On LFU removal, access count is normalized to value which had removed object.* Returns the number of removed objects.*/@Overrideprotected int pruneCache() {int count = 0;CacheObject<K,V> comin = null;// remove expired items and find cached object with minimal access countIterator<CacheObject<K,V>> values = cacheMap.values().iterator();while (values.hasNext()) {CacheObject<K,V> co = values.next();if (co.isExpired() == true) {values.remove();onRemove(co.key, co.cachedObject);count++;continue;}if (comin == null) {comin = co;} else {if (co.accessCount < comin.accessCount) {comin = co;}}}if (isFull() == false) {return count;}// decrease access count to all cached objectsif (comin != null) {long minAccessCount = comin.accessCount;values = cacheMap.values().iterator();while (values.hasNext()) {CacheObject<K, V> co = values.next();co.accessCount -= minAccessCount;if (co.accessCount <= 0) {values.remove();onRemove(co.key, co.cachedObject);count++;                    }}}return count;}

LRUCache:最近未访问缓存。缓存对象的消耗是一个常量。简单高效,比FIFO更适应一个变化的场景。缺点是可能会被不会重新访问的缓存占满空间,特别是在面对获取类型扫描时则完全不起作用。然后它是目前最常用的缓存算法。

/*** Creates a new LRU cache.*/public LRUCache(int cacheSize, long timeout) {this.cacheSize = cacheSize;this.timeout = timeout;cacheMap = new LinkedHashMap<K, CacheObject<K,V>>(cacheSize + 1, 1.0f, true) {@Overrideprotected boolean removeEldestEntry(Map.Entry eldest) {return LRUCache.this.removeEldestEntry(size());}};}/*** Removes the eldest entry if current cache size exceed cache size.*/protected boolean removeEldestEntry(int currentSize) {if (cacheSize == 0) {return false;}return currentSize > cacheSize;}// ---------------------------------------------------------------- prune/*** Prune only expired objects, <code>LinkedHashMap</code> will take care of LRU if needed.*/@Overrideprotected int pruneCache() {if (isPruneExpiredActive() == false) {return 0;}int count = 0;Iterator<CacheObject<K,V>> values = cacheMap.values().iterator();while (values.hasNext()) {CacheObject<K,V> co = values.next();if (co.isExpired() == true) {values.remove();count++;}}return count;}

TimedCache 不限制大小,只有当对象过期时才会删除。标准的chache方法不会显式的调用删除(prune),而是根据定义好的延迟进行定时删除。

public TimedCache(long timeout) {this.cacheSize = 0;this.timeout = timeout;cacheMap = new HashMap<K, CacheObject<K,V>>();}// ---------------------------------------------------------------- prune/*** Prunes expired elements from the cache. Returns the number of removed objects.*/@Overrideprotected int pruneCache() {int count = 0;Iterator<CacheObject<K,V>> values = cacheMap.values().iterator();while (values.hasNext()) {CacheObject co = values.next();if (co.isExpired() == true) {values.remove();count++;}}return count;}// ---------------------------------------------------------------- auto pruneprotected Timer pruneTimer;/*** Schedules prune.*/public void schedulePrune(long delay) {if (pruneTimer != null) {pruneTimer.cancel();}pruneTimer = new Timer();pruneTimer.schedule(new TimerTask() {@Overridepublic void run() {prune();}}, delay, delay);}/*** Cancels prune schedules.*/public void cancelPruneSchedule() {if (pruneTimer != null) {pruneTimer.cancel();pruneTimer = null;}}

注意,还提供了一个FileLFUCache,没有继承AbstractCacheMap.用LFU将文件缓存到内存,极大加快访问常用文件的性能。

    protected final LFUCache<File, byte[]> cache;protected final int maxSize;protected final int maxFileSize;protected int usedSize;/*** Creates file LFU cache with specified size. Sets* {@link #maxFileSize max available file size} to half of this value.*/public FileLFUCache(int maxSize) {this(maxSize, maxSize / 2, 0);}public FileLFUCache(int maxSize, int maxFileSize) {this(maxSize, maxFileSize, 0);}/*** Creates new File LFU cache.* @param maxSize total cache size in bytes* @param maxFileSize max available file size in bytes, may be 0* @param timeout timeout, may be 0*/public FileLFUCache(int maxSize, int maxFileSize, long timeout) {this.cache = new LFUCache<File, byte[]>(0, timeout) {@Overridepublic boolean isFull() {return usedSize > FileLFUCache.this.maxSize;}@Overrideprotected void onRemove(File key, byte[] cachedObject) {usedSize -= cachedObject.length;}};this.maxSize = maxSize;this.maxFileSize = maxFileSize;}// ---------------------------------------------------------------- get/*** Returns max cache size in bytes.*/public int getMaxSize() {return maxSize;}/*** Returns actually used size in bytes.*/public int getUsedSize() {return usedSize;}/*** Returns maximum allowed file size that can be added to the cache.* Files larger than this value will be not added, even if there is* enough room.*/public int getMaxFileSize() {return maxFileSize;}/*** Returns number of cached files.*/public int getCachedFilesCount() {return cache.size();}/*** Returns timeout.*/public long getCacheTimeout() {return cache.getCacheTimeout();}/*** Clears the cache.*/public void clear() {cache.clear();usedSize = 0;}// ---------------------------------------------------------------- getpublic byte[] getFileBytes(String fileName) throws IOException {return getFileBytes(new File(fileName));}/*** Returns cached file bytes.*/public byte[] getFileBytes(File file) throws IOException {byte[] bytes = cache.get(file);if (bytes != null) {return bytes;}// add filebytes = FileUtil.readBytes(file);if ((maxFileSize != 0) && (file.length() > maxFileSize)) {// don't cache files that size exceed max allowed file sizereturn bytes;}usedSize += bytes.length;// put file into cache// if used size > total, purge() will be invoked
        cache.put(file, bytes);return bytes;}

转载于:https://www.cnblogs.com/davidwang456/p/4650755.html

jodd-cache集锦相关推荐

  1. jodd.cache.LRUCache: 小巧的本地缓存, 及其并发bug

    LRU (Least recently used) 最近最少使用,如果数据最近被访问过,那么将来被访问的几率也更高. LFU (Least frequently used) 最不经常使用,如果一个数据 ...

  2. 小巧的本地缓存Jodd

    小巧的本地缓存Jodd 说到缓存,大家容易想到memcached和redis,它们大名鼎鼎,但都是远程缓存,需要通过TCP网络访问. 这些缓存服务器本身性能很好,但不管性能再怎么好,也要通过网络访问, ...

  3. 一份平民化的应用性能优化检查列表(完整篇)--转

    原文地址:http://calvin1978.blogcn.com/articles/checklist.html 1.总原则 一些正确但稍显废话的原则,但能指导后面每个章节的优化,所以还是要啰嗦一次 ...

  4. 一份平民化的应用性能优化CheckList(完整篇)

    1 总原则 作者说:这是一些正确但稍显废话的原则,但能指导后面每个章节的优化,所以还是要啰嗦一次. (1) 可扩展性架构,堆机器能不能解决问题是最最优先考虑的问题 (2)去中心化的点对点通信,优于通过 ...

  5. 闪存系统性能优化方向集锦?AC timing? Cache? 多路并发?

    声明 主页: 元存储的博客_CSDN博客 依公开知识及经验整理,如有误请留言. 个人辛苦整理,付费内容,禁止转载. 内容摘要 1. 优化 AC Timing,提升总线频率 1.1 优化 AC Timi ...

  6. LAMP 关键数据集锦技术选项参考

    LAMP 关键数据集锦技术选项参考 源自日积月累自己的其他人的经验总结 负载均衡 LVS 工作在四层,内核态,性能极高,有VIP功能,配合 keepalived 做有效的 心跳检查和负载均衡安装配置 ...

  7. jodd忽略ssl证书_Jodd - Java界的瑞士军刀,无法想象的轻量级工具包

    Jodd介绍 Jodd是对于Java开发更便捷的开源迷你框架,包含工具类.实用功能的集合,总包体积不到1.7M. Jodd构建于通用场景使开发变得简单,但Jodd并不简单!它能让你把事情做得更好,实现 ...

  8. ArcGIS Server常见问题集锦(转载)

    ArcGIS Server常见问题集锦(转载) 安装部署问题 1 用户名问题    在GIS Server PostInstall过程中会涉及到两个用户,默认情况下一个ArcGISSOM,一个是Arc ...

  9. 十月下旬腾讯,网易游戏,百度盛大迅雷校园招聘笔试题集锦(10.25)

    十月下旬腾讯,网易游戏,百度最新校园招聘笔试题集锦 引言 笔试啊,笔试,面试啊,面试,找工作啊,找工作.此文十月百度,阿里巴巴,迅雷搜狗最新面试十一题已经整理了最新的面试题70道,本文依次整理腾讯,网 ...

最新文章

  1. 体验是情感的(译稿)
  2. vue使用echarts可视化图形插件
  3. ad中电容用什么封装_图文并茂用最通俗易懂的对话为你讲解电子技术知识- C什么是电容?...
  4. 安装与设置Visual SVN
  5. php自定义通讯协议,PHP自定义协议攻击 by L0st
  6. html如何去掉有无标题点,HTML中,如何去掉某个元素下的一些特殊标签?
  7. [转载] python提取list中特定的元素_Python中list列表的基本操作
  8. CSS3 修改和去除移动端点击事件出现的背景框
  9. 如果浏览器大战的格局改变会怎样?
  10. seo与sem的区别
  11. 计算机组成原理——指令分析
  12. 【C语言】switch用法
  13. Allegro各属性说明如 Clines或者Cline Segs
  14. 企业10大HR软件分析对比(精)
  15. iOS 蓝牙连接小米手环
  16. 游虎DOTA专区 - 内容丰富的DOTA专区,DOTA录像分析很不错的..
  17. python学习-第9课
  18. 价格数字转换成大写汉字的一个类
  19. Tensorflow2.0学习-Keras Tuner 妙用 (六)
  20. Pytorch 并行训练(DP, DDP)的原理和应用

热门文章

  1. php如何获取js文本框内的内容,js获取input标签的输入值(实例代码)
  2. python猜单词游戏_磁盘空间不足。
  3. 程序流程图_干货收藏 | Java 程序员必备的一些流程图
  4. oracle dbcontrol界面,oracle enterprise manager配置简介
  5. a9 pro android 7,惊艳 | 全球最具性价比双摄手机,Blackview A9 Pro
  6. 利用python进行数据分析学习笔记 第7章(3)
  7. pandas重命名变量名
  8. 简单回声服务器的实现
  9. mybatis insert 返回主键_面试准备季——MyBatis 面试专题(含答案)
  10. mongodb聚合查询优化_MongoDB聚合查询详解