php+redis+设置前缀,spring使用Redis自定义前缀后缀名(去掉SimpleKey []+自定义)
標簽:
spring中自動加上?SimpleKey [] 解決方案
一、自定義后綴名
1、定義類實現KeyGenerator接口
MyKeyGenerator
package com.wbg.springRedis.service.impl;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Component("myKeyGenerator")
public class MyKeyGenerator implements KeyGenerator {
@Override
public Object generate(Object target, Method method, Object... params) {
//返回后綴名
//return method.getName();
//注意,這里不能返回null,否則會報錯
//java.lang.IllegalArgumentException:
// Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public java.util.List com.wbg.springRedis.service.impl.RoleServiceImpl.listAll()] caches=[listAll] | key='' | keyGenerator='myKeyGenerator' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
return "";
}
}
2、注解使用:
@Cacheable(value = "listAll", keyGenerator = "myKeyGenerator")
已經解決
二、自定義前綴:
@Bean
RedisCacheManager cacheManager() {
RedisCacheConfiguration configuration = RedisCacheConfiguration
.defaultCacheConfig()
.computePrefixWith(cacheName -> "redis" + cacheName)
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.string()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.java()));
return RedisCacheManager.builder(redisConnectionFactory()).cacheDefaults(configuration).build();
}
標簽:
來源: https://www.cnblogs.com/weibanggang/p/10191028.html
總結
以上是生活随笔為你收集整理的php+redis+设置前缀,spring使用Redis自定义前缀后缀名(去掉SimpleKey []+自定义)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线程基础知识_线程生命周期_从JVM内存
- 下一篇: 计算机与环境科学,计算机在环境科学与工程