当前位置:   article > 正文

php+redis+设置前缀,spring使用Redis自定义前缀后缀名(去掉SimpleKey []+自定义)

rediscacheconfiguration simplekey

标签:

spring中自动加上 SimpleKey [] 解决方案

13377e64950159ffe85e6809d094acf2.png

一、自定义后缀名

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")

58c1fe92ddec997f911169fced795578.png

已经解决

7509d7b6486a26ad595a063b80b2a412.png

二、自定义前缀:

@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();

}

549384896ccfb1556d310440560ef241.png

标签:

来源: https://www.cnblogs.com/weibanggang/p/10191028.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/389653
推荐阅读
相关标签
  

闽ICP备14008679号