springmvc实战技巧解析(五)使用spring缓存注解

举报
小鲍侃java 发表于 2021/09/11 00:04:30 2021/09/11
【摘要】 Spring为我们提供了几个注解来支持Spring Cache。其核心主要是@Cacheable和@CacheEvict。使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回结果...

Spring为我们提供了几个注解来支持Spring Cache。其核心主要是@Cacheable和@CacheEvict。使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回结果,而使用@CacheEvict标记的方法会在方法执行前或者执行后移除缓存。

Cacheable


   可以标记在一个方法上,也可以标记在一个类上。当标记在一个方法上时表示该方法是支持缓存的,当标记在一个类上时则表示该类所有的方法都是支持缓存的。对于一个支持缓存的方法,Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。Spring在缓存方法的返回值时是以键值对进行缓存的,值就是方法的返回结果。@Cacheable可以指定三个属性,value、key和condition。
value属性指定Cache名称,key属性是用来指定Spring缓存方法的返回结果时对应的key的。该属性支持SpringEL表达式。 condition属性指定发生的条件

    @Cacheable(value="users", key="#id")
       public User find(Integer id) {
          returnnull;
       }
    @Cacheable(value="users", key="#p0.id")
       public User find(User user) {
          returnnull;
       }
    @Cacheable(value={"users"}, key="#user.id", condition="#user.id%2==0")

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

CachePut


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

CacheEvict


   @CacheEvict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。@CacheEvict可以指定的属性有value、key、condition。其中value、key和condition的语义与@Cacheable对应的属性类似。即value表示清除操作是发生在哪些Cache上的;key表示需要清除的是哪个key。

Spring配置文件中加入缓存配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/cache
	http://www.springframework.org/schema/cache/spring-cache.xsd">
	<cache:annotation-driven/>
	 
   <bean id="cacheManager"class="org.springframework.cache.support.SimpleCacheManager">  
        <property name="caches">  
            <set>            
                   <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">  
                    <property name="name" value="default"/>  
                   </bean>     
                   <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">  
                    <property name="name" value="cacheDemo"/>  指定缓存名
                   </bean>  
            </set>  
        </property>  
    </bean>    
</beans>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

实现

@Service
public class CachedemoService { 与配置文件中设置的缓存名一致
	@Cacheable(value = "cacheDemo", key = "'cacheDemo'+#id")
	public Object getData(String id) {
		return null;
	}
	@CachePut(value = "cacheDemo", key = "'cacheDemo'+#id")
	public Object putData(String id, Object obj) {
		return obj;
	}
	@CacheEvict(value = "cacheDemo", key = "'cacheDemo'+#id")
	public void delData(String id) {
	}
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

文章来源: baocl.blog.csdn.net,作者:小黄鸡1992,版权归原作者所有,如需转载,请联系作者。

原文链接:baocl.blog.csdn.net/article/details/83107188

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。