springboot 集成J2Cache

 

J2Cache 是 OSChina 目前正在使用的两级缓存框架。第一级缓存使用 Ehcache,第二级缓存使用 Redis 。由于大量的缓存读取会导致 L2 的网络成为整个系统的瓶颈,因此 L1 的目标是降低对 L2 的读取次数。该缓存框架主要用于集群环境中。单机也可使用,用于避免应用重启导致的 Ehcache 缓存数据丢失。

j2Cache提供了springboot 的集成。

集成方法如下:

1.引入pom.xml

 <dependency>
            <groupId>net.oschina.j2cache</groupId>
            <artifactId>j2cache-spring-boot2-starter</artifactId>
            <version>2.7.6-release</version>
        </dependency>

        <dependency>
            <groupId>net.oschina.j2cache</groupId>
            <artifactId>j2cache-core</artifactId>
            <version>2.7.7-release</version>
        </dependency>

2.增加配置文件

ehcache3.xml

<!-- for ehcache 3.x -->
<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">

    <!-- Don't remote default cache configuration -->
    <cache-template name="default">
        <key-type>java.lang.String</key-type>
        <value-type>java.io.Serializable</value-type>
        <expiry>
            <ttl unit="seconds">1800</ttl>
        </expiry>
        <resources>
            <heap>1000</heap>
            <offheap unit="MB">100</offheap>
        </resources>

    </cache-template>
    <!--
    <persistence directory="${ecache.path}"/>
    -->
    <cache alias="default" uses-template="default"/>

</config>

j2cache.properties

#J2Cache configuration


#########################################
# Cache Broadcast Method
# values:
# jgroups -> use jgroups's multicast
# redis -> use redis publish/subscribe mechanism
#########################################

j2cache.broadcast = net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy

# 是否开启二级缓存
j2cache.l2-cache-open=true
j2cache.open-spring-cache= true
j2cache.allow-null-values= true
j2cache.cache-clean-mode= active
j2cache.redis-client=jedis

#组播的通道名称
jgroups.channel.name = j2cache

#########################################
# 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(hashs) as level 2 cache
# [classname] -> use custom provider
#########################################

j2cache.L1.provider_class = ehcache3
j2cache.L2.provider_class = net.oschina.j2cache.cache.support.redis.SpringRedisProvider
#j2cache.L2.provider_class = redis
j2cache.L2.config_section = redis
#j2cache.L2.provider_class = redis

#########################################
# Cache Serialization Provider
# values:
# fst -> fast-serialization
# kyro -> kyro
# java -> java standard
# [classname implements Serializer]
#########################################

j2cache.serialization = fst

#########################################
# Ehcache configuration
#########################################

#ehcache.name=
#ehcache.configXml=/ehcache.xml
ehcache3.configXml = /config/ehcache3.xml

#########################################
# Caffeine configuration
# caffeine.region.[name] = size, xxxx[s|m|h|d]
#
#########################################

caffeine.region.default = 1000, 1h 

#########################################
# 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 = sentinel
redis.mode = single
#cluster name just for sharded
redis.cluster_name = mymaster

## redis cache namespace optional, default[j2cache]
redis.namespace = j2cache

## connection
#redis.hosts = 127.0.0.1:26378,127.0.0.1:26379,127.0.0.1:26380
redis.hosts = 192.168.1.100:6379
redis.timeout = 2000
redis.password =
#redis.database = 0

## redis pub/sub channel name
redis.channel = j2cache

## redis pool properties
redis.maxTotal = -1
redis.maxIdle = 2000
redis.maxWaitMillis = 100
redis.minEvictableIdleTimeMillis = 864000000
redis.minIdle = 1000
redis.numTestsPerEvictionRun = 10
redis.lifo = false
redis.softMinEvictableIdleTimeMillis = 10
redis.testOnBorrow = true
redis.testOnReturn = false
redis.testWhileIdle = false
redis.timeBetweenEvictionRunsMillis = 300000
redis.blockWhenExhausted = true

 

redis.mode = single 模式支持 single,sentinel,cluster

redis.hosts:需要配置为redis的主机

j2cache.l2-cache-open:是否开启二级缓存

3.注入CacheChannel 

@Component(value = "iCache")
public class J2cacheImpl implements ICache {

    private String region="rx";

    @Autowired
    private CacheChannel cacheChannel;
 
 

4.修改配置文件

application.properties

j2cache:
  config-location: classpath:/config/j2cache.properties
  

配置j2cache.properties 文件路径

 

转载于:https://www.cnblogs.com/yg_zhang/p/10937977.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
J2CacheOSChina目前正在使用的两级缓存框架(要求至少 Java 8)。第一级缓存使用内存(同时支持 Ehcache 2.x、Ehcache 3.x 和 Caffeine),第二级缓存使用 Redis(推荐)/Memcached。由于大量的缓存读取会导致L2的网络成为整个系统的瓶颈,因此L1的目标是降低对L2的读取次数。该缓存框架主要用于集群环境中。单机也可使用,用于避免应用重启导致的缓存冷启动后对后端业务的冲击。 数据读取: 读取顺序 -> L1 -> L2 -> DB 数据更新 1、从数据库中读取最新数据,依次更新L1 -> L2 ,发送广播清除某个缓存信息 2、接收到广播(手工清除缓存 & 一级缓存自动失效),从L1中清除指定的缓存信息 J2Cache配置: 配置文件位于core/resources目录下,包含三个文件: j2cache.properties J2Cache核心配置文件,可配置两级的缓存,Redis服务器、连接池以及缓存广播的方式 caffeine.properties如果一级缓存选用Caffeine ,那么该文件用来配置缓存信息 ehcache.xml Ehcache的配置文件,配置说明请参考Ehcache文档 ehcache3.xml Ehcache3的配置文件,配置说明请参考Ehcache文档 network.xml JGroups网络配置,如果使用JGroups组播的话需要这个文件,一般无需修改 实际使用过程需要将所需的配置文件复制到应用类路径中,如WEB-INF/classes目录。 J2Cache运行时所需jar包请查看core/pom.xml 测试方法: 1、安装Redis 2、git clone https://gitee.com/ld/J2Cache 3、修改core/resource/j2cache.properties配置使用已安装的Redis服务器 4、在命令行中执行mvn package -DskipTests=true进行项目编译 5、打开多个命令行窗口,同时运行runtest.sh 6、在 > 提示符后输入help查看命令,并进行测试

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值