springmvc的缓存注解

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


image.png

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

1.概念

1.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")

2.CachePut

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

3.CacheEvict

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

2.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>

3.使用

@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月前
|
缓存 NoSQL Redis
通过切面结合Redis自定义缓存注解
通过切面结合Redis自定义缓存注解
21 0
|
1月前
|
存储 缓存 Java
【Spring原理高级进阶】有Redis为啥不用?深入剖析 Spring Cache:缓存的工作原理、缓存注解的使用方法与最佳实践
【Spring原理高级进阶】有Redis为啥不用?深入剖析 Spring Cache:缓存的工作原理、缓存注解的使用方法与最佳实践
|
1月前
|
XML 存储 缓存
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache管理器的实战开发指南(修正篇)
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache管理器的实战开发指南(修正篇)
48 0
|
2天前
|
缓存 NoSQL Java
在 Spring Boot 应用中使用 Spring Cache 和 Redis 实现数据查询的缓存功能
在 Spring Boot 应用中使用 Spring Cache 和 Redis 实现数据查询的缓存功能
11 0
|
1月前
|
存储 XML 缓存
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南(一)
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南
216 0
|
4天前
|
缓存 NoSQL Java
在 SSM 架构(Spring + SpringMVC + MyBatis)中,可以通过 Spring 的注解式缓存来实现 Redis 缓存功能
【6月更文挑战第18天】在SSM(Spring+SpringMVC+MyBatis)中集成Redis缓存,涉及以下步骤:添加Spring Boot的`spring-boot-starter-data-redis`依赖;配置Redis连接池(如JedisPoolConfig)和连接工厂;在Service层使用`@Cacheable`注解标记缓存方法,指定缓存名和键生成策略;最后,在主配置类启用缓存注解。通过这些步骤,可以利用Spring的注解实现Redis缓存。
23 2
|
29天前
|
缓存 NoSQL Java
Spring Cache之本地缓存注解@Cacheable,@CachePut,@CacheEvict使用
SpringCache不支持灵活的缓存时间和集群,适合数据量小的单机服务或对一致性要求不高的场景。`@EnableCaching`启用缓存。`@Cacheable`用于缓存方法返回值,`value`指定缓存名称,`key`定义缓存键,可按SpEL编写,`unless`决定是否不缓存空值。当在类上使用时,类内所有方法都支持缓存。`@CachePut`每次执行方法后都会更新缓存,而`@CacheEvict`用于清除缓存,支持按键清除或全部清除。Spring Cache结合Redis可支持集群环境。
93 6
|
1月前
|
缓存 Java 数据库
优化您的Spring应用程序:缓存注解的精要指南
优化您的Spring应用程序:缓存注解的精要指南
63 0
|
12天前
|
缓存 NoSQL Java
Java一分钟之-Spring Data Redis:使用Redis做缓存
【6月更文挑战第10天】Spring Data Redis是Spring框架的一部分,简化了Java应用与Redis的集成,支持多种数据结构操作。本文介绍了其基本使用,包括添加依赖、配置Redis连接及使用RedisTemplate。还讨论了常见问题,如序列化、缓存穿透和雪崩,并提供解决方案。通过实战示例展示了缓存与数据库读写分离的实现,强调了Spring Data Redis在提升系统性能中的作用。
41 0
|
1月前
|
缓存 NoSQL Java
缓存框架-Spring Cache的使用
Spring Cache是一个注解驱动的缓存框架,它提供了一层抽象,允许切换不同的缓存实现,如EHCache、Caffeine和Redis。启用缓存只需在配置中引入相关依赖并开启`@EnableCaching`。`@Cacheable`用于方法执行前检查缓存,存在则直接返回,不存在则执行方法并将结果存入缓存。`@CachePut`在方法执行后将结果放入缓存,常用于更新操作。`@CacheEvict`用于清除缓存数据,可以按key删除或清空整个缓存。`@Caching`可以组合多个缓存操作。在Redis中,可以通过序列化处理存储复杂对象,提高可读性。
72 4