ehcache缓存的配置和使用
1.缓存和缓存的作用
缓存就是数据交换的缓冲区,由于缓存的运行速度比内存快得多,故缓存的作用就是帮助硬件更快地运行。当服务器的数据库中的某数据被频繁访问的时候,由于频繁的读取数据库会增大数据库的压力,同时也影响效率。把高频访问的数据放到缓存中,可以提高访问效率,提升用户体验。常用的开源缓存方法有ehcache,memcache,redis,mongoDB等,这些缓存方式各有优缺点。
1.ehcache是一个纯Java的进程内缓存框架,具有快速、精干等特点。优点是小巧,使用简单,速度快,效率高,缺点是缓存共享麻烦,集群分布式应用不方便。
2.memcached是一套分布式的高速缓存系统,大致的原理也和ehcache相同.相对ehcache而言,memcached是一个工 具,ehcache是一个框架,memcached更加底层更加灵活.Memcached的优点是可以利用多核优势,单实例吞吐量极高。缺点是只支持简单的key/value数据结构,不像Redis可以支持丰富的数据类型。无法进行持久化,无法进行数据同步。
3.Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库。优点是支持多种数据结构,缺点是Redis只能使用单线程,性能受限于CPU性能。
4.mongoDB 是一种文档性的数据库,主要解决海量数据的访问效率问题。缺点是不适合小数据量缓存,优点是适合大文件如日志等文件的缓存。
2.ehcache的配置
1.在maven的pom.xml文件中加入以下依赖
<ehcache.version>2.10.4</ehcache.version>
<dependency><!-- Ehcache 2.x //-->
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache.version}</version>
</dependency>
2.在resource问价夹下放入ehcache.xml文件
<ehcache updateCheck="false" dynamicConfig="false">
<diskStore path="java.io.tmpdir"/>
<cacheManagerEventListenerFactory class="" properties=""/>
<!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.
The following attributes are required for defaultCache:
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
overflowToDisk="true">
</defaultCache>
<!--Predefined caches. Add your cache configuration settings here.
If you do not have a configuration for your cache a WARNING will be issued when the
CacheManager starts
The following attributes are required for defaultCache:
name - Sets the name of the cache. This is used to identify the cache. It must be unique.
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<cache name="sysCache"
maxElementsInMemory="5000"
eternal="false"
timeToIdleSeconds="3000"
timeToLiveSeconds="3000"
overflowToDisk="false">
</cache>
</ehcache>
3.整合spring的配置,在resource文件夹下创建spring-ehcache.xml文件。
<!-- 使用注解的方式使用ehcaceh 则配置启用缓存注解开关 -->
<!--<cache:annotation-driven cache-manager="cacheManager"/>-->
<bean id="cacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml">
</bean>
<!-- 声明cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="cacheManagerFactory" >
</bean>
并在web.xml中添加配置spring-ehcache.xml
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
classpath:spring-ehcache.xml
</param-value>
</context-param>
3.ehcache的使用
ehcache的使用有两种方式,一种是代码操作,一种是注解操作。
1.代码操作:
//存入缓存 其中cacheName 和ehcache.xml中的配置的name保持一致
public void saveCache (String cacheName,String key,String value){
CacheManager create = CacheManager.create(this.getClass().getResourceAsStream("/ehcache.xml"));
Cache cache = create.getCache(cacheName);
cache.put(new Element(key, value));
}
//取出缓存
public String getCache(String cacheName,String key){
CacheManager create = CacheManager.create(this.getClass().getResourceAsStream("/ehcache.xml"));
Cache cache = create.getCache(cacheName);
Element element = cache.get(key);
return (String) element.getObjectValue();
}
2.注解操作:
在service的方法上加上注解,如:@Cacheable(value,key),value的值和ehcache.xml中的配置的name保持一致
@Cacheable,表明所修饰的方法是可以缓存的:当第一次调用这个方法时,它的结果会被缓存下来,在缓存的有效时间内,以后访问这个方法都直接返回缓存结果,不再执行方法中的代码段
@CachePut,与@Cacheable不同,@CachePut不仅会缓存方法的结果,还会执行方法的代码段
@CacheEvict,与@Cacheable功能相反,@CacheEvict表明所修饰的方法是用来删除失效或无用的缓存数据
总结:ehcache的使用相对比较简单一下,以上内容也是借鉴了一些前人的基础,加上自己的一些理解,如有错误,欢迎指正。