find" key="#user.id"/>
? ? ? ? "deleteAll" all-entries="true"/>
? ? ?
?
? ? ? 上面配置定义了一个名为cacheAdvice的cache:advice,其中指定了将缓存findById方法和find方法到名为users的缓存中。这里的方法还可以使用通配符“*”,比如“find*”表示任何以“find”开始的方法。
? ? ? 有了cache:advice之后,我们还需要引入aop命名空间,然后通过aop:config指定定义好的cacheAdvice要应用在哪些pointcut上。如:
? "false">
? ? ? "cacheAdvice" pointcut="execution(* com.xxx.UserService.*(..))"/>
?
? ? ? 上面的配置表示在调用com.xxx.UserService中任意公共方法时将使用cacheAdvice对应的cache:advice来进行Spring Cache处理。更多关于Spring Aop的内容不在本文讨论范畴内。
? ? ? CacheManager是Spring定义的一个用来管理Cache的接口。Spring自身已经为我们提供了两种CacheManager的实现,一种是基于Java API的ConcurrentMap,另一种是基于第三方Cache实现——Ehcache,如果我们需要使用其它类型的缓存时,我们可以自己来实现Spring的CacheManager接口或AbstractCacheManager抽象类。下面分别来看看Spring已经为我们实现好了的两种CacheManager的配置示例。
? "cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
? ? ? "caches">
? ? ? ?
? ? ? ? ? ? "org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="xxx"/>
? ? ? ?
? ? ?
?
? ? ? 上面的配置使用的是一个SimpleCacheManager,其中包含一个名为“xxx”的ConcurrentMapCache。
?
? "cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcacheManager"/>
? "ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="ehcache-spring.xml"/>
? ? ? 上面的配置使用了一个Spring提供的EhCacheCacheManager来生成一个Spring的CacheManager,其接收一个Ehcache的CacheManager,因为真正用来存入缓存数据的还是Ehcache。Ehcache的CacheManager是通过Spring提供的EhCacheManagerFactoryBean来生成的,其可以通过指定ehcache的配置文件位置来生成一个Ehcache的CacheManager。若未指定则将按照Ehcache的默认规则取classpath根路径下的ehcache.xml文件,若该文件也不存在,则获取Ehcache对应jar包中的ehcache-failsafe.xml文件作为配置文件。更多关于Ehcache的内容这里就不多说了,它不属于本文讨论的内容,欲了解更多关于Ehcache的内容可以参考我之前发布的Ehcache系列文章,也可以参考官方文档等。
? ? ? 键的生成策略有两种,一种是默认策略,一种是自定义策略。
? ? ? 默认的key生成策略是通过KeyGenerator生成的,其默认策略如下:
? ? ? 如果我们需要指定自己的默认策略的话,那么我们可以实现自己的KeyGenerator,然后指定我们的Spring Cache使用的KeyGenerator为我们自己定义的KeyGenerator。
? ? ? 使用基于注解的配置时是通过cache:annotation-driven指定的.
? "userKeyGenerator"/>?
? "userKeyGenerator" class="com.xxx.cache.UserKeyGenerator"/>?
? ? ? 而使用基于XML配置时是通过cache:advice来指定的。
? "cacheAdvice" cache-manager="cacheManager" key-generator="userKeyGenerator">
?
?
? ? ? 需要注意的是此时我们所有的Cache使用的Key的默认生成策略都是同一个KeyGenerator。
? ? ? 自定义策略是指我们可以通过Spring的EL表达式来指定我们的key。这里的EL表达式可以使用方法参数及它们对应的属性。使用方法参数时我们可以直接使用“#参数名”或者“#p参数index”。下面是几个使用参数作为key的示例。
? @Cacheable(value="users", key="#id")
? public User find(Integer id) {
? ? ? returnnull;
? }?
? @Cacheable(value="users", key="#p0")
? public User find(Integer id) {
? ? ? returnnull;
? }?
? @Cacheable(value="users", key="#user.id")
? public User find(User user) {
? ? ? returnnull;
? }?
? @Cacheable(value="users", key="#p0.id")
? public User find(User user) {
? ? ? returnnull;
? }?
? ? ? 除了上述使用方法参数作为key之外,Spring还为我们提供了一个root对象可以用来生成key。通过该root对象我们可以获取到以下信息。
? ? ? ? 当我们要使用root对象的属性作为key时我们也可以将“#root”省略,因为Spring默认使用的就是root对象的属性。如:
? @Cacheable(value={"users", "xxx"}, key="caches[1].name")
? public User find(User user) {
? ? ? returnnull;
? }?
? ? ? 前面介绍的内容是Spring内置的对Cache的支持,其实我们也可以通过Spring自己单独的使用Ehcache的CacheManager或Ehcache对象。通过在Application Context中配置EhCacheManagerFactoryBean和EhCacheFactoryBean,我们就可以把对应的EhCache的CacheManager和Ehcache对象注入到其它的Spring bean对象中进行使用。?
? ? EhCacheManagerFactoryBean是Spring内置的一个可以产生Ehc