Ehcache整合spring配置

本文介绍如何在Spring框架中整合Ehcache实现缓存管理。通过配置ehcache.xml和application_spring_cache.xml,利用@Cacheable和@CacheEvict等注解控制缓存的存储与清除。

为了提高系统的运行效率,引入缓存机制,减少数据库访问和磁盘IO。下面说明一下ehcache和spring整合配置。

1.   需要的jar包

slf4j-api-1.6.1.jar

ehcache-core-2.1.0.jar

ehcache-spring-annotations-1.1.2.jar

slf4j-log4j12-1.6.1.jar

spring-context-support-4.0.6.RELEASE.jar


2.   ehcache.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ehcache name=“menu”>  
  3.      
  4.     <diskStore path="java.io.tmpdir/ehcache"/>  
  5.       
  6.     <!-- 默认缓存 -->  
  7.     <defaultCache  
  8.            maxElementsInMemory="1000"  
  9.            eternal="false"  
  10.            timeToIdleSeconds="120"  
  11.            timeToLiveSeconds="120"  
  12.            overflowToDisk="false"/>  
  13.              
  14.     <!-- 菜单缓存 -->      
  15.     <cache name="menuCache"   
  16.            maxElementsInMemory="1000"   
  17.            eternal="false"  
  18.            timeToIdleSeconds="120"  
  19.            timeToLiveSeconds="120"  
  20.            overflowToDisk="false"   
  21.            memoryStoreEvictionPolicy="LRU"/>  
  22.       
  23. </ehcache>  
3.   application_spring_cache.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"   
  5.     xmlns:cache="http://www.springframework.org/schema/cache"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.         http://www.springframework.org/schema/context   
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.         http://www.springframework.org/schema/cache  
  11.         http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">  
  12.       
  13.     <cache:annotation-driven cache-manager="cacheManager"/>  
  14.       
  15.      <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
  16.         <property name="configLocation" value="classpath:application/ehcache.xml" />  
  17.     </bean>  
  18.       
  19.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">      
  20.         <property name="cacheManager"  ref="cacheManagerFactory"/>      
  21.     </bean>  
  22.    
  23. </beans>  

web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:conf/applicationContext.xml
    </param-value>
</context-param>

applicationContext.xml

<import resource="spring-cache-config.xml"></import>


4.   使用

首先在ehcache.xml中配置缓存策略,即添加一组cache。

4.1     添加缓存

[java]  view plain  copy
  1. @Cacheable(value="menuCache",key="'UserMenuKey'+#userid")  
  2.     public List<MenuBean> queryMenuByUserId(String userid)  
  3. {  
  4. ……  
  5. }  

@Cacheable主要针对方法配置,能够根据方法的请求参数对其结果进行缓存。

value

缓存的名称,在 spring 配置文件中定义,必须指定至少一个

例如:
@Cacheable(value=” menuCache”) 或者 
@Cacheable(value={”cache1”,”cache2”}

key

缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

例如:
@Cacheable(value=” menuCache”,

key=”#userName”)

condition

缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存

例如:
@Cacheable(value=” menuCache”,

condition=”#userName.length()>2”)



@CachePut主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用。

value

缓存的名称,在 spring 配置文件中定义,必须指定至少一个

例如:
@Cacheable(value=” menuCache”) 或者 
@Cacheable(value={”cache1”,”cache2”}

key

缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

例如:
@Cacheable(value=”testcache”,

key=”#userName”)

condition

缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存

例如:
@Cacheable(value=” menuCache”,

condition=”#userName.length()>2”)

 

4.2     清除缓存

[java]  view plain  copy
  1. @CacheEvict(value="menuCache",key="'UserMenuKey'+#userRoleBean.getUserId()")  
  2. public boolean delUserRole(UserRoleBean userRoleBean) {  
  3. ……  
  4. }  

@CachEvict主要针对方法配置,能够根据一定的条件对缓存进行清空。

value

缓存的名称,在 spring 配置文件中定义,必须指定至少一个

例如:
@CachEvict(value=” menuCache”) 或者 
@CachEvict(value={”cache1”,”cache2”}

key

缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

例如:
@CachEvict(value=” menuCache”,

key=”#userName”)

condition

缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才清空缓存

例如:
@CachEvict(value=” menuCache”,
condition=”#userName.length()>2”)

allEntries

是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存

例如:
@CachEvict(value=” menuCache”,

allEntries=true)

beforeInvocation

是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存

例如:
@CachEvict(value=” menuCache”,beforeInvocation=true)




下面是需要被缓存处理的UserService.java

package com.jadyer.service;  
  
import java.util.Map;  
import java.util.concurrent.ConcurrentHashMap;  
  
import org.springframework.cache.annotation.CacheEvict;  
import org.springframework.cache.annotation.Cacheable;  
import org.springframework.stereotype.Service;  
  
/** 
 * Cacheable注解负责将方法的返回值加入到缓存中 
 * CacheEvict注解负责清除缓存(它的三个参数与@Cacheable的意思是一样的) 
 * @see ---------------------------------------------------------------------------------------------------------- 
 * @see value------缓存位置的名称,不能为空,若使用EHCache则其值为ehcache.xml中的<cache name="myCache"/> 
 * @see key--------缓存的Key,默认为空(表示使用方法的参数类型及参数值作为key),支持SpEL 
 * @see condition--只有满足条件的情况才会加入缓存,默认为空(表示全部都加入缓存),支持SpEL 
 * @see ---------------------------------------------------------------------------------------------------------- 
 * @see 该注解的源码位于spring-context-3.2.4.RELEASE-sources.jar中 
 * @see Spring针对Ehcache支持的Java源码位于spring-context-support-3.2.4.RELEASE-sources.jar中 
 * @see ---------------------------------------------------------------------------------------------------------- 
 * @create Oct 3, 2013 6:17:54 PM 
 * @author 玄玉<http://blog.youkuaiyun.com/jadyer> 
 */  
@Service  
public class UserService {  
    private Map<String, String> usersData = new ConcurrentHashMap<String, String>();  
      
    public UserService(){  
        System.out.println("用户数据初始化..开始");  
        usersData.put("2", "玄玉");  
        usersData.put("3", "我的博客:http://blog.youkuaiyun.com/jadyer");  
        System.out.println("用户数据初始化..完毕");  
    }  
      
    //将查询到的数据缓存到myCache中,并使用方法名称加上参数中的userNo作为缓存的key  
    //通常更新操作只需刷新缓存中的某个值,所以为了准确的清除特定的缓存,故定义了这个唯一的key,从而不会影响其它缓存值  
    @Cacheable(value="myCache", key="'get'+#userNo")  
    public String get(String userNo){  
        System.out.println("数据库中查到此用户号[" + userNo + "]对应的用户名为[" + usersData.get(userNo) + "]");  
        return usersData.get(userNo);  
    }  
      
    @CacheEvict(value="myCache", key="'get'+#userNo")  
    public void update(String userNo){  
        System.out.println("移除缓存中此用户号[" + userNo + "]对应的用户名[" + usersData.get(userNo) + "]的缓存");  
    }  
      
    //allEntries为true表示清除value中的全部缓存,默认为false  
    @CacheEvict(value="myCache", allEntries=true)  
    public void removeAll(){  
        System.out.println("移除缓存中的所有数据");  
    }  
}  



下面是UserController.java

package com.jadyer.controller;  
  
import javax.annotation.Resource;  
  
import org.springframework.stereotype.Controller;  
import org.springframework.ui.Model;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
  
import com.jadyer.service.UserService;  
  
/** 
 * 这里用到的jar如下 
 * aopalliance.jar 
 * commons-logging-1.1.2.jar 
 * ehcache-2.7.4.jar 
 * slf4j-api-1.7.5.jar 
 * spring-aop-3.2.4.RELEASE.jar 
 * spring-beans-3.2.4.RELEASE.jar 
 * spring-context-3.2.4.RELEASE.jar 
 * spring-context-support-3.2.4.RELEASE.jar 
 * spring-core-3.2.4.RELEASE.jar 
 * spring-expression-3.2.4.RELEASE.jar 
 * spring-web-3.2.4.RELEASE.jar 
 * spring-webmvc-3.2.4.RELEASE.jar 
 * @create Oct 3, 2013 6:22:43 PM 
 * @author 玄玉<http://blog.youkuaiyun.com/jadyer> 
 */  
@Controller  
@RequestMapping("cacheTest")  
public class UserController {  
    @Resource  
    private UserService userService;  
      
    @RequestMapping(value="/get/{userNo}", method=RequestMethod.GET)  
    public String get(@PathVariable String userNo, Model model){  
        String username = userService.get(userNo);  
        model.addAttribute("username", username);  
        return "getUser";  
    }  
      
    @RequestMapping(value="/update/{userNo}", method=RequestMethod.GET)  
    public String update(@PathVariable String userNo, Model model){  
        userService.update(userNo);  
        model.addAttribute("userNo", userNo);  
        return "updateUser";  
    }  
      
    @RequestMapping(value="/removeAll", method=RequestMethod.GET)  
    public String removeAll(){  
        userService.removeAll();  
        return "removeAllUser";  
    }  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值