J2cache 简介
J2Cache 是 OSChina 目前正在使用的两级缓存框架(要求至少 Java 8)。第一级缓存使用内存(同时支持 Ehcache 2.x、Ehcache 3.x 和 Caffeine),第二级缓存使用 Redis(推荐)/Memcached 。 由于大量的缓存读取会导致 L2 的网络成为整个系统的瓶颈,因此 L1 的目标是降低对 L2 的读取次数。 该缓存框架主要用于集群环境中。单机也可使用,用于避免应用重启导致的缓存冷启动后对后端业务的冲击。
项目地址:https://gitee.com/ld/J2Cache
项目配置
1、特别说明
- 项目中使用J2cache作为缓存实现
- 项目中使用lettuce 作为redis的客户端
- Mybatis 使用J2cache作为缓存实现
2、根目录放置 j2cache.properties
配置内容如下:
#J2Cache configuration
#########################################
# Cache Broadcast Method
# values:
# jgroups -> use jgroups's multicast
# redis -> use redis publish/subscribe mechanism (using jedis)
# lettuce -> use redis publish/subscribe mechanism (using lettuce, Recommend)
# rabbitmq -> use RabbitMQ publisher/consumer mechanism
# rocketmq -> use RocketMQ publisher/consumer mechanism
# none -> don't notify the other nodes in cluster
# xx.xxxx.xxxx.Xxxxx your own cache broadcast policy classname that implement net.oschina.j2cache.cluster.ClusterPolicy
#########################################
j2cache.broadcast=lettuce
# jgroups properties
jgroups.channel.name=j2cache
jgroups.configXml=conf/j2cache/network.xml
# RabbitMQ properties
rabbitmq.exchange=j2cache
rabbitmq.host=localhost
rabbitmq.port=5672
rabbitmq.username=guest
rabbitmq.password=guest
# RocketMQ properties
rocketmq.name=j2cache
rocketmq.topic=j2cache
# use ; to split multi hosts
rocketmq.hosts=127.0.0.1:9876
#########################################
# Level 1&2 provider
# values:
# none -> disable this level cache
# ehcache -> use ehcache2 as level 1 cache
# ehcache3 -> use ehcache3 as level 1 cache
# caffeine -> use caffeine as level 1 cache(only in memory)
# redis -> use redis as level 2 cache (using jedis)
# lettuce -> use redis as level 2 cache (using lettuce)
# readonly-redis -> use redis as level 2 cache ,but never write data to it. if use this provider, you must uncomment `j2cache.L2.config_section` to make the redis configurations available.
# memcached -> use memcached as level 2 cache (xmemcached),
# [classname] -> use custom provider
#########################################
j2cache.L1.provider_class=ehcache
j2cache.L2.provider_class=lettuce
# When L2 provider isn't `redis`, using `L2.config_section = redis` to read redis configurations
# j2cache.L2.config_section = redis
# Enable/Disable ttl in redis cache data (if disabled, the object in redis will never expire, default:true)
# NOTICE: redis hash mode (redis.storage = hash) do not support this feature)
j2cache.sync_ttl_to_redis=true
# Whether to cache null objects by default (default false)
j2cache.default_cache_null_object=false
#########################################
# Cache Serialization Provider
# values:
# fst -> using fast-serialization (recommend)
# kyro -> using kyro serialization
# json -> using fst's json serialization (testing)
# fastjson -> using fastjson serialization (embed non-static class not support)
# java -> java standard
# [classname implements Serializer]
#########################################
j2cache.serialization=fastjson
#json.map.person = net.oschina.j2cache.demo.Person
#########################################
# Ehcache configuration
#########################################
ehcache.configXml=conf/ehcache/ehcache.xml
# ehcache3.configXml = conf/ehcache/ehcache3.xml
# ehcache3.defaultHeapSize = 1000
#########################################
# Caffeine configuration
# caffeine.region.[name] = size, xxxx[s|m|h|d]
#
#########################################
caffeine.properties=conf/j2cache/caffeine.properties
#########################################
# Redis connection configuration
#########################################
#########################################
# Redis Cluster Mode
#
# single -> single redis server
# sentinel -> master-slaves servers
# cluster -> cluster servers (数据库配置无效,使用 database = 0)
# sharded -> sharded servers (密码、数据库必须在 hosts 中指定,且连接池配置无效 ; redis://user:password@127.0.0.1:6379/0)
#
#########################################
redis.mode=single
#redis storage mode (generic|hash)
redis.storage=generic
## redis pub/sub channel name
redis.channel=j2cache
## redis pub/sub server (using redis.hosts when empty)
redis.channel.host=
#cluster name just for sharded
redis.cluster_name=j2cache
## redis cache namespace optional, default[empty]
redis.namespace=
## connection
# Separate multiple redis nodes with commas, such as 192.168.0.10:6379,192.168.0.11:6379,192.168.0.12:6379
redis.hosts=127.0.0.1:6379
redis.timeout=2000
redis.password=
redis.database=0
## redis pool properties
redis.maxTotal=100
redis.maxIdle=10
redis.maxWaitMillis=5000
redis.minEvictableIdleTimeMillis=60000
redis.minIdle=1
redis.numTestsPerEvictionRun=10
redis.lifo=false
redis.softMinEvictableIdleTimeMillis=10
redis.testOnBorrow=true
redis.testOnReturn=false
redis.testWhileIdle=true
redis.timeBetweenEvictionRunsMillis=300000
redis.blockWhenExhausted=false
redis.jmxEnabled=false
#########################################
# Lettuce scheme
#
# redis -> single redis server
# rediss -> single redis server with ssl
# redis-sentinel -> redis sentinel
# redis-cluster -> cluster servers
#
#########################################
lettuce.mode=single
lettuce.namespace=
lettuce.storage=hash
lettuce.channel=j2cache
lettuce.scheme=redis
lettuce.hosts=139.199.9.193:6379
lettuce.cluster_name=j2cache
lettuce.password=redis
lettuce.database=0
lettuce.sentinelMasterId=
#########################################
# memcached server configurations
# refer to https://gitee.com/mirrors/XMemcached
#########################################
memcached.servers=127.0.0.1:11211
memcached.username=
memcached.password=
memcached.connectionPoolSize=10
memcached.connectTimeout=1000
memcached.failureMode=false
memcached.healSessionInterval=1000
memcached.maxQueuedNoReplyOperations=100
memcached.opTimeout=100
memcached.sanitizeKeys=false
2、在conf 目录下需要有上面配置文件指定的文件
说明: 相关配置内容参考,已有项目模块配置
3、在 application-{env}.yaml 配置中指定缓存配置
因为使用了J2cache作为Mybatis的缓存,就没必要在Service层在使用Spring Cache注解,这里指定spring.cache=generic
同时指定j2cache缓存配置:
################################################################################################################
###j2cache (J2CacheConfig) 基本配置:
###http://mp.baomidou.com/#/spring-boot
################################################################################################################
j2cache:
config-location: /j2cache.properties
open-spring-cache: true
redis-client: lettuce
4、Mybatis 中使用J2cache缓存
Mapper.xml配置文件中增加如下配置:
<!-- 开启二级缓存 -->
<cache type="net.oschina.j2cache.mybatis.J2CacheAdapter"/>
在Mybatis中通过使用 flushCache=”true” 和 useCache=”true” 控制缓存的使用,如果有对象缓存交叉的情况,请定义公共的缓存什么
<insert id="insert" parameterType="DkszbModel" flushCache="true">
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
SELECT sys_guid() FROM DUAL
</selectKey>
INSERT INTO GXXS_KQ_DKSZB(
<include refid="Base_Column_List"></include>
) VALUES (#{id},#{jsid},#{dkfs},#{yxcdsc},#{yxkcsc},#{bdkkssj,jdbcType=VARCHAR},#{bdkjssj,jdbcType=VARCHAR},
#{sfyxbk},#{kbkcs},#{jtnkbk},#{sfcysjdk},#{yxxkbdk},#{kdxkksc,jdbcType=VARCHAR})
</insert>
<update id="update" parameterType="DkszbModel" flushCache="true">
</update>
<select id="getCountByUid" parameterType="String" resultType="Integer" useCache="true">
SELECT COUNT(1) FROM GXXS_KQ_DKSZB WHERE JSID=#{uid}
</select>
作者:Jeebiz 创建时间:2019-07-13 23:35
更新时间:2024-01-23 22:14
更新时间:2024-01-23 22:14