Commonly used annotations in redis of SpringBoot

Table of contents

1. @Cacheable

2. @CachePut

3. @CacheEvict


1. @Cacheable

The function is mainly for method configuration, which can cache the results according to the request parameters of the method 

Description of main parameters: 

  1) value : 

  The name of the cache, defined in the spring configuration file, must specify at least one

  For example: @Cacheable(value="mycache") or @Cacheable(value={"cache1","cache2"}.

  2) key : cached key, can be empty

  If it is specified to be written according to the SpEL expression, if not specified, it will be combined according to all parameters of the method by default,

  For example: @Cacheable(value=”testcache”, key=”#userName”). 

  3) condition : The condition of the cache, can be empty

The case is as follows:

service layer

//表示使用my-redis-cache1缓存空间,key的生成策略为book+bid,当bid>10的时候才会使用缓存
    @Cacheable(value = "my-redis-cache1",key = "'book'+#bid",condition = "#bid>10")
    public Book selectByPrimaryKey(Integer bid) {
        return bookMapper.selectByPrimaryKey(bid);
    }

The test code is as follows

 @Test
    public void selectByPrimaryKey() {
        Book book = bookService.selectByPrimaryKey(18);
        System.out.println(book);
        System.out.println("---------------------------------------------");
        Book book2 = bookService.selectByPrimaryKey(18);
        System.out.println(book2);
    }

console output

When the bids are 8 and 18 respectively, bid=8 does not trigger the cache, the data is fetched from the database, and there are sql statements to print; only when bid=18, the query results are cached, and the data is fetched from the cache without sql statements Print

2. @CachePut

The function is mainly for method configuration, which can cache its results according to the request parameters of the method. Unlike @Cacheable, it will trigger a real query every time

method call 

  Description of main parameters:

  The parameter configuration is the same as @Cacheable.

3. @CacheEvict

The function is mainly for method configuration, which can clear the cache according to certain conditions

 Description of main parameters:

  1) The parameter configuration of value, key and condition is the same as that of @Cacheable.

  2) allEntries :

   Whether to clear all cache content, the default is false,

   If specified as true, all caches will be cleared immediately after the method call,

   For example: @CachEvict(value=”testcache”, allEntries=true). 

  3) beforeInvocation :

   Whether to clear before the method is executed, the default is false,

   If specified as true, the cache will be cleared before the method is executed.

   By default, if the method execution throws an exception, the cache will not be cleared,

   例如@CachEvict(value=”testcache”,beforeInvocation=true)

The case is as follows:

service layer code

 @CacheEvict(value = "my-redis-cache2",allEntries = true)
    public void clear() {
        System.out.println("清空my-redis-cache2缓存槽中的所有对象....");
    }

 The test code is as follows

public void clear() {
    bookService.clear();
}

Note : If you need to test, first cache 2 objects in the cache

That's all for today's study, bye!

Description: Learning records, if there are mistakes, please correct me, if you have any questions, welcome to comment    

Guess you like

Origin blog.csdn.net/qq_52445443/article/details/122412633