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. Linux下进程间通信方式——pipe(管道)

    每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程A把数据从用户空间拷到内核缓冲区,进程B再从内核缓冲区把 ...

  2. CPU 的由来

    由 c# 的CEF 框架提供的 js 扩展,WebBrowser. JavascriptObjectRepository. 问:为什么要提供这一种方式. 提供了一种 能让js 与后端代码通讯的 方式. ...

  3. 关于sg函数的一些证明

    复习csp2019的时候稍微看了看博弈论,发现自己对于sg函数的理解完全不到位 有些定义甚至想都没想过 于是就口胡了一篇blog来安慰虚弱的自己 Question 1 对于一个满足拓扑性质的公平组合游 ...

  4. Android Studio 之 ROM【3】,LiveData+ViewModel+AsyncTask+Repository+RecyclerView

    教程地址:https://www.bilibili.com/video/av65180549 源码地址:https://github.com/longway777/Android-2019-Tutor ...

  5. 【视频开发】【计算机视觉】相机标定(Camera calibration)《二》

    简介 摄像机标定(Camera calibration)简单来说是从世界坐标系换到图像坐标系的过程,也就是求最终的投影矩阵 P 的过程,下面相关的部分主要参考UIUC的计算机视觉的课件(网址Sprin ...

  6. sqlcode与sqlerrm的区别

    sqlCode:是数据库操作的返回码,其中0--成功:-1--失败:100--没有检索到数据. sqlerrm:是数返回指定错误代码的错误信息. 在一个内在的异常中,SQLCODE返回Oracle错误 ...

  7. linux 远程文件复制和拉取

    基本命令格式 上传 scp -r  myfilder  tiantian@192.168.168.221:/home/tiantian/temp/ 复制本地文件到远程/home/tiantian/te ...

  8. 一文让你彻底理解准确率,精准率,召回率,真正率,假正率,ROC/AUC

    参考资料:https://zhuanlan.zhihu.com/p/46714763 ROC/AUC作为机器学习的评估指标非常重要,也是面试中经常出现的问题(80%都会问到).其实,理解它并不是非常难 ...

  9. javascript 忍者秘籍读书笔记

    书名 "学徒"=>"忍者" 性能分析 console.time('sss') console.timeEnd('sss') 函数 函数是第一类对象 通过字 ...

  10. Windows域的创建与加入教程

    一.说明 以前看书的时候时常看到“域”这个词,后来听一些渗透的演讲通常会说进入内网后首先要找域控.在认知中“域控”应该是域管理服务器一类的东西,但毕竟没有实现过所以还是不太懂长什么样. 前段时间朋友电 ...