您的位置:首页 > 其它

web缓存--Ehcache使用

2015-07-14 17:48 218 查看
如果想使用缓存玩玩,也可以自己写个,就是用map集合。

写个MyCache类

package com.xingguo.util;

import java.util.HashMap;
import java.util.Map;

public class MyCache {

private MyCache(){}

private static Map<String,Object> cache = new HashMap<String, Object>();

//存入缓存
public static void put(String key,Object value){
cache.put(key, value);
}

//获取缓存
public static Object get(String key){
return cache.get(key);
}

//删除缓存
public static void remove(String key){
cache.remove(key);
}

}


在DAO中调用

package com.xingguo.dao;

import com.xingguo.db.DBHelp;
import com.xingguo.db.rowmapper.BeanPropertyRowMapper;
import com.xingguo.entity.AlarmPerceivedSeverity;
import com.xingguo.util.MyCache;

public class AlarmPerDao {
private static DBHelp db = new DBHelp();
public AlarmPerceivedSeverity findById(Integer id){
AlarmPerceivedSeverity alarm = null;
Object obj = MyCache.get("alarm:"+id);
if(obj != null){
alarm = (AlarmPerceivedSeverity) obj;
}else{
String sql = "select * from alarm_perceivedseverity where id= ";
alarm = db.queryForObject(sql, new BeanPropertyRowMapper<AlarmPerceivedSeverity>(AlarmPerceivedSeverity.class));
MyCache.put("alarm:"+id, alarm);
}
return alarm;
}
}


这里面不用关心调用的DBHelp,这只是执行sql。其他的jdbc也可以。主要是调用MyCache中的put和get。在程序中如果有删除表数据,应该调用MyCache.remove()。

关于Ehcache的使用。首先应该在项目用引用它的jar包。可以去http://ehcache.org/downloads/catalog 下载。把里面的3个jar包导入进去。我下载的是ehcache-2.10.0-distribution.tar.gz。

然后把ehcache.xml导入项目的src目录下,这样在调用时可以直接加载到该配置。可以修改ehcache.xml中的内容:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir"/>

<cache name="alarmPerceivedSeverity"
maxElementsInMemory="1000" <!-- 内存中最大缓存对象数 -->
eternal="false" <!-- 是否持久化,如果为true下面的将不起作用 -->
diskSpoolBufferSizeMB="20"<!-- 磁盘缓存的缓存区大小 -->
timeToIdleSeconds="300"<!-- 允许闲置时间 -->
timeToLiveSeconds="600"<!-- 允许存活时间,单位是秒 -->
memoryStoreEvictionPolicy="LRU"<!-- 根据指定的策略去清理内存 -->
overflowToDisk="true">
</cache>

</ehcache>


然后写个Ehcache的类。

package com.xingguo.util;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

public class EhCacheUtil {

private EhCacheUtil(){};

private static CacheManager cacheManager = new CacheManager();

public static void put(String cacheName,Object key,Object value){

Ehcache cache = cacheManager.getEhcache(cacheName);
Element element = new Element(key,value);
cache.put(element);
}

public static Object get(String cacheName,Object key){
Ehcache cache = cacheManager.getEhcache(cacheName);
Element element = cache.get(key);
if(element != null){
return element.getObjectValue();
}else{
return null;
}
}

public static void remove(String cacheName,Object key){
Ehcache cache = cacheManager.getEhcache(cacheName);
Element element = cache.get(key);
cache.remove(element);
}
}


先创建CacheManager对象CacheManager cacheManager = new CacheManager();

然后获取Ehcache接口,通过Ehcache cache = cacheManager.getEhcache(cacheName);其中cacheName是ehcache.xml中属性name的值。可以根据需要多加个标签。

获取值是使用Element对象。

package com.xingguo.dao;

import com.xingguo.db.DBHelp;
import com.xingguo.db.rowmapper.BeanPropertyRowMapper;
import com.xingguo.entity.AlarmPerceivedSeverity;
import com.xingguo.util.EhCacheUtil;

public class AlarmPerDao {

private static DBHelp db = new DBHelp();
public AlarmPerceivedSeverity findById(Integer id){
AlarmPerceivedSeverity alarm = null;
Object obj = EhCacheUtil.get("alarmPerceivedSeverity", "alarm:"+id);
if(obj != null){
alarm = (AlarmPerceivedSeverity) obj;
}else{
String sql = "select * from alarm_perceivedseverity where id= ";
alarm = db.queryForObject(sql, new BeanPropertyRowMapper<AlarmPerceivedSeverity>(AlarmPerceivedSeverity.class));
EhCacheUtil.put("alarmPerceivedSeverity","alarm:"+id, alarm);
}
return alarm;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: