spring cache注解@Cacheable参数key赋值

1 参数

@Cacheable(key ="#id")

一般这里会是 id ,但是可能与其它 entity的id重复。

2 前缀加参数

@Cacheable(key = "'com:test:'+#id")

但是给 key 赋值 会导致代码不够优雅,因为 缓存、更新、失效 都要给key赋值。
可以定义全局变量解决此问题
public static final String KEY_NAME = "'com:test:'";
@Cacheable(key = KEY_NAME +"+#id")

public class Constant {
    public static final String yky_auth_user_role_CacheName = "yky-auth:cache:userRole:";
    public static final String yky_auth_permission_CacheName = "yky-auth:cache:permission:";
    
    public static final String prefix_yky_auth_user_role_CacheName = "'yky-auth:cache:userRole:'";
    public static final String prefix_yky_auth_permission_CacheName = "'yky-auth:cache:permission:'";

}
 

@Cacheable(value=Constant.yky_auth_user_role_CacheName,key=Constant.prefix_yky_auth_user_role_CacheName+"+#root.targetClass.name +\":\"+ #root.methodName+#userId")
    @Override
    public List<SysRole> getRoleByUserId(String userId) {
        
         return userRepository.getRoleByUserId(userId);
    }

3 自定义keyGenerator

@Cacheable(keyGenerator="keyGenerator")

@Override
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName()).append(":");
                sb.append(method.getName()).append(":");
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }

4 指定类名和方法名

@Cacheable(key ="#root.targetClass.name +\":\"+ #root.methodName")

一般这里会是 id ,但是可能与其它 entity的id重复。

@Cacheable(value=Constant.yky_auth_permission_CacheName,key=Constant.prefix_yky_auth_permission_CacheName+"+#root.targetClass.name +\":\"+ #root.methodName")
    @Override
    public List<PermissionAndRole> getAll() {
        
        
        return permissionAndRoleRepository.getAll();
    }

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cache中,@Cacheable注解是用来标记一个方法的返回值可以被缓存起来以提高性能。其中,sync参数用于指定是否启用同步模式,默认值为false,表示不启用同步。当sync参数设置为true时,意味着在缓存方法执行期间,其他请求将会被阻塞,直到缓存方法执行完成才会返回结果。而当sync参数设置为false时,存方法的多个请求可以并发执行,不会相互阻塞。所以,如果你想要在缓存方法执行期间阻塞其他请求,可以将sync参数设置为true。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MyBatis二级缓存+spring cache @Cacheable、@CacheConfig](https://blog.csdn.net/qq_37337660/article/details/120188742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Spring cache @Cacheable的使用](https://blog.csdn.net/weixin_43759039/article/details/128666454)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值