用Map实现简单缓存
2016-07-06 16:30
309 查看
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* 缓存池
* @author **
* @date 2016年7月6日 下午3:22:54
*/
public class CachePool {
private static final Logger logger = LogManager.getLogger(CachePool.class);
private static CachePool cachePool;
private Map<Object, Object> cacheItems;
private CachePool() {
cacheItems = new ConcurrentHashMap<Object, Object>();
}
@Autowired
private ISearchMerchantService searchMerchantService;
/**
* 获取唯一实例
* @return
*/
public static CachePool getInstance() {
if (cachePool == null) {
synchronized (CachePool.class) {
if(cachePool == null) {
cachePool = new CachePool();
}
}
}
return cachePool;
}
/**
* 清空cache
*/
public void clearAllIterms() {
cacheItems.clear();
}
/**
* 刷新cache
*/
public void refresh () {
logger.info("Before refreshing Cache size = {}", cachePool.getSize() );
//清空缓存
cacheItems.clear();
//重新查库
/*Map<String, String> merchantMap = searchMerchantService.searchMerchants();
//遍历map
Set<String> set = merchantMap.keySet();
Iterator<String> it = set.iterator();
while(it.hasNext()) {
Object key = it.hasNext();
Object value = merchantMap.get(key);
if(key != null && value != null) {
cachePool.putCacheItem(key.toString(), value.toString());
}
}*/
logger.info("After the refresh Cache size = {}", cachePool.getSize() );
}
/**
* 获取指定cache信息
* @param key
* @return
*/
public Object getCacheItem(Object key) {
if(cacheItems.containsKey(key)) {
return cacheItems.get(key);
}
return null;
}
/**
* 存放cache信息
* @param key
* @param value
*/
public void putCacheItem(Object key, Object value) {
if(!cacheItems.containsKey(key)) {
cacheItems.put(key, value);
}
}
/**
* 获取cache长度
* @return
*/
public int getSize() {
return cacheItems.size();
}
public Map<Object, Object> getCacheItems() {
return cacheItems;
}
public void setCacheItems(Map<Object, Object> cacheItems) {
this.cacheItems = cacheItems;
}
}
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* 缓存池
* @author **
* @date 2016年7月6日 下午3:22:54
*/
public class CachePool {
private static final Logger logger = LogManager.getLogger(CachePool.class);
private static CachePool cachePool;
private Map<Object, Object> cacheItems;
private CachePool() {
cacheItems = new ConcurrentHashMap<Object, Object>();
}
@Autowired
private ISearchMerchantService searchMerchantService;
/**
* 获取唯一实例
* @return
*/
public static CachePool getInstance() {
if (cachePool == null) {
synchronized (CachePool.class) {
if(cachePool == null) {
cachePool = new CachePool();
}
}
}
return cachePool;
}
/**
* 清空cache
*/
public void clearAllIterms() {
cacheItems.clear();
}
/**
* 刷新cache
*/
public void refresh () {
logger.info("Before refreshing Cache size = {}", cachePool.getSize() );
//清空缓存
cacheItems.clear();
//重新查库
/*Map<String, String> merchantMap = searchMerchantService.searchMerchants();
//遍历map
Set<String> set = merchantMap.keySet();
Iterator<String> it = set.iterator();
while(it.hasNext()) {
Object key = it.hasNext();
Object value = merchantMap.get(key);
if(key != null && value != null) {
cachePool.putCacheItem(key.toString(), value.toString());
}
}*/
logger.info("After the refresh Cache size = {}", cachePool.getSize() );
}
/**
* 获取指定cache信息
* @param key
* @return
*/
public Object getCacheItem(Object key) {
if(cacheItems.containsKey(key)) {
return cacheItems.get(key);
}
return null;
}
/**
* 存放cache信息
* @param key
* @param value
*/
public void putCacheItem(Object key, Object value) {
if(!cacheItems.containsKey(key)) {
cacheItems.put(key, value);
}
}
/**
* 获取cache长度
* @return
*/
public int getSize() {
return cacheItems.size();
}
public Map<Object, Object> getCacheItems() {
return cacheItems;
}
public void setCacheItems(Map<Object, Object> cacheItems) {
this.cacheItems = cacheItems;
}
}
相关文章推荐
- android Google Map获取地理位置信息的方法
- Spark RDD API详解(一) Map和Reduce
- Python中map()函数浅析
- Android使用Google Map浅谈
- 页面缓存:内存和文件之间的那些事
- 浅析SQL Server中的执行计划缓存(上)
- Enterprise Library for .NET Framework 2.0缓存使用实例
- PowerShell中编程清空IE缓存方法
- PowerShell中使用.NET将程序集加入全局程序集缓存
- C#中缓存的基本用法总结
- Erlang中的映射组Map详细介绍
- c++中map的基本用法和嵌套用法实例分析
- Android实现图片异步加载并缓存到本地
- wap开发中如何有效的利用缓存减少消息的传送量
- PHP基于文件存储实现缓存的方法
- smarty缓存用法分析
- 在ASP.NET 2.0中操作数据之五十九:使用SQL缓存依赖项SqlCacheDependency
- 在ASP.NET 2.0中操作数据之五十八:在程序启动阶段缓存数据
- 在ASP.NET 2.0中操作数据之五十七:在分层架构中缓存数据
- 引用全局程序集缓存内的程序集的方法