J2Cache SpringBoot集成(j2cache-spring-boot2-starter)

目录

    • 一、引言
    • 二、核心配置类
    • 三、核心实现类
    • 四、重构Starter支持Spring Data Redis原生实现

一、引言

在J2Cache源码中,内置了一个j2cache-spring-boot2-starter模块
J2Cache SpringBoot集成(j2cache-spring-boot2-starter)_第1张图片
查看源码即可发现此模块适用于J2Cache与SpringBoot2的快速集成,
底层支持通过J2Cache相关配置覆盖spring-boot-starter-data-redis实现
(即支持RedisTemplateMessageLIstener广播监听),
同时提供了Spring Cache的相关适配。

关于j2cache-spring-boot2-starter模块的配置及使用示例可参见:
https://gitee.com/luoex/j2cache-demo/tree/master/j2cache-spring-cache-demo
文章最后也提供了一版重构后的J2Cache Spring Boot Starter实现,具体说明及实现可参见后文。

二、核心配置类

查看此模块resources/META-INF/spring.factories即可发现其核心配置类如下图:
J2Cache SpringBoot集成(j2cache-spring-boot2-starter)_第2张图片

配置类 说明 Spring关联配置(application.properties) J2Cache自定义配置(j2cache.properties)
J2CacheAutoConfiguration 初始化J2Cache
读取Spring及j2cache相关配置,
初始化J2CacheConfig及CacheChannel
j2cache.config-location=/j2cache.properties
注:需在此配置指定的位置创建J2Cache配置属性文件,如resources/j2cache.properties
J2CacheSpringCacheAutoConfiguration 初始化J2Cache支持Spring Cache适配器
根据CacheChannel创建自定义J2CacheCacheManger
j2cache.open-spring-cache=true
j2cache.allow-null-values=true
spring.cache.type=GENERIC
J2CacheSpringRedisAutoConfiguration 兼容Spring Data Redis配置
通过J2Cache Redis相关配置生成Spring Redis相关配置(JedisConnectionFactory、LettuceConnectionFactory、RedisTemplate、RedisSerializer、RedisMessageListenerContainer),如此仅需配置J2Cache后便可同样使用RedisTemplate等。
j2cache.l2-cache-open=true
j2cache.redis-client=jedis|lettuce
j2cache.j2CacheConfig.serialization=fastjson
j2cache.cache-clean-mode=active|passive|blend
j2cache.L2.provider_class=net.oschina.j2cache.cache.support.redis.SpringRedisProvider
j2cache.L2.config_section=redis
j2cache.broadcast=net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy

三、核心实现类

J2Cache SpringBoot集成(j2cache-spring-boot2-starter)_第3张图片

核心模块 实现类
J2Cache L2缓存 - Spring Data Redis(RedisTemplate)实现 SpringRedisProvider - RedisTemplate实现
SpringRedisGenericCache - key/value实现
SpringRedisCache - hash实现
J2CacheSerializer - Redis序列化器实现
J2Cache ClusterPolicy - Spring Data Redis(MessageListener)实现 SpringRedisPubSubPolicy - J2Cache ClusterPolicy实现(变更通知)
SpringRedisActiveMessageListener - active通知具体实现(支持Redis事件通知Egx,接到key超时、删除通知后执行evict L1缓存)
订阅channel(以下0需替换为具体Redis db):
__keyevent@0__:expired
__keyevent@0__:del
SpringRedisMessageListener - passive通知具体实现(兼容原J2Cache实现,即L1过期后通知其他节点,向Redis指定channel发送Command)
ConfigureNotifyKeyspaceEventsAction - 配置Redis notify-keyspace-events(Egx)
J2Cache - Spring Cache实现 J2cacheCacheManger - Spring CacheManager实现
J2CacheCache - Spring Cache实现
注:依赖J2Cache CacheChannel

感兴趣的可以查看具体源码实现。
J2Cache SpringBoot集成(j2cache-spring-boot2-starter)_第4张图片

缓存清除模式
active:主动清除,二级缓存过期主动通知各节点清除,优点在于所有节点可以同时收到缓存清除
passive:被动清除,一级缓存过期进行通知各节点清除一二级缓存
blend:两种模式一起运作,对于各个节点缓存准确性以及及时性要求高的可以使用(推荐使用前面两种模式中一种)

Redis notify-keyspace-events配置(Redis keyspace notifications)
K Keyspace events, published with keyspace@ prefix.
E Keyevent events, published with keyevent@ prefix.
g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, …
$ String commands
l List commands
s Set commands
h Hash commands
z Sorted set commands
t Stream commands
d Module key type events
x Expired events (events generated every time a key expires)
e Evicted events (events generated when a key is evicted for maxmemory)
m Key miss events (events generated when a key that doesn’t exist is accessed)
A Alias for “g$lshztxed”, so that the “AKE” string means all the events except “m”.

四、重构Starter支持Spring Data Redis原生实现

由于直接使用社区提供的j2cache-spring-boot2-starter是将j2cache.properties的配置转换为Spring Data Redis配置,
即读取j2cache.properties中的redis属性再转换为Spring Data Redis相关对象(JedisConnectionFactory, LettuceConnectionFactory),
具体实现逻辑可参看:
https://gitee.com/ld/J2Cache/…/modules/spring-boot2-starter/…/autoconfigure/J2CacheSpringRedisAutoConfiguration.java

本示例提供了一种更贴近SpringBoot生态的方式,重构后的starter直接使用Spring Data Redis相关配置及对象,无需再通过单独的j2cache.properties进行配置及构造,同时对J2Cache的配置进行了提取与封装,可以基于SpringBoot生态application.yml等直接进行配置,并复用了原starter关于RedisSerializer、MessageListener、SpringRedisProvider、J2CacheCacheManager等实现。

代码库

重构后的j2cache-native-spring-boot-starter实现及集成示例可参见:
https://gitee.com/luoex/j2cache-demo/tree/master/j2cache-spring-cache-native-demo
J2Cache SpringBoot集成(j2cache-spring-boot2-starter)_第5张图片

配置示例

spring:
  # Spring Data Redis配置
  redis:
    client-type: lettuce
    host: 127.0.0.1
    port: 6379
    password:
    database: 0
    # lettuce连接池配置
    lettuce:
      pool:
        enabled: true
        max-active: 8
        max-idle: 8
        min-idle: 5
        time-between-eviction-runs: 30s
        max-wait: 5s
      shutdown-timeout: 100ms
# J2Cache配置
j2cache:
  # 开启J2Cache支持Spring Cache
  open-spring-cache: true
  # 缓存清除模式(active | passive | blend)
  cache-clean-mode: passive
  # 缓存变更广播方式(jgroups | redis | lettuce | rabbitmq | rocketmq | none | 自定义class)
  # 保持默认SpringBoot Redis自定义实现SpringRedisPubSubPolicy
  broadcast: com.luo.j2cache.sb.cache.support.redis.SpringRedisPubSubPolicy
  # 缓存null对象
  default-cache-null-object: true
  # L1缓存配置
  l1:
    # L1 缓存provider名称(caffeine | ehcache | ehcache3)
    provider-class: caffeine
    # Caffeine配置
    caffeine:
      region:
        default: 100,10m
        myRegion: 10,5m
  # L2缓存配置
  l2:
    # L2 缓存provider名称(redis | lettuce | readonly-redis | memcached)
    # 保持默认SpringBoot Redis自定义实现SpringRedisProvider
    provider-class: com.luo.j2cache.sb.cache.support.redis.SpringRedisProvider
    # 二级缓存序列化格式(fst(推荐) | kryo | json | fastjson | java(默认) | fse | 自定义classname)
    serialization: fastjson
    redis:
      # 是否启用同步一级缓存的Time-To-Live超时时间到Redis TTL(true启用,false不启用则永不超时)
      sync-ttl-to-redis: true
      # Redis发布订阅(Pub/Sub)对应的channel名称
      channel: j2cache
      #  Key命名空间(前缀),默认为空
      namespace: myName
#  兼容原J2Cache属性配置(即原来需要放到j2cache.properties中的属性皆可直接在此配置)
#  默认使用redis则无需此配置  
#  properties: 
#    caffeine.region.myRegion: 100,5m
#    redis.mode: single   

你可能感兴趣的:(java,#,springboot,j2cache,springboot,redis,caffeine)