spring - 带redis的弹簧靴
问题描述
我使用 spring boot 和 redis 进行缓存。我可以缓存从数据库(oracle)获取的数据使用@Cacheable(key = "{#input,#page,#size}",value = "on_test")
。当我尝试key("on_test::0,0,10")
使用 redisTemplate 获取数据时,结果为 0 为什么?
Redis 配置:
@Configuration
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
redisStandaloneConfiguration.setPassword(RedisPassword.of("admin@123"));
return new JedisConnectionFactory(redisStandaloneConfiguration);
}
@Bean
public RedisTemplate<String,Objects> redisTemplate() {
RedisTemplate<String,Objects> template = new RedisTemplate<>();
template.setStringSerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
//service
@Override
@Cacheable(key = "{#input,#page,#size}",value = "on_test")
public Page<?> getAllByZikaConfirmedClinicIs(Integer input,int page,int size) {
try {
Pageable newPage = PageRequest.of(page, size);
String fromCache = controlledCacheService.getFromCache();
if (fromCache == null && input!=null) {
log.info("cache is empty lets initials it!!!");
Page<DataSet> all = dataSetRepository.getAllByZikaConfirmedClinicIs(input,newPage);
List<DataSet> d = redisTemplate.opsForHash().values("on_test::0,0,10");
System.out.print(d);
return all;
}
return null;
解决方案
使用的全部意义@Cacheable
在于您不需要RedisTemplate
直接使用。您只需要调用getAllByZikaConfirmedClinicIs()
(从定义它的类的外部),Spring 将首先自动检查缓存的结果是否可用并返回它而不是调用函数。
如果这不起作用,您是否已注释您的 Spring Boot 配置类之一@EnableCaching
以启用缓存?
您可能还需要设置spring.cache.type=REDIS
inapplication.properties
或spring.cache.type: REDIS
inapplication.yml
以确保 Spring 使用的是 Redis 而不是其他一些缓存提供程序。
推荐阅读
- vhdl - 当整个条件为真时检查 if 语句中的下一个条件
- list - odoo 10 域签入日期!= 签出日期
- android - 在 Firebase AuthUI 中重新定位徽标
- powershell - Powershell 脚本的输出显示不正确
- python - 从 Tensorflow 服务服务器获取 StatusCode.FAILED_PRECONDITION:未找到服务签名密钥“tensorflow/serving/predict”
- c# - 为什么 xPath 假定命名空间
- html - 使用 grep 将 HTML 解析为 CSV
- javascript - 使用 JSBarcode 库无法正确打印条码
- c++ - 计算小数点后的数字c ++
- c# - Combobox.SelectedItem 在参数中不起作用