jfinal 使用 redis 缓存数据

项目使用的框架是jfinal + vue  + iview ,  然后接口是c#  项目,由于c# 的 framework 的版本是 2.0 的,导致引用类名空间一直不兼容,做选择在java web  那边做缓存处理。

web  是jfinal  框架搭建的,具体的可以参照 jfinal 技术文档 http://www.jfinal.com/doc

 RedisPlugin是作为JFinal的Plugin而存在的,所以使用时需要在JFinalConfig中配置RedisPlugin,以下是RedisPlugin配置示例代码:

public void configPlugin(Plugins me) {
    //redisPlugin
		RedisPlugin redis = new RedisPlugin("user","127.0.0.1");
		me.add(redis);
	}

添加了上面的代码来创建user 的缓存,启动项目之后报错了:java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig

[WARN]-[Thread: main]-[org.eclipse.jetty.util.component.AbstractLifeCycle.setFailed()]: FAILED jfinal: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
    at com.sanusoft.ck.common.MainConfig.configPlugin(MainConfig.java:108)
    at com.jfinal.core.Config.configPluginWithOrder(Config.java:70)
    at com.jfinal.core.Config.configJFinal(Config.java:56)
    at com.jfinal.core.JFinal.init(JFinal.java:63)
    at com.jfinal.core.JFinalFilter.init(JFinalFilter.java:49)
    at org.eclipse.jetty.servlet.FilterHolder.doStart(FilterHolder.java:119)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:724)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:265)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:706)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:492)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
    at org.eclipse.jetty.server.Server.doStart(Server.java:277)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at com.jfinal.server.JettyServerForIDEA.doStart(JettyServerForIDEA.java:133)
    at com.jfinal.server.JettyServerForIDEA.start(JettyServerForIDEA.java:72)
    at com.jfinal.core.JFinal.start(JFinal.java:174)
    at com.sanusoft.ck.common.MainConfig.main(MainConfig.java:44)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 20 more

网上搜了一下,说是缺少commons-pool2 的引用,然后去https://mvnrepository.com/search?q=org.apache 搜索下maven 的引用加了


    org.apache.commons
    commons-pool2
    2.5.0

而后,然并卵。。还是有错误java.lang.ClassNotFoundException: redis.clients.jedis.JedisPoolConfig

[WARN]-[Thread: main]-[org.eclipse.jetty.util.component.AbstractLifeCycle.setFailed()]: FAILED jfinal: java.lang.NoClassDefFoundError: redis/clients/jedis/JedisPoolConfig
java.lang.NoClassDefFoundError: redis/clients/jedis/JedisPoolConfig
    at com.sanusoft.ck.common.MainConfig.configPlugin(MainConfig.java:108)
    at com.jfinal.core.Config.configPluginWithOrder(Config.java:70)
    at com.jfinal.core.Config.configJFinal(Config.java:56)
    at com.jfinal.core.JFinal.init(JFinal.java:63)
    at com.jfinal.core.JFinalFilter.init(JFinalFilter.java:49)
    at org.eclipse.jetty.servlet.FilterHolder.doStart(FilterHolder.java:119)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:724)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:265)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:706)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:492)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
    at org.eclipse.jetty.server.Server.doStart(Server.java:277)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at com.jfinal.server.JettyServerForIDEA.doStart(JettyServerForIDEA.java:133)
    at com.jfinal.server.JettyServerForIDEA.start(JettyServerForIDEA.java:72)
    at com.jfinal.core.JFinal.start(JFinal.java:174)
    at com.sanusoft.ck.common.MainConfig.main(MainConfig.java:44)
Caused by: java.lang.ClassNotFoundException: redis.clients.jedis.JedisPoolConfig
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 20 more

后面又加了一个依赖


redis.clients
jedis
2.9.0

这才把项目启动好,然后我也尝试着把commons-pool2 的引用给去掉,可以正常运行,这么说来就是少了jedis  的类库了。

然而并没有完全的解决掉问题,后面项目接入存取逻辑

Cache userCache = Redis.use("user");
userCache.setex("xu", 3600, "码农");//setex()可以设置过期时间,无需设置使用set()
userCache.get("xu");
						

运行到这还tm 报错了:java.lang.NoClassDefFoundError: org/nustaq/serialization/FSTObjectOutput

Caused by: java.lang.NoClassDefFoundError: org/nustaq/serialization/FSTObjectOutput
    at com.jfinal.plugin.redis.serializer.FstSerializer.valueToBytes(FstSerializer.java:54)
    at com.jfinal.plugin.redis.Cache.valueToBytes(Cache.java:1213)
    at com.jfinal.plugin.redis.Cache.set(Cache.java:66)
    at com.sanusoft.ck.common.sdk.erp.sanu.api.ErpVipApi.sendMsg(ErpVipApi.java:385)
    at com.sanusoft.ck.vip.CustomerController.msgHuif(CustomerController.java:411)
    ... 51 more

接而再引用下fst 依赖

		
			de.ruedigermoeller
			fst
			2.50
		

完美接入redis。

总的来说就是要引用两个依赖包,jedis 和 fst

that's all, thanks!

你可能感兴趣的:(java开发)