import java.io.Serializable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
/**
* 缓存管理器
* @author dengmin
*/
public class UICacheManager {
final static Log log = LogFactory.getLog(UICacheManager.class);
public static CacheManager manager;
static{
try {
manager = CacheManager.getInstance();
if(manager==null)
manager = CacheManager.create();
} catch (CacheException e) {
log.fatal("Initialize cache manager failed.", e);
}
}
/**
* 从缓存中获取对象
* @param cache_name
* @param key
* @return
*/
public static Serializable getObjectCached(String cache_name, Serializable key){
Cache cache = getCache(cache_name);
if(cache!=null){
try {
Element elem = cache.get(key);
if(elem!=null && !cache.isExpired(elem))
return elem.getValue();
} catch (Exception e) {
log.error("Get cache("+cache_name+") of "+key+" failed.", e);
}
}
return null;
}
/**
* 把对象放入缓存中
* @param cache_name
* @param key
* @param value
*/
public synchronized static void putObjectCached(String cache_name, Serializable key, Serializable value){
Cache cache = getCache(cache_name);
if(cache!=null){
try {
cache.remove(key);
Element elem = new Element(key, value);
cache.put(elem);
} catch (Exception e) {
log.error("put cache("+cache_name+") of "+key+" failed.", e);
}
}
}
/**
* 获取指定名称的缓存
* @param arg0
* @return
* @throws IllegalStateException
*/
public static Cache getCache(String arg0) throws IllegalStateException {
return manager.getCache(arg0);
}
/**
* 获取缓冲中的信息
* @param cache
* @param key
* @return
* @throws IllegalStateException
* @throws CacheException
*/
public static Element getElement(String cache, Serializable key) throws IllegalStateException, CacheException{
Cache cCache = getCache(cache);
return cCache.get(key);
}
/**
* 停止缓存管理器
*/
public static void shutdown(){
if(manager!=null)
manager.shutdown();
}
}
分享到:
相关推荐
本工具类包括初始化cache 修改缓存容器配置 向指定容器中设置值 取值 删除指定的ehcache容器 删除所有容器 删除容器内所有元素 释放CacheManage 获取所有的cache名称
使用ehcahe时自己做的工具类,方便以后使用
EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
spring集成ehcache所需的jar包
mybatis 缓存包 及源码 mybatis-ehcache-1.0.3 用于配置mybatis缓存必须jar包,附带源码。
Ehcache的二级缓存jar包,收集的jar包,实际使用无问题,欢迎下载,如不能使用,请及时联系发布者
该Jar整合了Spring SpringMVC Mybatis以及Ehcache所需要的所有Jar包
基于公司的项目在Spring中集成Ehcache,并提供EhcaheUtils工具类,并通过Spring的AOP编程实现方法缓存注解话,先奉献出核心代码,需要的朋友可以参考哦!
mybatis缓存, mybatis的缓存EhCache,mybatis工具,mybatis工具类
EhCacheUtils - 基于ehcache的工具类 LruCacheUtils - 基于LinkedHashMap实现LRU缓存的工具类 MemcachedUtils - 基于memcached的工具类 RedisUtils - 基于redis的工具类,与redis的集群配置无缝结合 db JdbcUtils ...
见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache.xml及web.xml的pageEhCacheFilter 4.MyBatis+Maven代码生成工具。见bin目录 5.作为项目或者技术研究的基础架构。必要的jar包依赖都已配置好,...
共9种常用NoSQL数据库操作,工具类,演示代码。用于整理日常常用的开发模式,一是作为开发笔记以备我自己日后使用,二是分享出来供大家参考。 重点介绍Redis,MongoDB,ElasticSeach,Cassandra模型数据库,因为它们...
cacheEhCacheUtils - 基于ehcache的工具类LruCacheUtils - 基于LinkedHashMap实现LRU缓存的工具类MemcachedUtils - 基于memcached的工具类XMemcachedUtils - 基于memcached的工具类(使用XMemcached客户端)Redis...
见DaoImpl类 3.Ehcache方法缓存及页面缓存。见applicationContext-cache.xml及web.xml的pageEhCacheFilter 4.MyBatis+Maven代码生成工具。见bin目录 5.作为项目或者技术研究的基础架构。必要的jar包依赖都已配置好,...
收集的jar包和资料,实际使用无问题,欢迎下载,如不能使用,请及时联系发布者 简介:二级缓存ehcache pom、开启二级缓存和查询缓存配置
Ehcache配置详解及CacheManager使用,设置时间和调用工具类实现
工具类:Apache Commons、FastJson、EASYPOI、BladeToolBox 2、前端 JS框架:jQuery CSS框架:Twitter Bootstrap 客户端验证:JQuery-html5Validate 富文本:KindEcitor 数据表格:jqGrid 树结构控件:...
这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个jar文件是所有应用...
基于java的map和timer实现本地缓存及定时清理失效缓存的功能 本项目仅用于初学者学习使用 初学者可基于此项目初步了解缓存实现的基本原理 后期在项目中使用建议使用现成的缓存框架:redis、ehcache等
项目选型:MVC框架:Spring mvc,使用Thymeleaf渲染view应用层:Spring 3持久层:Mybatis 3缓存:Spring Cache,提供redis缓存实现安全框架:Shiro,提供基于数据库的Realm及Principal基类,可采用框架提供的Ehcache...