Spring cache缓存的使用

引入依赖:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>

缓存注解

  1. Cache:
    缓存接口,定义缓存,实现包括:RedisCache,EhCacheCache,ConcurrentMapCache
  2. CacheManager:
    缓存管理器,管理各种缓存(Cache)组件
  3. @Cacheable:
    针对方法配置,能够根据方法的请求参数,对其结果进行缓存。
  4. @CacheEvict:
    清空缓存
//       @CacheEvict:缓存清除
//       key:指定要清除的数据
//       allEntries = true : 指定清除这个缓存中的所有数据
//       beforeInvocation=fales: 缓存的清除是否在方法之前执行
//       默认代表缓存清除操作是在方法执行之后执行;如果出现异常缓存就不会清除
//       beforeInvocation=true  代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除

    @CacheEvict(beforeInvocation = true)

  1. @CachePut:
    保证方法被调用,又希望结果被缓存。既调用方法,又更新缓存数据;同步更新缓存,修改了数据库的某个数据,同时更新缓存
    // 运行:
    // 1.先调用目标方法
    // 2.将目标方法的结果缓存起来
  2. @EnableCacheing:
    开启基于缓存的注解
  3. keyGenerator:
    缓存数据时key生成策略
  4. serialize:
    缓存数据时value序列化策略
/**
     *   @Caching是 @Cacheable、@CachePut、@CacheEvict注解的组合
     *   以下注解的含义:
     *   1.当使用指定名字查询数据库后,数据保存到缓存
     *   2.现在使用id、age就会直接查询缓存,而不是查询数据库
     */
    @Caching(
            cacheable = {
    
    @Cacheable(value = "person",key="#name")},
            put={
    
     @CachePut(value = "person",key = "#result.id"),
                  @CachePut(value = "person",key = "#result.age")
                }
    )


@Cacheable,@CachePut,@CacheEvict主要的配置参数:
  1. @Cacheable(value = {“user”})
    value: 缓存的名称,在spring配置文件中的定义,必须指定至少一个。
  2. @Cacheable(value = {“user”},key = “#user”)
    key: 缓存的key ,非必填,指定需按照SpEL表达式编写, 不指定,则缺省按照方法的所有参数进行组合
  3. @Cacheable(value = {“user”},condition = “#username.length()>8”)
    condition: 缓存的条件,可以为空, 可以为SpEL编写,返回true/false,在调用方法之前之后都螚判断,只有为true 才能进行缓存/清空缓存。
  4. @CacheEvict(value = “user”,allEntries = true)
    allEntries: 是否清空缓存,默认为false, 指定为true, 方法调用后,立即清空所有缓存。
  5. @CacheEvict(value = “user”,beforeInvocation = true)
    beforeInvocation: 是否在方法执行前清空,默认false, 指定为true 在方法还没执行的时候就清空缓存。 默认情况下,若方法执行抛出异常,则不会清空缓存。
  6. @Cacheable(value = “user”,unless = “#user==null”)
    @CachePut(value = “user”,unless = “#user= =null”)

    unless: 用于否决缓存,不像 condition
    该表达式只在方法执行之后判断,此时可以拿到返回值result 进行判断, 条件为true 不会缓存,false 才会缓存。

主类:
开启缓存注解

@SpringBootApplication
@EnableCaching
@MapperScan(basePackages = "com.feifan.mapper")
public class App 
{
    
    

    public static void main( String[] args )
    {
    
    
        SpringApplication.run(App.class);
    }
}

Service层

@Service
public class UserServiceImpl implements IUserService{
    
    

    @Autowired
    private UserDao userDao;

    @Override
    public UserDO findOne(int id) {
    
    
        return userDao.findOne(id);
    }

    @Override
    @Cacheable(cacheNames = {
    
    "find"})
    public List<UserDO> findList(List<Integer> ids) {
    
    
        System.err.println(">>>");
        return userDao.findList(ids);
    }
}

存入缓存,后面不进行查库操作。
在这里插入图片描述


@Cacheables 能够根据方法的请求参数对其结果进行缓存。
cacheNames 缓存的名称,也就是在缓存中有一个叫emp的名字来标识不同的缓存组件。

猜你喜欢

转载自blog.csdn.net/qq_16183731/article/details/101558866
今日推荐