如何在spring boot时从application.properties获取redis哈希配置,比如生存时间?

uxhixvfz  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(375)

我用

  1. @Value("${cache.host}")
  2. private String redisHost;
  3. @Value("${cache.port}")
  4. private int redisPort;

我想从应用程序属性中获取@redishhash中的timetolive。如何获取此配置?

  1. @RedisHash(value = "UserModel", timeToLive = 5)

我在上面手动给出,但是我想从application.properties给出

jckbn6z7

jckbn6z71#

我不确定您是否可以从application.properties执行此操作,但您可以通过使用基于java的配置配置rediscachemanager bean来执行此操作,如下所示:

  1. @Bean
  2. public RedisCacheManager RedisCacheManager(RedisConnectionFactory redisConnectionFactory) {
  3. Map<String, RedisCacheConfiguration> cacheConfig = new HashMap<String, RedisCacheConfiguration>();
  4. cacheConfig.put("UserModel", RedisCacheConfiguration.defaultCacheConfig()
  5. .entryTtl(Duration.ofHours(5)));
  6. RedisCacheManager rdisCacheManager = new RedisCacheManager(
  7. RedisCacheWriter.lockingRedisCacheWriter(redisConnectionFactory),
  8. RedisCacheConfiguration.defaultCacheConfig(), cacheConfig);
  9. return rdisCacheManager;
  10. }

ps:这个方法应该在带有@configuration注解的类中

相关问题