@Add cache with Cacheable annotation

If you add, you have Clear cache.

@Cacheable cannot set the expiration time directly. It should be used in combination with @ CacheConfig to set the expiration time

Function description

@The Cacheable annotation is used on a method to indicate that the return result of the method can be cached. That is, the returned result of this method will be put in the cache, so that when the method is called with the same parameters in the future, the value in the cache will be returned instead of actually executing the method.

Note that one thing is emphasized here: the parameters are the same. This should be easy to understand, because the cache does not care about the execution logic of the method. What it can determine is that for the same method, if the parameters are the same, the returned results are the same. However, if the parameters are different, the cache can only assume that the results are different. Therefore, for the same method, how many parameter combinations are used during the operation of your program, and how many cached keys will be generated in theory (of course, these combined parameters refer to those related to the generation of keys). Let's learn about some parameters of @ Cacheable:

Attribute description

value: cache location name, which cannot be empty. If EHCache is used, it is EHCache The name of the cache declared in XML
Key: the cached key, which is empty by default. It means that the parameter type and parameter value of the method are used as the key, and SpEL is supported
Condition: trigger condition. It will be added to the cache only when the conditions are met. The default value is empty, which means that all are added to the cache and support spiel

Properties of the cache name

@Cacheable provides two parameters to specify the cache name: value (can be omitted and not written by default) and cacheNames.

@Override
@Cacheable("menu")
public Menu findById(String id) {
    Menu menu = this.getById(id);
    if (menu != null){
        System.out.println("menu.name = " + menu.getName());
    }
    return menu;
}

In this example, the findById method is associated with a cache named menu. When calling this method, the menu cache will be checked. If there are results in the cache, the method will not be executed.

Extension:

In fact, according to the official document, @ Cacheable supports associating multiple caches with the same method. In this case, each cache of these associations will be checked before the method is executed, and as long as at least one cache hits, the value in the cache will be returned.
@Cacheable({"menu", "menuById"})

Distinguishing properties of methods with the same name

A cache name corresponds to an annotated method, but a method may pass in different parameters, and the results will be different.
For example:
Use @ Cacheable("menu") because my parameter values are different. For example, if the first incoming age is 11, there will be a cache called menu. If the second incoming age is 22, there will also be a cache called menu. How to distinguish? Use the key attribute to distinguish.

Specific use

@Cacheable(value = {"menuById"}, key = "#id")
The cache results are stored in the cache as a key value pair, and the key in the annotation is the key of the key value pair; Value is the result returned by the method
Format: # add the parameter name in the method

@Cacheable(value="andCache",key="#userId + 'findById'")  
public SystemUser findById(String userId) {  
    SystemUser user = (SystemUser) dao.findById(SystemUser.class, userId);        
    return user ;         
}  

@Override
    @Cacheable(value = {"menuById"}, key = "#id")
    public Menu findById(String id) {
        Menu menu = this.getById(id);
        if (menu != null){
            System.out.println("menu.name = " + menu.getName());
        }
        return menu;
    }

    @Override
    @Cacheable(value = {"menuById"}, key = "'id-' + #menu.id")
    public Menu findById(Menu menu) {
        return menu;
    }

    @Override
    @Cacheable(value = {"menuById"}, key = "'hash' + #menu.hashCode()")
    public Menu findByHash(Menu menu) {
        return menu;
    }

```

### Trigger condition properties

condition: Trigger condition: it will be added to the cache only if the conditions are met. The default value is empty, which means that all items are added to the cache. It is supported SpEL

```

//Save the cache in andCache and save it only when the length of the parameter userId is less than 32. By default, the parameter value and type are used as the cache key  
@Cacheable(value="andCache",condition="#userId.length < 32")  
public boolean isReserved(String userId) {  
    System.out.println("hello andCache"+userId);  
    return false;  
}
```


Keywords: Java Redis Spring Boot Cache

Added by miz_luvly@hotmail.com on Mon, 03 Jan 2022 07:45:15 +0200