Ubuntu 上 安装 redis
参见
http://segmentfault.com/a/1190000004109484html
jfinal 2.0 中已集成RedisPlugin插件。java
RedisPlugin 是支持 Redis 的极速化插件。使用 RedisPlugin 能够极度方便的使用 redis,该插件不只提供了丰富的 API,并且还同时支持多 redis 服务端。Redis 拥有超高的性能,丰富的数据结构,自然支持数据持久化,是目前应用很是普遍的 nosql 数据库。对于 redis 的有效应用可极大提高系统性能,节省硬件成本。redis
RedisPlugin 是做为 JFinal 的 Plugin 而存在的,因此使用时须要在 JFinalConfig 中配置
RedisPlugin,如下是 RedisPlugin 配置示例代码:sql
@Override public void configPlugin(Plugins plugins){ //缓存user模块 到 redis RedisPlugin userRedis=new RedisPlugin("userCache","172.16.0.102","test123"); plugins.add(userRedis); }
以上代码建立了一个 RedisPlugin 对象userRedis。最早建立的RedisPlugin 对象所持有的 Cache 对象将成为主缓存对象,主缓存对象可经过 Redis.use()直接获取,不然须要提供 cacheName 参数才能获取,例如:Redis.use(“other”)数据库
Redis 与 Cache 联合起来能够很是方便地使用 Redis 服务,Redis 对象经过 use()方法来获取到 Cache 对象,Cache 对象提供了丰富的 API 用于使用 Redis 服务,下面是具体使用示例:segmentfault
cache赋值缓存
package controller; import com.jfinal.core.Controller; import com.jfinal.plugin.redis.Cache; import com.jfinal.plugin.redis.Redis; /** * Created by renpeng on 2015/12/1. */ public class IndexController extends Controller { public void index(){ //获取redis 缓存对象user Cache userCache= Redis.use("userCache"); //在config中最早建立的cache 可使用Redis.use() 直接来获取。示例: //Cache userCache= Redis.use(); userCache.set("userid_1","admin"); System.out.print("index.jsp: cache set 完成#############################"); renderJsp("index.jsp"); } }
运行效果:数据结构
JFinal action report -------- 2015-12-08 10:37:23 ------------------------------ Controller : controller.IndexController.(IndexController.java:1) Method : index Interceptor : interceptor.ExceptionIntoLogInterceptor.(ExceptionIntoLogInterceptor.java:1) -------------------------------------------------------------------------------- index.jsp: cache set 完成#############################
cache取值nosql
@Before(AuthInterceptor.class) public void index(){ //System.out.print(getParaToInt()+"====================="); //setAttr("user", User.me.findById(4)); Cache userCache= Redis.use("userCache"); System.out.print("# user/index: cache get 内容:"+userCache.get("userid_1")); setAttr("userPage", User.me.paginate(getParaToInt(0, 1), 10)); render("index.html"); }
运行结果:jsp
JFinal action report -------- 2015-12-08 10:38:20 ------------------------------ Controller : controller.UserController.(UserController.java:1) Method : index Interceptor : interceptor.ExceptionIntoLogInterceptor.(ExceptionIntoLogInterceptor.java:1) interceptor.AuthInterceptor.(AuthInterceptor.java:1) -------------------------------------------------------------------------------- # user/index: cache get 内容:admin
注:一般状况下只会建立一个 RedisPlugin 链接一个 redis 服务端,使用 Redis.use().set(key,value)便可。