spring using some of the key management issues RedisCacheManager

spring can well manage a variety of fast cache memory.

These common memory cache library implementations have redis, Ehcache.

This article deals with a redis, after all, this thing is pretty easy to use.

 

spring by org.springframework.cache.Cache and org.springframework.cache.CacheManager two interfaces to manage the cache

redis the cache implementation class is RedisCacheManager, their relationship is this:

object

   <-AbstractCacheManager=>(CacheManager, InitializingBean)

       <-AbstractTransactionSupportingCacheManager

          <-RedisCacheManager

It can be seen RedisCacheManager implements the interface CacheManager interface.

First, how to customize the key redis

If you use the default way to register RedisCacheManager, as follows:

RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(this.cacheTimeOutHour))

Assume comment is this:

@Cacheable(value="getUserPoJoById",key="#userId")

The generated key is then redis shaped like this:

getUserPoJoById :: 103

Wherein the double colon (: :) is the delimiter.

This is because RedisCacheConfiguration.defaultCacheConfig () source code as follows:

 

public static RedisCacheConfiguration defaultCacheConfig() {
        return defaultCacheConfig(null);
    }
public static RedisCacheConfiguration defaultCacheConfig(@Nullable ClassLoader classLoader) {

        DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();

        registerDefaultConverters(conversionService);

        return new RedisCacheConfiguration(Duration.ZERO, true, true, CacheKeyPrefix.simple(),
                SerializationPair.fromSerializer(RedisSerializer.string()),
                SerializationPair.fromSerializer(RedisSerializer.java(classLoader)), conversionService);
    }

 

Can be seen from the above code prefix key is used CacheKeyPrefix.simple () , . CacheKeyPrefix Simple () is implemented as follows:

 

@FunctionalInterface
public interface CacheKeyPrefix {

    /**
     * Compute the prefix for the actual {@literal key} stored in Redis.
     *
     * @param cacheName will never be {@literal null}.
     * @return never {@literal null}.
     */
    String compute(String cacheName);

    /**
     * Creates a default {@link CacheKeyPrefix} scheme that prefixes cache keys with {@code cacheName} followed by double
     * colons. A cache named {@code myCache} will prefix all cache keys with {@code myCache::}.
     *
     * @return the default {@link CacheKeyPrefix} scheme.
     */
    static CacheKeyPrefix simple() {
        return name -> name + "::";
    }
}

 simple implementation CacheKeyPrefix the method is equivalent to compute:

String compute(String cacheName){

   return cacheName+"::";

}

 

So the default is to use the double-colon to separate them.

 

But in many cases, we do not want redis the key is this form, we may want to:

  1. Before the whole key prefix
  2. Using different delimiter

How to do it? Call CacheKeyPrefix customization can be achieved.

Let's look at CacheKeyPrefix the only interface method (non-static):

String compute(String cacheName);

That we may need to specify achieved through compute.

With the idea, then the following is the implementation:

 

RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(this.cacheTimeOutHour)).computePrefixWith(cacheName -> cacheName + this.keyPrefix);
        RedisCacheManager cm=RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(redisCacheConfiguration).build();
        cm.setTransactionAware(true);

 

Critical code section earlier: computePrefixWith ( CacheName -> + CacheName the this .keyPrefix);

We look at computePrefixWith of RedisCacheConfiguration implementation code:

public RedisCacheConfiguration computePrefixWith(CacheKeyPrefix cacheKeyPrefix) {

        Assert.notNull(cacheKeyPrefix, "Function for computing prefix must not be null!");

        return new RedisCacheConfiguration(ttl, cacheNullValues, true, cacheKeyPrefix, keySerializationPair,
                valueSerializationPair, conversionService);
    }

Therefore Code: CacheName -> + CacheName the this .keyPrefix is to construct CacheKeyPrefix the compute method

 

String compute(String cacheName){

   return cacheName+this.keyPrefix;

}

 

 

If you want to add a prefix, it can be:

computePrefixWith ( CacheName -> this.getCachePrefix + "->" + + CacheName the this .keyPrefix)
which is equivalent to compute method becomes: return this.getCachePrefix + "->" + + CacheName the this .keyPrefix

 

 

5.1.x spring after heavy use lambda, if not familiar with, you can not read this code.

 

Second, the definition of other key aspects that need attention

1. When a plurality of applications share a time redis example, note the use of the prefix

2. If there is a lot of value, it is recommended key shorter, and form a key document in the name

Guess you like

Origin www.cnblogs.com/lzfhope/p/12459759.html