J2CACHE 两级缓存框架
概述
缓存框架我们有ehcache 和 redis 分别是 本地内存缓存和 分布式缓存框架。在实际情况下如果单台机器 使用ehcache 就可以满足需求了,速度快效率高,有些数据如果需要多台机器共享这个时候怎么办呢,我们需要通过redis,将缓存存放到redis上面。
这也会导致一个问题,因为所有的请求都会到redis读取,当大量的读取会导致大量的网络流量,因此网络流量会成为访问的瓶颈。
J2CACHE就是解决这个问题而生的,缓存分为两级
L1: 进程内缓存(caffeine\ehcache)
L2: Redis/Memcached 集中式缓存
数据读取流程
读取顺序 -> L1 -> L2
缓存先读取L1 ,不存在则读取L2
数据更新
1 从数据库中读取最新数据,依次更新 L1 -> L2 ,发送广播清除某个缓存信息
2 接收到广播(手工清除缓存 & 一级缓存自动失效),从 L1 中清除指定的缓存信息
测试J2CACHE
创建一个maven 项目
引入
- <dependency><!-- Ehcache 3.x //-->
- <groupId>org.ehcache</groupId>
- <artifactId>ehcache</artifactId>
- <version>3.4.0</version>
- </dependency>
- <dependency>
- <groupId>net.oschina.j2cache</groupId>
- <artifactId>j2cache-core</artifactId>
- <version>2.7.7-release</version>
- </dependency>
引入配置文件
ehcache3.xml
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 = redis
- #########################################
- # 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 = ehcache3
- j2cache.L2.provider_class = redis
- # 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 = true
- #########################################
- # 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 = fst
- #json.map.person = net.oschina.j2cache.demo.Person
- #########################################
- # Ehcache configuration
- #########################################
- # ehcache.configXml = /ehcache.xml
- # ehcache3.configXml = /ehcache3.xml
- # ehcache3.defaultHeapSize = 1000
- #########################################
- # Redis connection configuration
- #########################################
- #########################################
- # Redis Cluster Mode
- #
- # single -> single redis server
- # sentinel -> master-slaves servers
- # cluster -> cluster servers (\u93c1\u7248\u5d41\u6434\u64bb\u53a4\u7f03\ue1bd\u68e4\u93c1\u582c\u7d1d\u6d63\u8de8\u6564 database = 0\u951b\ufffd
- # sharded -> sharded servers (\u7035\u55d9\u721c\u9286\u4f79\u669f\u93b9\ue1bc\u7c31\u8e47\u5474\u300f\u9366\ufffd hosts \u6d93\ue15f\u5bda\u7039\u6c3e\u7d1d\u6d93\u65c7\u7e5b\u93ba\u30e6\u775c\u95b0\u5d87\u7586\u93c3\u72b3\u6665 ; redis://user:password@127.0.0.1:6379/0\u951b\ufffd
- #
- #########################################
- redis.mode = sentinel
- #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 = mymaster
- ## 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 = 202.10.79.170:16001,202.10.79.170:16002,202.10.79.170:16003
- redis.timeout = 10000
- 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
广播方式使用redis
- j2cache.broadcast = redis
L1,L2 缓存实现方法
- j2cache.L1.provider_class = ehcache3
- j2cache.L2.provider_class = redis
redis 使用哨兵模式进行配置。
使用代码进行测试
编写测试代码如下:
读取缓存
- public static void main(String[] args) throws InterruptedException {
- CacheChannel cache = J2Cache.getChannel();
- while(true){
- System.out.println(cache.get("user", "name"));
- Thread.sleep(2000);
- }
- }
这个代码会一直读取 user:name 的缓存。
我们需要测试的是,当一个客户端设置一个 user:name 的缓存时,这个代码能反映缓存的变化。
设置缓存:
- public static void main(String[] args) {
- CacheChannel cache = J2Cache.getChannel();
- cache.set("user", "name", "A");
- cache.close();
- }
首先设置一个A的缓存。
我们可以看到第一个代码返回的是如下情况
[user,name,L2]=>A
[user,name,L1]=>A
[user,name,L1]=>A
它会先从 redis读取,在读取本地。
我们在设置一次:
- public static void main(String[] args) {
- CacheChannel cache = J2Cache.getChannel();
- cache.set("user", "name", "B");
- cache.close();
- }
第二次我们这个name 为 B,这个时候我们可以发现:
[user,name,L2]=>B
[user,name,L1]=>B
[user,name,L1]=>B
缓存变成了B ,而且是先读取 REDIS ,在读取 EHCACHE缓存,即先读取 L2 再读取L1。
这样我们的缓存就既能保证性能,有可以保证缓存及时被更新。
J2CACHE 两级缓存框架的更多相关文章
- Spring+ehcache+redis两级缓存
问题描述 场景:我们的应用系统是分布式集群的,可横向扩展的.应用中某个接口操作满足以下一个或多个条件: 1. 接口运行复杂代价大, 2. 接口返回数据量大, 3. 接口的数据基本不会更改, 4. 接口 ...
- 用guava快速打造两级缓存能力
首先,咱们都有一共识,即可以使用缓存来提升系统的访问速度! 现如今,分布式缓存这么强大,所以,大部分时候,我们可能都不会去关注本地缓存了! 而在一起高并发的场景,如果我们一味使用nosql式的缓存,如 ...
- Redis+Caffeine两级缓存,让访问速度纵享丝滑
原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 在高性能的服务架构设计中,缓存是一个不可或缺的环节.在实际的项目中,我们通常会将一些热点数据存储到Redis或MemCache这类缓存中间件中, ...
- 基于Spring接口,集成Caffeine+Redis两级缓存
原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 在上一篇文章Redis+Caffeine两级缓存,让访问速度纵享丝滑中,我们介绍了3种整合Caffeine和Redis作为两级缓存使用的方法,虽 ...
- J2Cache 和普通缓存框架有何不同,它解决了什么问题?
不少人看到 J2Cache 第一眼时,会认为这就是一个普普通通的缓存框架,和例如 Ehcache.Caffeine .Spring Cache 之类的项目没什么区别,无非是造了一个新的轮子而已.事实上 ...
- springboot中使用自定义两级缓存
工作中用到了springboot的缓存,使用起来挺方便的,直接引入redis或者ehcache这些缓存依赖包和相关缓存的starter依赖包,然后在启动类中加入@EnableCaching注解,然后在 ...
- mybatis两级缓存原理剖析
https://blog.csdn.net/zhurhyme/article/details/81064108 对于mybatis的缓存认识一直有一个误区,所以今天写一篇文章帮自己订正一下.mybat ...
- 阿里开源的缓存框架JetCache
之前一直在用Spring Cache进行接口数据的缓存,主要是Spring Cache在对具体key缓存失效时间的设置不是很方法,还要自己去扩展,无意中发现了阿里的JetCache.大部分的需求都能满 ...
- 数据库历险记(三) | 缓存框架的连环炮 数据库历险记(二) | Redis 和 Mecached 到底哪个好? 数据库历险记(一) | MySQL这么好,为什么还有人用Oracle? 面对海量请求,缓存设计还应该考虑哪些问题?
数据库历险记(三) | 缓存框架的连环炮 文章首发于微信公众号「陈树义」,专注于 Java 技术分享的社区.点击链接扫描二维码,与500位小伙伴一起共同进步.微信公众号二维码 http://p3n ...
随机推荐
- 铁板纹理 铁锈Rust
软件:Substance Designer 2017.1.2 这篇文章记录铁锈的制作方法,铁锈效果见图一 图一:铁锈Rust 铁锈的具体制作过程为: 使用BnW Spots 2(Noise)结点生成噪 ...
- udev example -- detect usb and write test file
之前学习了下Udev,就随便做了个测试小程序.....设计什么的也没考虑,就实现了一个基本功能,插入U盘,识别,循环检测到有特定文件后,就然后往U盘里面写数据,插拔多次,都能正常工作. 里面的warn ...
- 实验四:xl命令的常见子命令以及操作
实验名称: xl命令的常见子命令以及操作 实验环境: 这里我们需要正常安装一台虚拟机,如下图: 我们这里以一台busybox为例,来进行这些简单的常见的操作: 实验要求: 这里我们准备了5个常见操作: ...
- syslog-ng日志收集分析服务搭建及配置
syslog-ng日志收集分析服务搭建及配置:1.网上下载eventlog_0.2.12.tar.gz.libol-0.3.18.tar.gz.syslog-ng_3.3.5.tar.gz三个软件: ...
- Android 开发 框架系列 百度语音合成
官方文档:http://ai.baidu.com/docs#/TTS-Android-SDK/6d5d6899 官方百度语音合成控制台:https://cloud.baidu.com/product/ ...
- QTP 学习 - 参数化
- 面试回顾——kafka
关于消息队列的使用场景:https://www.cnblogs.com/linjiqin/p/5720865.html kafka: Topic Kafka将消息种子(Feed)分门别类 每一类的消息 ...
- jquery.cookie用法及其注意点
jquery.cookie是一个轻量级的cookie插件,由于已被封装好,可拿来即用. 基本的创建.读取.删除见另一篇文章 浅谈localStorage.sessionStorage 与cookie ...
- Android Room 学习(一)
Room简介 Room persistence库为SQLite提供了一个抽象层,以便在利用SQLite的全部功能的同时实现更强大的数据库访问. 该库可帮助您在运行应用程序的设备上创建应用程序数据的缓存 ...
- logback的使用和logback.xml详解,在Spring项目中使用log打印日志
logback的使用和logback.xml详解 一.logback的介绍 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分 ...