Как я могу добавить префикс cacheNames к префиксу spring.cache.redis.key? - PullRequest
0 голосов
/ 26 июня 2019

Мне удалось заставить cacheNames работать, и мои ключи Redis выглядят так:

{cacheName}::{myKey}
{cacheName}::{myKey}

Теперь мне интересно, как я могу поставить префикс {cacheName} с моим сконфигурированным значением spring.cache.redis.key-prefix?

Когда я ставлю эти записи,

spring.cache.redis.key-prefix=some::
spring.cache.redis.use-key-prefix=true

Я хочу, чтобы ключи выглядели так.

some::{cacheName}::{myKey}
some::{cacheName}::{myKey}

1 Ответ

0 голосов
/ 27 июня 2019

Я не уверен, как использовать конфигурацию вместе с внутренними функциями.

Я накопил проблему. https://jira.spring.io/browse/DATAREDIS-1006

Мне удалось добиться того, что я хотел сделать с помощью следующих кодов.

@PostConstruct
private void onPostConstruct() {
    if (springCacheRedisKeyPrefix != null) {
        springCacheRedisKeyPrefix = springCacheRedisKeyPrefix.trim();
    }
    if (springCacheRedisUseKeyPrefix && springCacheRedisKeyPrefix != null
        && !springCacheRedisKeyPrefix.isEmpty()) {
        cacheKeyPrefix = cacheName -> springCacheRedisKeyPrefix + "::" + cacheName + "::";
    } else {
        cacheKeyPrefix = CacheKeyPrefix.simple();
    }
}

@Bean
public RedisCacheManager cacheManager(final RedisConnectionFactory connectionFactory) {
    final RedisCacheManager cacheManager = RedisCacheManager.builder(connectionFactory)
            .cacheDefaults(defaultCacheConfig()
                                   .computePrefixWith(cacheKeyPrefix)
                                   .entryTtl(Duration.ofMillis(springCacheRedisTimeToLive))
            )
            .build();
    return cacheManager;
}

@Value(value = "${spring.cache.redis.key-prefix:}")
private String springCacheRedisKeyPrefix;

@Value("${spring.cache.redis.use-key-prefix:false}")
private boolean springCacheRedisUseKeyPrefix;

@Value("${spring.cache.redis.time-to-live:1200000}")
private long springCacheRedisTimeToLive;

private transient CacheKeyPrefix cacheKeyPrefix;
...