设为首页 加入收藏

TOP

Spring使用Cache(二)
2015-11-12 23:05:54 来源: 作者: 【 】 浏览:38
Tags:Spring 使用 Cache
ntries是boolean类型,表示是否需要清除缓存中的所有元素。默认为false,表示不需要。当指定了allEntries为true时,Spring Cache将忽略指定的key。有的时候我们需要Cache一下清除所有的元素,这比一个一个清除元素更有效率。


? @CacheEvict(value="users", allEntries=true)


? public?void delete(Integer id) {


? ? ? System.out.println("delete user by id: " + id);


? }


? ? ? 清除操作默认是在对应方法成功执行之后触发的,即方法如果因为抛出异常而未能成功返回时也不会触发清除操作。使用beforeInvocation可以改变触发清除操作的时间,当我们指定该属性值为true时,Spring会在调用该方法之前清除缓存中的指定元素。


? @CacheEvict(value="users", beforeInvocation=true)


? public?void delete(Integer id) {


? ? ? System.out.println("delete user by id: " + id);


? }


? ? ? 其实除了使用@CacheEvict清除缓存元素外,当我们使用Ehcache作为实现时,我们也可以配置Ehcache自身的驱除策略,其是通过Ehcache的配置文件来指定的。由于Ehcache不是本文描述的重点,这里就不多赘述了,想了解更多关于Ehcache的信息,请查看我关于Ehcache的专栏。


? ? ? @Caching注解可以让我们在一个方法或者类上同时指定多个Spring Cache相关的注解。其拥有三个属性:cacheable、put和evict,分别用于指定@Cacheable、@CachePut和@CacheEvict。


? @Caching(cacheable = @Cacheable("users"), evict = { @CacheEvict("cache2"),


? ? ? ? @CacheEvict(value = "cache3", allEntries = true) })


? public User find(Integer id) {


? ? ? returnnull;


? }


? ? ? Spring允许我们在配置可缓存的方法时使用自定义的注解,前提是自定义的注解上必须使用对应的注解进行标注。如我们有如下这么一个使用@Cacheable进行标注的自定义注解。


@Target({ElementType.TYPE, ElementType.METHOD})


@Retention(RetentionPolicy.RUNTIME)


@Cacheable(value="users")


public?@interface?MyCacheable {


}


? ? ? 那么在我们需要缓存的方法上使用@MyCacheable进行标注也可以达到同样的效果。


? @MyCacheable


? public User findById(Integer id) {


? ? ? System.out.println("find user by id: " + id);


? ? ? User user = new User();


? ? ? user.setId(id);


? ? ? user.setName("Name" + id);


? ? ? return user;


? }


? ? ? 配置Spring对基于注解的Cache的支持,首先我们需要在Spring的配置文件中引入cache命名空间,其次通过就可以启用Spring对基于注解的Cache的支持。


"1.0" encoding="UTF-8"?>


"http://www.springframework.org/schema/beans"


? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


? xmlns:cache="http://www.springframework.org/schema/cache"


? xsi:schemaLocation="http://www.springframework.org/schema/beans


? ? http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


? ? http://www.springframework.org/schema/cache


? ? http://www.springframework.org/schema/cache/spring-cache.xsd">


?



? ? ? 有一个cache-manager属性用来指定当前所使用的CacheManager对应的bean的名称,默认是cacheManager,所以当我们的CacheManager的id为cacheManager时我们可以不指定该参数,否则就需要我们指定了。


? ? ? 还可以指定一个mode属性,可选值有proxy和aspectj。默认是使用proxy。当mode为proxy时,只有缓存方法在外部被调用的时候Spring Cache才会发生作用,这也就意味着如果一个缓存方法在其声明对象内部被调用时Spring Cache是不会发生作用的。而mode为aspectj时就不会有这种问题。另外使用proxy时,只有public方法上的@Cacheable等标注才会起作用,如果需要非public方法上的方法也可以使用Spring Cache时把mode设置为aspectj。


? ? ? 此外,还可以指定一个proxy-target-class属性,表示是否要代理class,默认为false。我们前面提到的@Cacheable、@cacheEvict等也可以标注在接口上,这对于基于接口的代理来说是没有什么问题的,但是需要注意的是当我们设置proxy-target-class为true或者mode为aspectj时,是直接基于class进行操作的,定义在接口上的@Cacheable等Cache注解不会被识别到,那对应的Spring Cache也不会起作用了。


? ? ? 需要注意的是只会去寻找定义在同一个ApplicationContext下的@Cacheable等缓存注解。


? ? ? 除了使用注解来声明对Cache的支持外,Spring还支持使用XML来声明对Cache的支持。这主要是通过类似于aop:advice的cache:advice来进行的。在cache命名空间下定义了一个cache:advice元素用来定义一个对于Cache的advice。其需要指定一个cache-manager属性,默认为cacheManager。cache:advice下面可以指定多个cache:caching元素,其有点类似于使用注解时的@Caching注解。cache:caching元素下又可以指定cache:cacheable、cache:cache-put和cache:cache-evict元素,它们类似于使用注解时的@Cacheable、@CachePut和@CacheEvict。下面来看一个示例:


? "cacheAdvice" cache-manager="cacheManager">


? ? ? "users">


? ? ? ? "findById" key="#p0"/>


? ? ? ? "

首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Spring AOP自定义注解方式实现日.. 下一篇如何根据你想要的生活选择第一门..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: