JFinal 极速集成二级缓存 j2cache


声明:本文转载自https://my.oschina.net/u/1168934/blog/1603915,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

项目集群的时候要用到二级缓存,趁着@红薯 终于舍得更新j2cache了,极速集成一波!

代码地址:

https://gitee.com/xiaoxustudent/JFinal-vue-element-admin/tree/master/admin/src/main/java/com/sandu/j2cache

1.Maven引用:吐槽一下,真的很多jar,不过下面都是可选的,主要看你用什么,我这里直接从红薯代码https://gitee.com/ld/J2Cache/blob/master/core/pom.xml 里面复制过来了。

<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --> 		 		<dependency> 			<groupId>net.oschina.j2cache</groupId> 			<artifactId>j2cache-core</artifactId> 			<version>2.1.0</version> 		</dependency> 		<dependency> 			<groupId>net.sf.ehcache</groupId> 			<artifactId>ehcache</artifactId> 			<version>2.10.4</version> 		</dependency>  		<dependency> 			<groupId>org.ehcache</groupId> 			<artifactId>ehcache</artifactId> 			<version>3.4.0</version> 		</dependency>  		<dependency> 			<groupId>com.github.ben-manes.caffeine</groupId> 			<artifactId>caffeine</artifactId> 			<version>2.6.1</version> 		</dependency>  		<dependency> 			<groupId>redis.clients</groupId> 			<artifactId>jedis</artifactId> 			<version>2.9.0</version> 			<type>jar</type> 			<scope>compile</scope> 		</dependency>  		<dependency> 			<groupId>org.jgroups</groupId> 			<artifactId>jgroups</artifactId> 			<version>3.6.13.Final</version> 		</dependency>  		<dependency> 			<groupId>de.ruedigermoeller</groupId> 			<artifactId>fst</artifactId> 			<version>2.53</version> 		</dependency>  		<dependency> 			<groupId>com.esotericsoftware</groupId> 			<artifactId>kryo-shaded</artifactId> 			<version>3.0.0</version> 		</dependency>  		<dependency> 			<groupId>org.xerial.snappy</groupId> 			<artifactId>snappy-java</artifactId> 			<version>1.1.4</version> 		</dependency>  		<dependency> 			<groupId>commons-beanutils</groupId> 			<artifactId>commons-beanutils</artifactId> 			<version>1.9.3</version> 		</dependency>

2.写个JFinal Plugin

package com.sandu.j2cache;  import com.jfinal.plugin.IPlugin;  import net.oschina.j2cache.CacheChannel; import net.oschina.j2cache.J2Cache;  public class J2CachePlugin implements IPlugin {     private static CacheChannel cache;      @Override     public boolean start() {         System.setProperty("java.net.preferIPv4Stack", "true"); //Disable IPv6 in JVM         cache = J2Cache.getChannel();         J2CacheKit.init(cache);         return true;     }      @Override     public boolean stop() {         cache.close();         return true;     } }

cache = J2Cache.getChannel() 拿到这个cache就完事了,可以调用各种api,这里我参考JFinal的cacheKit
小小封装了一个J2CacheKit,

3.配置文件

j2cache.properties  //这个必须的,里面有各种配置,我L1用了ehcache,详情看代码

j2cache.L1.provider_class = ehcache
j2cache.L2.provider_class = redis

network.xml //广播用的,不用管

ehcache.xml //ehcache配置文件,如果你用ehcache3,就用ehcache3.xml

4.测试,先开了redis服务

package com.sandu.j2cache;  import java.io.IOException;  import net.oschina.j2cache.J2Cache;  public class J2CacheTest { 	 	public static void main(String[] args) throws IOException { 		new J2CachePlugin().start(); 		String str1 = "高阁客竟去,小园花乱飞。参差连曲陌,迢递送斜晖。"; 		String str2 = "肠断未忍扫,眼穿仍欲归。芳心向春尽,所得是沾衣。"; 		String key1 = "ab1";  		String key2 = "ab2";  		String cacheName = "example"; 		 		//读写 		System.out.println("写入:" + str1); 		System.out.println("写入:" + str2); 		 		J2CacheKit.put(cacheName, key1, str1); 		J2CacheKit.put(cacheName, key2, str2); 		 		System.out.println("获取key1:" + J2CacheKit.get(cacheName, key1)); 		System.out.println("获取key2:" + J2CacheKit.get(cacheName, key2)); 		 //		//删除 		J2CacheKit.remove(cacheName, key1); 		System.out.println("删除:" + key1); 		System.out.println("删除后输出:"+key1 +  J2CacheKit.get(cacheName, key1)); 		System.out.println("删除后输出:"+key2 +  J2CacheKit.get(cacheName, key2)); //		 		//删除全部 		J2CacheKit.removeAll(cacheName); 		J2Cache.getChannel().clear(cacheName); 		System.out.println("删除全部数据:"); 		System.out.println("删除后输出:"+key1 +  J2CacheKit.get(cacheName, key1)); 		System.out.println("删除后输出:"+key2 +  J2CacheKit.get(cacheName, key2)); 	}  } 
[main] INFO net.oschina.j2cache.J2Cache - Load J2Cache Config File : [/j2cache.properties]. [main] INFO net.oschina.j2cache.CacheProviderHolder - Using L1 CacheProvider : net.oschina.j2cache.ehcache.EhCacheProvider [main] INFO net.oschina.j2cache.CacheProviderHolder - Using L2 CacheProvider : net.oschina.j2cache.redis.RedisCacheProvider [main] INFO net.oschina.j2cache.redis.RedisPubSubClusterPolicy - Connected to redis channel:j2cache, time 74 ms. [main] INFO net.oschina.j2cache.J2Cache - Using cluster policy : net.oschina.j2cache.redis.RedisPubSubClusterPolicy 写入:高阁客竟去,小园花乱飞。参差连曲陌,迢递送斜晖。 写入:肠断未忍扫,眼穿仍欲归。芳心向春尽,所得是沾衣。 [main] INFO net.oschina.j2cache.util.SerializationUtils - Using Serializer -> [fst:net.oschina.j2cache.util.FSTSerializer] 获取key1:高阁客竟去,小园花乱飞。参差连曲陌,迢递送斜晖。 获取key2:肠断未忍扫,眼穿仍欲归。芳心向春尽,所得是沾衣。 删除:ab1 删除后输出:ab1null 删除后输出:ab2肠断未忍扫,眼穿仍欲归。芳心向春尽,所得是沾衣。 删除全部数据: 删除后输出:ab1null 删除后输出:ab2null 

5.总结

真正的代码只有J2CachePlugin和额外封装的J2CacheKit,另外j2cache 2.0版本 caffeine缓存下clear不生效,已反馈,在以后版本修复了。

本文发表于2018年01月08日 16:36
(c)注:本文转载自https://my.oschina.net/u/1168934/blog/1603915,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 2375 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1