JFinal

登录 注册

JFinal 极速集成二级缓存 j2cache

项目集群的时候要用到二级缓存,趁着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不生效,已反馈,在以后版本修复了。


评论