Implementation of @ CacheEvict Cleaning Multiple key
- 2021-08-17 00:04:03
- OfStack
Implement with @ Caching
Entry parameters are of basic types:
@Caching(evict={@CacheEvict(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_NAME + "'+#roleId"),
@CacheEvict(value = Cache.CONSTANT, key = "'" + CacheKey.ROLES_NAME + "'+#roleId"),
@CacheEvict(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_NAME + "'+#roleId")})
public ResponseData remove(@RequestParam Long roleId) {
............
}
The entry parameter is the object's:
@Caching(evict={@CacheEvict(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_NAME + "'+#roleDto.roleId"),
@CacheEvict(value = Cache.CONSTANT, key = "'" + CacheKey.ROLES_NAME + "'+#roleDto.roleId"),
@CacheEvict(value = Cache.CONSTANT, key = "'" + CacheKey.SINGLE_ROLE_NAME + "'+#roleDto.roleId")})
public ResponseData edit(RoleDto roleDto) {
this.roleService.editRole(roleDto);
return SUCCESS_TIP;
}
Added: @ CacheEvict Fuzzy Delete Cache Issue-RedisTemplate Fuzzy key Delete Cache
Cache management uses annotation form, which is very convenient for us to use. However, today I want to use @ CacheEvict's real and key for fuzzy deletion, and found that @ CacheEvict doesn't seem to support such operation, and then remembered that RedisTemplate seems to support fuzzy matching of key.
So, the method has
Since @ CacheEvict can't do it, use RedisTemplate
Step 1:
Get key
Here key is: user: role *
Set<Object,Object> keys = redisTemplate.keys( key );
Step 2:
Judge that keys is not empty, and then delete it
if (CollectionUtils.isNotEmpty(keys)) {
redisTemplate.delete(keys);
}
Summary:
When deleting the cache, if keys is empty, there is no response, because looking at the source code, redisTemplate helps you judge, and if it is empty, return
It may not be deleted here, which is probably the serialization problem of key