缓存(六)key的生成策略


1.默认使用keyGenerator生成:默认使用simpleKeyGennerator生成key:
       simpleKeyGennerator默认如果没有参数:key = new SimpleKey()
                                                          一个参数:key = 参数值    
                                                        多个参数:key = new SimpleKey(params)    

2.可以用SpEL表达式去写key,可以实现动态拼接key,key="#root.methodName+'['+#参数属性名+']'"会被拼接为方法名【参数】。
 
3.也可以自定义一个key生成器
 
package com.config;

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;
import java.util.Arrays;

@Configuration
public class MyCacheConfig {


    @Bean(name = "myKeyGenerator")
    public KeyGenerator keyGenerator(){
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                return method.getName()+"["+Arrays.asList(params).toString() +"]";
            }
        };
    }
}
调用的时候:
    @Cacheable(cacheNames = "users",keyGenerator = "myKeyGenerator")

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM