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

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

我用

@Value("${cache.host}")
    private String redisHost;

    @Value("${cache.port}")
    private int redisPort;

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

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

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

jckbn6z7

jckbn6z71#

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

@Bean
public RedisCacheManager RedisCacheManager(RedisConnectionFactory redisConnectionFactory) {
    Map<String, RedisCacheConfiguration> cacheConfig = new HashMap<String, RedisCacheConfiguration>();
    cacheConfig.put("UserModel", RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofHours(5)));
    RedisCacheManager rdisCacheManager = new RedisCacheManager(
            RedisCacheWriter.lockingRedisCacheWriter(redisConnectionFactory),
            RedisCacheConfiguration.defaultCacheConfig(), cacheConfig);
    return rdisCacheManager;
}

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

相关问题