web缓存--Ehcache使用
2015-07-14 17:48
218 查看
如果想使用缓存玩玩,也可以自己写个,就是用map集合。
写个MyCache类
在DAO中调用
这里面不用关心调用的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中的内容:
然后写个Ehcache的类。
先创建CacheManager对象CacheManager cacheManager = new CacheManager();
然后获取Ehcache接口,通过Ehcache cache = cacheManager.getEhcache(cacheName);其中cacheName是ehcache.xml中属性name的值。可以根据需要多加个标签。
获取值是使用Element对象。
写个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; } }
相关文章推荐
- UINavigationController返回手势失效问题
- Matlab中mat2cell的使用
- U盘安装Centos7.1操作系统的问题记录
- 2012年东京区域赛 UVAlive6182~6191
- 针对wordpress的二次开发
- 【AngularJS】【01】简介
- How to disable vertical scrolling in UIScrollView (Obj-C)
- poj 3273 Monthly Expense
- C++运算符优先级
- android studio删除Model
- 【第三方登录】之微信第三方登录
- 判断邮件地址是否存在的方法 推荐
- hibernate映射文件
- 黑马程序员_JAVA之GUI与Socket
- Python监控进程性能数据并绘图保存为PDF文档
- cocos2d学习之旅(零)
- LinearLayout测量过程分析
- 分类——逻辑回归模型
- CSS3中HSL和HSLA的简单使用示例
- java实现新浪微博Oauth接口发送图片和文字的方法