【问题标题】:spring boot with redis带redis的弹簧靴
【发布时间】:2020-05-18 11:45:53
【问题描述】:

我使用 spring boot 和 redis 进行缓存。我可以缓存从数据库(oracle)获取的数据,使用 @Cacheable(key = "{#input,#page,#size}",value = "on_test")。 当我尝试使用 redisTemplate 从key("on_test::0,0,10") 获取数据时,结果为 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;

【问题讨论】:

    标签: spring spring-boot redis


    【解决方案1】:

    使用@Cacheable 的全部意义在于您不需要直接使用RedisTemplate。您只需要调用getAllByZikaConfirmedClinicIs()(从定义它的类外部),Spring 将首先自动检查缓存结果是否可用并返回,而不是调用函数。

    如果这不起作用,您是否使用 @EnableCaching 注释了您的 Spring Boot 配置类之一以启用缓存?

    您可能还需要在application.properties 中设置spring.cache.type=REDIS,或在application.yml 中设置spring.cache.type: REDIS,以确保Spring 使用的是Redis 而不是其他缓存提供程序。

    【讨论】:

    • 是的,我这样做了。我希望我的缓存数据搜索缓存数据。@Cacheable 工作正常,但我需要从缓存中获取数据并搜索
    • 您是否尝试过缓存一些内容,然后运行 ​​Redis 命令 KEYS * 以查看实际的缓存键是什么?
    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 2021-02-16
    • 2018-11-22
    • 1970-01-01
    相关资源
    最近更新 更多