cacheUtils
来源:互联网 发布:购买域名的网站 编辑:程序博客网 时间:2024/06/06 10:02
一个小小的内存缓存操作类,测试版,我自己都不敢用,
package test;import java.util.LinkedHashMap;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;public class CacheUtils2 { private static Map<String, Map<String, Object>> pool = new ConcurrentHashMap<String, Map<String, Object>>(); private static String name; private static int maxNumber; public CacheUtils2(String name, int maxNumber) { super(); this.name = name; this.maxNumber = maxNumber; } private static CacheUtils2 cache=null; public static CacheUtils2 getInstance(String name,int maxNumber) { if (cache == null) { cache = new CacheUtils2(name,maxNumber); } return cache; } public static void add(String key, String value) { Map<String, Object> oldCache = pool.get(name); if (oldCache == null) { Map<String, Object> newCache = new LinkedHashMap<String, Object>(); newCache.put(key, value); pool.put(name, newCache); } else { if (oldCache.size() >= maxNumber) { oldCache.remove(oldCache.entrySet().iterator().next().getKey()); oldCache.put(key, value); } else { oldCache.put(key, value); } } } public static Object get(String key) { Map<String, Object> Caches = pool.get(name); return Caches.get(key); } public static void main(String[] args) { CacheUtils2 u = CacheUtils2.getInstance("url", 25); for (int i = 0; i <= 30; i++) { u.add("key" + i, "value" + i); } u.add("key30", "value11100"); u.add("key29", "value11100"); u.add("key28", "value11100"); u.add("key27", "value11100"); for (int i = 0; i <= 30; i++) { System.out.println(u.get("key" + i)); } CacheUtils2 u2 = CacheUtils2.getInstance("url", 10); for (int i = 0; i <= 30; i++) { System.out.println(u.get("key" + i)); } }}
阅读全文
0 0
- CacheUtils
- cacheUtils
- 你想要的CacheUtils
- 基础工具类-1-CacheUtils
- Android studio 项目构建七|压缩代码和资源
- 文件对话框CFileDialog类:打开和保存
- 中国剩余定理
- 微信公众号开发--接收与回复消息(Java)
- 前端跨域的问题
- cacheUtils
- 状态选择器及Button形状
- android 动画详解
- angularjs指令中的compile与link函数详解
- 关于字典表与实例表的思考
- vue指令大全
- Leetcode84. Largest Rectangle in Histogram.
- Master: Error: JAVA_HOME is not set and could not be found.
- 事物的五种隔离级别