Skip to content

Spring Cache

Spring Provide Uniformed Cache Abstraction

  • Add cache support for Java method to cache the execute result
  • Support ConcurrentMap, EhCache, Caffeine, JCache(JSR-107
  • Interfaces
  • org.springframework.cache.Cache
  • org.springframework.cache.CacheManager

Cache Annotations

@EnableCaching(procyTargetClass=true)

  • @Cacheable
  • @CacheEvict
  • @CachePut
  • @Caching
  • @CacheConfig

Use Redis as Cache

Configurations,

  • spring.cache.type = redis
  • spring.cache.cahe-names = coffee
  • spring.cache.redis.time-tolive = 5000
  • spring.cache.redis.cache-null-values = false
  • spring.redis.host = localhost

Connection with Redis

The new spring data Redis uses LettuceConnectionFactory to replace JedisConnectionFactory.

  • RedisStandaloneConfiguration
  • RedisSentinelConfiguration
  • RedisClusterConfiguration

Read-Write Separation

Lettuce supports read-write separation.

  • Read-only primary server, read-only secondary server
  • Read primary server first, read secondary server first

LettuceClientConfiguration

LettucePoolingClientConfiguration

LettuceClientConfigurationBuilderCustomizer

RedisTemplate

RedisTemplate

  • opsForXxx()

StringRedisTemplate

Don't forget to set expire time

RedisRepository

Annotations

  • @RedisHash
  • @Id
  • @Indexed
  • @EnableRedisRepository

Example

  • Properties

    Properties
    spring.redis.host=localhost
    spring.redis.lettuce.pool.maxActive=5
    spring.redis.lettuce.pool.maxIdle=5
    
  • Repository

    Java
    public interface CoffeeCacheRepository extends CrudRepository<CoffeeCache, Long> {
        Optional<CoffeeCache> findOneByName(String name);
    }
    
  • Cache Entity

    Java
    import lombok.AllArgsConstructor;
    import lombok.Builder;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.joda.money.Money;
    import org.springframework.data.annotation.Id;
    import org.springframework.data.redis.core.RedisHash;
    import org.springframework.data.redis.core.index.Indexed;
    
    @RedisHash(value = "springbucks-coffee", timeToLive = 60)
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public class CoffeeCache {
        @Id
        private Long id;
        @Indexed
        private String name;
        private Money price;
    }
    
  • RedisCustomConverions

Handle Different Data Source Repository

How to distinguish these repositories?

  • Based on entity annotation
  • Based on the inherited interface type
  • Scan different package