简体   繁体   English

Spring Cache中的@Cacheable将值存储在cache外部的redis中。 如何将它放入Redis的缓存中?

[英]@Cacheable in Spring Cache Stores value in redis outside cache . How do i put it inside cache in redis?

@Override
@Cacheable("stu")
public EmployeeEntity getEmployee(Integer id) {

    return employeeDAO.findById(id).get(); 
} 

Above code save the key in redis in this format "stu::7" here "stu" is the name of the cache and 7 is the key but it stores the cache name and id as one key. 上面的代码以“ stu :: 7”格式将密钥保存在redis中,其中“ stu”是缓存的名称,而7是密钥,但它将缓存名称和id作为一个密钥存储。

but i want to store in this format in redis STU ->7 Stu should be name of the cache and inside that all the key value pair. 但是我想以这种格式存储在Redis STU中-> 7 Stu应该是缓存的名称,并且在所有键值对内。

It's strange, because docs tell that 很奇怪,因为文档告诉

Default is "", meaning all method parameters are considered as a key, unless a custom keyGenerator() has been configured. 默认值为“”,这意味着所有方法参数均被视为键,除非已配置了自定义keyGenerator()。

It's trivially, but try to set key and cache name explicitly if you don't try it before 这很简单,但是如果您之前没有尝试过,请尝试显式设置键和缓存名称

@Cacheable(value = "stu", key = "{#id}")
public EmployeeEntity getEmployee(Integer id) {
    return employeeDAO.findById(id).get(); 
} 

You can set custom key generator to @Cacheable annotation where you can customise it as you want: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/annotation/Cacheable.html#keyGenerator-- 您可以将自定义密钥生成器设置为@Cacheable批注,您可以根据需要对其进行自定义: https : @Cacheable 的.html#keyGenerator--

@Cacheable(value = "stu", keyGenerator = "customKeyGenerator")

Where customKeyGenerator is the name of your custom key generator bean. 其中customKeyGenerator是自定义密钥生成器Bean的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM