Cache

Includes a simple wrapper for spring-cache:

  1. Wrapper for Caffeine, Redis, Ehcache
  2. Error handling and encapsulation via HopeCacheErrorHandler
  3. RedissonClient auto-configuration when hope.redis.enable=true
  4. Support for @EnableCaffeineCaching, @EnableRedisCaching, @EnableEhcacheCaching

Configuration

Cache Configuration

hope.cache configuration path, configuration object: HopeCacheProperties

ConfigurationNote
caffeineCaffeine Configuration
caffeine.allowInFlightCacheCreationWhether to allow the creation of undefined caches
caffeine.timeToLiveDefault TTL
caffeine.cachesMultiple cache configuration map, refer to CaffeineSpec
caffeine.caches[*].initialCapacityRefer to CaffeineSpec
caffeine.caches[*].maximumWeightRefer to CaffeineSpec
caffeine.caches[*].maximumSizeRefer to CaffeineSpec
caffeine.caches[*].recordStatsRefer to CaffeineSpec
caffeine.caches[*].keyStrengthRefer to CaffeineSpec
caffeine.caches[*].valueStrengthRefer to CaffeineSpec
caffeine.caches[*].expireAfterWriteRefer to CaffeineSpec
caffeine.caches[*].expireAfterAccessRefer to CaffeineSpec
caffeine.caches[*].refreshAfterWriteRefer to CaffeineSpec
redisRedis Configuration
redis.allowInFlightCacheCreationWhether to allow the creation of undefined caches
redis.timeToLiveDefault TTL
redis.cachesMultiple cache configuration map
redis.caches[*].timeToLiveIndividual cache TTL
redis.caches[*].cacheNullValuesAllow NULL values in individual cache
redis.caches[*].keyPrefixKey prefix for individual cache
redis.caches[*].useKeyPrefixUse key prefix for individual cache
redis.caches[*].enableStatisticsEnable statistics for individual cache
redis.caches[*].enableTransactionsEnable transaction management for individual cache
redis.caches[*].serializerSerializer for individual cache
ehCacheEhCache Configuration
ehCache.heapNumber of heap entities
ehCache.offHeapOffHeap size (storage)
ehCache.diskDisk size (storage)
ehCache.durationDefault TTL
ehCache.cachesVarious EhCache configurations
ehCache.caches[*].keyClzKey type
ehCache.caches[*].valueClzValue type
ehCache.caches[*].heapNumber of heap entities
ehCache.caches[*].offHeapOffHeap size (storage)
ehCache.caches[*].diskDisk size (storage)
ehCache.caches[*].durationTTL
errorPolicyError handling policy
errorPolicy.swallowGetExceptionIgnore Get errors
errorPolicy.swallowPutExceptionIgnore Put errors
errorPolicy.swallowEvictExceptionIgnore Evict errors
errorPolicy.swallowClearExceptionIgnore Clear errors
errorPolicy.logStackTracesPrint error stack traces
timeToLiveDefault TTL (fallback)

Redis Configuration

Configuration path: hope.redis; Configuration object: HopeRedisProperties

Merged with Spring’s own configuration:

  1. Spring Boot CacheProperties
  2. Spring Data RedisProperties
  3. Spring Boot CacheAutoConfiguration
  4. Spring Boot Cache
  5. Spring Cache spring.cache
  6. Spring Data Redis spring.data.redis

Hope further draws on the spring.data.redis configuration style, and is quite similar to Spring Data RedisProperties;

ConfigurationNote
clusterCluster node configuration
cluster.max-redirectsMaximum number of redirects to follow when executing commands across the cluster.
cluster.nodesList of “host:port” pairs to bootstrap from. This represents an “initial” list of cluster nodes and is required to have at least one entry.
sentinelSentinel nodes
sentinel.masterSentinel master node
sentinel.nodesList of “host:port” pairs.
sentinel.usernameLogin username for authenticating with sentinel(s).
sentinel.passwordPassword for authenticating with sentinel(s).
masterConfiguration method for master and slaves
master.masterMaster node host:port
master.slavesSlave node host:port list
master.usernameUsername
master.passwordPassword
databaseDatabase index used by the connection factory.
enable-
configImport Redisson configuration file via path
urlConnection URL. Overrides host, port, username, and password. Example: redis://user:password@example.com:6379
hostRedis server host.
usernameLogin username of the Redis server.
passwordLogin password of the Redis server.
portRedis server port.
sslWhether to enable SSL support.
poolSizeConnection pool size
idleSizeMinimum number of idle connections
idleTimeoutIdle connection timeout, unit: milliseconds
connectionTimeoutConnection timeout, unit: milliseconds
timeoutCommand wait timeout, unit: milliseconds
clientNameClient name to be set on connections with CLIENT SETNAME.

Error Handling

Handled according to the errorPolicy configuration. If an error needs to be thrown, it will be assembled into CacheErrorsEnum output:

Error CodeError TitleError DescriptionError Description 2
1002000001FAIL_GET_CACHEFail to get data from the cacheCache miss
1002000002FAIL_EVICT_CACHEFail to evict data from the cacheCache clear failure
1002000003FAIL_PUT_CACHEFail to put data to the cacheCache update failure
1002000004FAIL_CLEAR_CACHEFail to clear cacheCache clear failure
1002000005FAIL_CREATE_CACHEFail to create cacheCache creation error
1002000006CACHE_CONFIGURATION_ILLEGALCache configuration illegalCache configuration error

hope.cache.type

Old control method, triggering configuration context based on hope.cache.type type is no longer supported!

  1. CAFFEINE: Explicitly via @EnableCaffeineCaching
  2. REDIS: Explicitly via @EnableRedisCaching
  3. JCACHE: Explicitly via @EnableEhcacheCaching

Sample