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 文件路径

springboot 集成J2Cache的更多相关文章

  1. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  2. SpringBoot集成security

    本文就SpringBoot集成Security的使用步骤做出解释说明.

  3. springboot集成Actuator

    Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...

  4. SpringBoot集成Shiro并用MongoDB做Session存储

    之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...

  5. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  6. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

  7. springboot集成mybatis(一)

    MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...

  8. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  9. SpringBoot集成jsp

    一.springBoot集成jsp: 1.修改pom文件 <!--集成jsp所需jar包--> <!--jsp页面使用jstl标签--> <dependency> ...

随机推荐

  1. 如何更新GitHub上的代码?

    更新github上的代码 一.克隆代码 1.先是把自己GitHub上的代码克隆到本地(下载到本地) 步骤1.随便创建一个新文件夹(用来存放下载下来的代码) 步骤2.在文件夹中打开cmd 输入下载指令下 ...

  2. centos6中安装新版 Elasticsearch 7.x

    es出新版了,虽然公司里还是用的老版本,但是本地还是有必要自己安装了玩玩 下载地址:https://www.elastic.co/cn/downloads/elasticsearch 那么一般来说还是 ...

  3. PATA1028 List Sorting

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...

  4. LOJ6625 时间复杂度(min_25筛)

    本人在LOJ的第三题(前两题太水不好意思说了QwQ),欢迎大家踩std. 题目链接:LOJ 题目大意:定义函数 $f$:($minp$ 表示最小质因子) $$f(x)=\begin{cases}0&a ...

  5. 几句话总结一个算法之Policy Gradients

    强化学习与监督学习的区别在于,监督学习的每条样本都有一个独立的label,而强化学习的奖励(label)是有延后性,往往需要等这个回合结束才知道输赢 Policy Gradients(PG)计算某个状 ...

  6. Java8 新特性 默认方法

    默认方法为什么出现 默认方法的出现是因为在java8设计的过程中,因为加入了Lamdba表达式,和函数式接口,所以在非常多的接口里面要加入新的方法,但是如果在接口里面直接加入新的方法,那么以前写的所有 ...

  7. Java并发编程基础-Unsafe

    前言:Unsafe是Java中一个底层类,包含了很多基础的操作,比如数组操作.对象操作.内存操作.CAS操作.线程(park)操作.栅栏(Fence)操作,JUC包.一些三方框架都使用Unsafe类来 ...

  8. Django 安装使用

    Django 安装使用 Django 介绍: Django是一个开放源代码的Web应用框架,由Python写成.采用了MVT的框架模式,即模型M,视图V和模版T.它最初是被开发来用于管理劳伦斯出版集团 ...

  9. 基于Mybatis-Plus实现自动化操作创建时间和修改时间

    引入 在实际开发中,总会避免不了操作数据库,而在数据库中每个表都会有create_time和update_time字段记录操作时间,我们在操作这两个时间的时候也可能会出现不一致的情况,或者说这两个字段 ...

  10. B/S架构详解

    学习笔记:  * B/S架构详解 * 资源分类:            1. 静态资源:                * 使用静态网页开发技术发布的资源.                * 特点:  ...