栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合redis 注解 配置失效时间

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合redis 注解 配置失效时间

创建一个基础的springboot项目,这个我就不多说了,不明白的去搜教程。 pom文件

            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            io.projectreactor
            reactor-test
            test
        
        
            com.alibaba
            druid
            1.1.6
        
        
            mysql
            mysql-connector-java
            5.1.6
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        
        
            com.alibaba
            fastjson
            1.2.58
        
        
            com.google.guava
            guava
            18.0
        
 
application.properties
spring.profiles.active=test
application-test.yml
server:
  port: 8899
mybatis:
  mapper-locations: classpath*:mapper
       template.setEnableTransactionSupport(true);
       return template;
   }

    @Bean
    public CacheManager cacheManager(RedisTemplate template) {

        //配置多个缓存名称 这里我们用个能用的模板  这里有坑点往下看
        RedisCacheConfiguration templateRedisCacheCfg = RedisCacheConfiguration
                .defaultCacheConfig()
                // 设置key为String
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(template.getStringSerializer()))
                // 设置value 为自动转Json的Object
                .serializevaluesWith(RedisSerializationContext.SerializationPair.fromSerializer(template.getValueSerializer()))
                // 不缓存null
                .disableCachingNullValues();
        //这里对RedisCacheConfiguration不熟悉的可能会有疑问,我们这样批量templateRedisCacheCfg.entryTtl(Duration.ofMinutes(15))最后不都是同一个引用吗,过期时间会生效吗?
        Map expires = ImmutableMap.builder()
                .put("1m", templateRedisCacheCfg.entryTtl(Duration.ofMinutes(1)))
                .put("30m", templateRedisCacheCfg.entryTtl(Duration.ofMinutes(30)))
                .put("60m", templateRedisCacheCfg.entryTtl(Duration.ofMinutes(60)))
                .put("1d", templateRedisCacheCfg.entryTtl(Duration.ofDays(1)))
                .put("30d", templateRedisCacheCfg.entryTtl(Duration.ofDays(30)))
                .put("common-30d", templateRedisCacheCfg.entryTtl(Duration.ofDays(30)))
                .build();

        RedisCacheManager redisCacheManager =
                RedisCacheManager.RedisCacheManagerBuilder
                        // Redis 连接工厂
                        .fromConnectionFactory(template.getConnectionFactory())
                        // 默认缓存配置
                        .cacheDefaults(templateRedisCacheCfg.entryTtl(Duration.ofHours(1)))
                        // 配置同步修改或删除 put/evict
                        .transactionAware()
                        .initialCacheNames(expires.keySet())
                        .withInitialCacheConfigurations(expires)
                        .build();
        return redisCacheManager;
    }
}
这个时候有一个关键的注解,必须要加,不然redis不起作用,MapperScan注解是扫描mapper接口的。
@MapperScan("com.example.demo.mapper")
@EnableCaching
然后我们写一个接口查询的接口,mybatis我就不细讲了,
@RequestMapping("/redis")
@RestController

public class RedisController {

    @Autowired
    private RedisService redisService;

    @GetMapping("/getCompany")
    public List getCompany(){
       return  redisService.getCompany();
    }
}
@Service
public interface RedisService {

    List getCompany();
}
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private CompanyMapper companyMapper;


    @Override
    @Cacheable(value = "1m",key = "12")
    public List getCompany() {
      return   companyMapper.selectCompanyAll();
    }
}

@Repository
public interface  CompanyMapper {
    List selectCompanyAll();
}

通过上面代码我们可以看到service实现层注解@Cacheable(value = “1m”,key = “12”),这个value就是redis配置的map集合的key,那么这个1m设置的失效时间是1分钟。然后我们看一下实际效果

这里我们可以看到里面存入了一个1m::12的,那么我们是不是可以通过改变这个key值实现多个数据的插入设置失效时间呢?我看来很多文章,很多文章写的有一个很大的问题。也是通过这个value去实现动态的失效时间,但是他们的不能设置key值,所以如果存在多个就会导致覆盖。 最后就是redis和数据数据不一致性问题,这个目前都没有解决方案,目前百度查到的方案都存在缺陷,所以呢,我们存redis数据的数据是不轻易更改的数据,这个极大的防止这个问题。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/711636.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号