Springboot uses the @Cacheable annotation to implement data caching

        This article describes how to implement data caching in Springboot through the @Cacheable annotation. Every time a method annotated with @Cacheable (a method requiring caching) is called, Spring will check whether the specified target method of the specified parameter has already been called, and if so, directly obtain the result of the method call from the cache, if If not, call the method and cache the result and return it to the user. The next call is fetched directly from the cache.

1. Add @EnableCaching

        Use  @EnableCaching the flag to enable annotation-based caching on SpringBoot's main startup class.

@EnableCaching
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application .class, args);
    }
}

2、添加@Cacheable

  在需要缓存的方法上添加@Cacheable annotation. In the future, the same data will be queried directly from the cache without calling the method.

@Cacheable(value = "areaTreeData")
public CommonResult<List<Map<String, Object>>> queryTreeData(Long pId, Long lv) {
  Map<String, Object> map = new HashMap<>();
  map.put("state", 1);
  List<Map<String, Object>> list = getTreeData(map, pId, lv);
  return new CommonResult<>(list);
}

注意:

1、返回的数据类型必须支持序列化或实现了Serializable interface, otherwise the data cannot be cached.

2. Only by calling this method directly can it be cached, and it cannot be called by other methods in the class.

3、常用属性说明

  • cacheNames/value : Used to specify the name of the cache component

  • key : The key used when caching data, which can be used to specify. The default is to use the value of the method parameter. (This key you can use spEL expression to write)

  • keyGenerator : key generator. key and keyGenerator choose one of them

  • cacheManager : Can be used to specify the cache manager. Which cache manager to get the cache from.

  • condition : It can be used to specify that the condition is met before caching

  • unless : Negate caching. When the condition specified by unless is true, the return value of the method will not be cached. Of course, you can also obtain the results for judgment. (via  #result get method result)

  • sync : Whether to use asynchronous mode.

4. @CacheEvict annotation

        The role of @CachEvict is mainly for method configuration, which can clear the cache according to certain conditions. Common attribute parameters are as follows:

parameter explain example
value The name of the cache, defined in the spring configuration file, must specify at least one @CacheEvict(value=”my cache”)
key The key of the cache, which can be empty. If specified, it should be written according to the SpEL expression. If not specified, it will be combined according to all parameters of the method by default. @CacheEvict(value=”testcache”,key=”#userName”)
condition Cache conditions, can be empty, written in SpEL, return true or false, only true to cache @CacheEvict(value=”testcache”,condition=”#userName.length()>2”)
allEntries Whether to clear all cache contents, the default is false, if specified as true, all caches will be cleared immediately after the method call @CachEvict(value=”testcache”,allEntries=true)
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)
@CacheEvict(value = "areaTreeData", allEntries = true, beforeInvocation = true)
public Integer save(SysArea sysArea) {
   return mapper.insertSelective(sysArea);
}

New Era Migrant Workers 

Guess you like

Origin blog.csdn.net/sg_knight/article/details/128117348