`
minzaipiao
  • 浏览: 148996 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

项目中使用的ehCache的工具类

    博客分类:
  • Java
阅读更多
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();
	}

}

分享到:
评论

相关推荐

    Ehcache工具类

    本工具类包括初始化cache 修改缓存容器配置 向指定容器中设置值 取值 删除指定的ehcache容器 删除所有容器 删除容器内所有元素 释放CacheManage 获取所有的cache名称

    使用ehcache的工具类

    使用ehcahe时自己做的工具类,方便以后使用

    ehcache开发工具包

    1. `ehcache-1.6.2.jar`: 这是EhCache的主库文件,包含了所有必要的类和方法,开发者可以通过导入这个JAR文件到项目中来使用EhCache的功能。1.6.2是该版本的编号,表示这是EhCache的一个特定版本。 2. `ehcache-...

    mybatis-ehcache

    1. 添加Ehcache和mybatis-ehcache的依赖到项目中。 2. 配置MyBatis的配置文件(`mybatis-config.xml`),启用二级缓存并指定使用Ehcache。 ```xml ... <id>com.example.mapper.YourMapper</id> <type>org....

    SpringBoot中使用Ehcache的详细教程

    【SpringBoot中使用Ehcache的详细教程】 EhCache是一个高效的Java进程内缓存框架,因其快速、轻量级的特性,常被用作Hibernate的默认CacheProvider。本教程将详细讲解如何在SpringBoot项目中集成并使用Ehcache。 #...

    ehcache.zip

    在使用"ehcache.zip"这个压缩包时,你需要将其解压,并将包含的jar文件添加到你的项目类路径中。如果遇到无法使用的情况,可以联系发布者获取帮助。同时,确保你的应用程序配置了正确的Ehcache设置,以便充分发挥其...

    Spring 使用注解配置使用ehcache

    本文将深入探讨如何在Spring中使用注解来配置Ehcache。 首先,我们需要在项目中引入Ehcache的相关依赖。如果你使用的是Maven,可以在pom.xml文件中添加以下依赖: ```xml <groupId>net.sf.ehcache</groupId> ...

    EHCache的使用随记

    1. **引入依赖**:在项目中添加`ehcache-1.5.0.jar`依赖,以及`backport-util-concurrent.jar`(用于支持旧版JDK的并发工具包)。 2. **创建缓存管理器**:通过`CacheManager.getInstance()`获取或创建缓存管理器。...

    ehcache使用,以及集群配置

    2. **安全性**: 如果在生产环境中使用,应考虑配置Ehcache的安全策略,限制未授权的访问。 3. **更新策略**: 理解Ehcache的不同更新策略,如`PUT`、`PUT_IF_ABSENT`等,以便根据业务需求选择合适的操作。 总结,...

    Maven项目Spring4+Hibernate4+shiro+Ehcache项目集成

    在本项目中,我们主要探讨的是一个基于Maven构建的Java Web应用,它整合了Spring 4、Hibernate 4、Apache Shiro以及Ehcache等多个关键框架,旨在实现用户角色菜单管理的功能。以下是对这些技术及其集成应用的详细...

    spring集成ehcache所需的jar包

    spring集成ehcache所需的jar包

    SSM+ehcache-Jar包

    Ehcache不仅可以作为本地缓存,还可以在分布式环境中使用,支持集群和 terracotta 集群解决方案。 压缩包中的具体文件说明如下: - **ehcache-web-2.0.4.jar**:这是Ehcache的Web扩展,提供了与Web应用集成的功能...

    spring boot 使用jsp 集成hibernate+shiro+ehcache项目分享

    在项目中使用 Ehcache: - 添加 Ehcache 依赖。 - 配置 Ehcache,指定缓存策略,如缓存大小、存活时间和过期时间等。 - 在需要缓存的方法上添加 @Cacheable 注解,让 Spring AOP 自动处理缓存逻辑。 - 可以通过 @...

    ehcache资料(包含ehcache jar包)

    1. **添加依赖**:将提供的Ehcache jar包引入到项目类路径中,如果是Maven或Gradle项目,需要在配置文件中添加相应的依赖。 2. **配置Ehcache**:创建XML配置文件,定义缓存的名称、大小、过期策略等参数。 3. **...

    Spring+Ehcache集成

    **Spring与Ehcache集成详解** ...在实际项目中,还可以根据需求调整Ehcache的配置,如设置不同的缓存策略,或者使用分布式缓存等。同时,也可以结合Spring Boot和Spring Data JPA等现代框架,进一步简化配置和使用。

    Ehcache(2): Ehcache实例在Eclipse中的配置 改进

    本篇文章将详细讲解如何在Eclipse集成开发环境中配置Ehcache,以配合Hibernate进行使用,从而提升数据访问效率。首先,我们需要了解Ehcache的基本概念和工作原理。 Ehcache的核心功能是提供内存缓存服务,它分为三...

    Ehcache_Hello

    通过这个案例,开发者可以深入理解Ehcache的基本用法和工作流程,为实际项目中的缓存优化打下基础。 **总结** Ehcache作为高效的缓存工具,能够有效提升应用程序性能,减少数据库压力。掌握其基本API和配置方法是...

    ehcache1.6 和 ehcache-web-2.0.4

    它也提供了监控和管理Web应用程序中Ehcache的工具,帮助开发者了解缓存的使用情况,优化性能。 SLF4J(Simple Logging Facade for Java)是一个日志抽象层,允许用户在部署时插入所需的日志实现。slf4j-api-1.7.21....

    一个关于ehcache的项目

    - `CacheUtil.java`:工具类,封装了Ehcache的基本操作,如缓存的get和put方法。 - `TestCache.java`:测试类,演示如何使用Ehcache缓存数据。 通过分析这些文件,你可以了解到Ehcache在实际项目中的使用方式,如何...

    ehcache缓存的jar包和配置文件

    下面将详细介绍Ehcache的核心概念、配置以及如何在项目中使用。 1. Ehcache核心概念: - 缓存:缓存是存储临时数据的地方,以便快速检索。Ehcache提供内存和磁盘存储,可以设置不同的缓存策略。 - 缓存管理器...

Global site tag (gtag.js) - Google Analytics