Spring缓存

Spring从3.1开始支持缓存功能。缓存是以键值对的方式实现的。使用注解实现缓存,常用的有三个注解:@Cacheable、@CacheEvict、@CachePut
开启注解需要在Spring配置文件中添加如下配置:

<!--开启注解缓存-->
<cache:annotation-driven cache-manager="cacheManager" />
<!--缓存控制器-->
<bean id="cacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager"/>

准备测试实体类

public class User {
private Integer id;
private String name;
//getter/setter 和构造方法略 }

1. @Cacheable

@Cacheable可以用在方法上,也可以用在类上。用在方法上,表示这个方法支持缓存。应在类上,表示这个类中所有的方法都支持缓存。

public class UserCache {
    //这个方法不支持缓存
    public User getUserById(Integer id) {
        System.out.println("getUserById=" + id);
        return new User(id, "");
    }
    //支持缓存,缓存的名字叫userCache,缓存中的key是动态传入的name值
    @Cacheable(cacheNames="userCache",key="#name")
    public User getUserByName(String name) {
        System.out.println("getUserByName=" + name);
        return new User(null, name);
    }
}

在配置文件中配置bean

<bean id="userCache" class="cache.UserCache"></bean>

测试类:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-cache.xml");
UserCache userCache = applicationContext.getBean(UserCache.class);
userCache.getUserById(1);
userCache.getUserById(1);//进入方法,因为没加缓存
userCache.getUserByName("a");//进入方法,并缓存返回值
userCache.getUserByName("a");//这句不会进入方法,直接用的缓存
userCache.getUserByName("b");
CacheManager cm =  (CacheManager) applicationContext.getBean("cacheManager");
Cache cache1 = cm.getCache("userCache");//拿到缓存
System.out.println( cache1.get("b").get());//从缓存中获取数据

key属性用来定义Spring缓存方法的返回结果对应的key。key可以使用#参数名或者#p参数index,如下面的例子,#p0,#p代表参数,0是索引

@Cacheable(cacheNames=“userCache”,key=“#p0”) public User
getUserByName(String name) {}

或者

@Cacheable(cacheNames=“userCache”,key=“#name”) public User
getUserByName(String name) {}

key支持SpEL表达式: 比如,下面的例子用方法名+参数值作为key:

@Cacheable(cacheNames=“userCache”,key=“#root.methodName+#p0”) public
User getUserByName(String name) {}

使用condition属性可以指定缓存触发的条件,如当个id是偶数的时候加入缓存:

@Cacheable(cacheNames=“userCache”,key=“#root.methodName”,condition =
“#id%2==0”) public User getUserById(Integer id) {}

2. @CachePut

@CachePut的用法和@Cacheable一样。区别在于使用@Cacheable标注的方法,Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。而使用@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。

3. @CacheEvict

@CacheEvict用于标注清除缓存元素的方法或类。其中cacheNames、key、condition属性同`@Cacheable用法相同。

@CacheEvict(cacheNames = “userCache”, key = “‘getUserById’+#id”)
public void delete(Integer id) {
System.out.println(“删除”); }

另外@CacheEvict有一个allEntries属性,默认为false。当设置为true的时候,Spring会忽略掉指定的key,清除cacheNames里所有的元素。
@CacheEvict 的清除是在注解的方法执行之后清除的。即上面的delete方法执行完之后再清除缓存。通过beforeInvocation可以改变清除缓存的时机。把beforeInvocation设置成true,Spring会在执行方法之前清除缓存。

4. @Caching

@Caching注解用于在一个方法或类上同时定义多个Spring Cache,它有三个属性, cacheable
对应@Cacheable、evict对应@CacheEvict、put对应@CachePut

@Caching(cacheable = @Cacheable(“users”), evict =
{@CacheEvict(“cache2”),
@CacheEvict(value = “cache3”, allEntries = true)}) public void add() { }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专治八阿哥的孟老师

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值