The data that wants to be cached in redis cannot be saved, and this error has been reported.
Send the error content first:
Null key returned for cache operation (maybe you are using named params on classes without debug info?) caches=[value] | key='#xxx' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
Use @Cacheable code:
//代码中的xxx表示你存到redis中键值对的键(也就是key)
/代码中的value表示你存到redis中键值对的值
@Cacheable(value = "value", key = "#xxx")
public Student getValueByXXX(String xxx) {
return studentDao.getValueByXXX(xxx);
}
Reason for error: Redis determines that the key is null
Solution: Set the key generation strategy in the RedisConfig class.
The code is as follows:
/**
* 配置key的生成策略
* 下边的xxx就是你使用 @Cacheable注解内的key = "#xxx"中的xxx
* @return
*/
@Bean(name = "xxxGenerator")
public KeyGenerator xxxGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(params[0].toString());
return sb.toString();
}
};
}
If you import a package, guide this package:
import org.springframework.cache.interceptor.KeyGenerator;