Cache

包含 spring-cache 简单封装:

  1. 针对 Caffeine,Redis, Ehcache 封装
  2. 错误封装和处理 HopeCacheErrorHandler
  3. RedissonClient 自动配置 hope.redis.enable=true
  4. @EnableCaffeineCaching @EnableRedisCaching @EnableEhcacheCaching 支持

配置

Cache 配置

hope.cache 配置路径, 配置对象: HopeCacheProperties

配置备注
caffeineCaffeine配置
caffeine.allowInFlightCacheCreation是否允许创建非定义cache
caffeine.timeToLive默认TTL
caffeine.caches多cache配置map,参考CaffeineSpec
caffeine.caches[*].initialCapacity参考 CaffeinSpec
caffeine.caches[*].maximumWeight参考 CaffeinSpec
caffeine.caches[*].maximumSize参考 CaffeinSpec
caffeine.caches[*].recordStats参考 CaffeinSpec
caffeine.caches[*].keyStrength参考 CaffeinSpec
caffeine.caches[*].valueStrength参考 CaffeinSpec
caffeine.caches[*].expireAfterWrite参考 CaffeinSpec
caffeine.caches[*].expireAfterAccess参考 CaffeinSpec
caffeine.caches[*].refreshAfterWrite参考 CaffeinSpec
redisRedis 配置
redis.allowInFlightCacheCreation是否允许创建非预定义cache
redis.timeToLive默认TTL
redis.caches多cache配置map
redis.caches[*].timeToLive单个cache TTL
redis.caches[*].cacheNullValues单个cache 运行NULL值
redis.caches[*].keyPrefix单个cache keyPrefix
redis.caches[*].useKeyPrefix单个cache useKeyPrefix
redis.caches[*].enableStatistics单个cache 打开统计
redis.caches[*].enableTransactions单个cache 事务管理
redis.caches[*].serializer单个cache 序列化
ehCacheEhCache配置
ehCache.heapHeap 实体数量(个)
ehCache.offHeapOffHeap 大小(存储)
ehCache.diskDisk大小(存储)
ehCache.duration默认TTL
ehCache.caches各个EhCache配置
ehCache.caches[*].keyClzkey 类型
ehCache.caches[*].valueClzvalue 类型
ehCache.caches[*].heapHeap 实体数量(个)
ehCache.caches[*].offHeapOffHeap 大小(存储)
ehCache.caches[*].diskDisk大小(存储)
ehCache.caches[*].durationTTL
errorPolicy错误处理策略
errorPolicy.swallowGetException忽略Get错误
errorPolicy.swallowPutException忽略Put错误
errorPolicy.swallowEvictException忽略Evict错误
errorPolicy.swallowClearException忽略Clear错误
errorPolicy.logStackTraces打印错误堆栈
timeToLive默认TTL(兜底)

Redis 配置

配置路径:hope.redis; 配置对象: HopeRedisProperties

众和了spring 本身的配置:

  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 更借鉴 spring.data.redis 配置方式和 Spring Data RedisProperties 比较类似;

配置备注
cluster集群节点配置
cluster.max-redirects.Maximum 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节点
sentinel.mastersentinel Master 节点
sentinel.nodesList of “host:port” pairs.
sentinel.usernameLogin username for authenticating with sentinel(s).
sentinel.passwordPassword for authenticating with sentinel(s).
mastermaster、slaves配置方式
master.mastermaster 节点 host:port
master.slavesslave 节点 host:port 列表
master.username用户名
master.password密码
databaseDatabase index used by the connection factory.
enable-
config通过路径引入 redission 配置文件
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.
poolSize连接池大小
idleSize最小空闲连接数
idleTimeout连接空闲超时,单位:毫秒
connectionTimeout连接超时,单位:毫秒
timeout命令等待超时,单位:毫秒
clientNameClient name to be set on connections with CLIENT SETNAME.

错误处理

根据 errorPolicy 配置进行处理, 如需抛出组装成 CacheErrorsEnum 输出:

错误码错误标题错误描述错误描述2
1002000001FAIL_GET_CACHEfail get data from the cache缓存没有命中
1002000002FAIL_EVICT_CACHEfail evict data from the cache清除缓存失败
1002000003FAIL_PUT_CACHEfail put data to the cache更新缓存失败
1002000004FAIL_CLEAR_CACHEfail clear cache清理缓存失败
1002000005FAIL_CREATE_CACHEfail create cache创建缓存错误
1002000006CACHE_CONFIGURATION_ILLEGALcache configuration illegal缓存配置错误

hope.cache.type

老的控制方式,根据 hope.cache.type 类型触发配置上下文,现已不支持!

  1. CAFFEINE 通过显式的 @EnableCaffeineCaching
  2. REDIS 通过显式的 @EnableRedisCaching
  3. JCACHE 通过显式的 @EnableEhcacheCaching

Sample