在Nutz项目中须要用到Redis,其实网上已经有不少例子的了,特别是Wendal官方cookbook还有示例,但这毕竟不想操做数据库那么通用,已经有成熟的Dao系列包可用,对应Redis丰富的数据结构,和各类应用场景,仍是须要本身作一些封装的,这里小结一下项目中的用法。html
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> </dependency>
使用maven导入的,也能够直接下载Jar包java
链接池是Jedis包内置的,池对象做为一个单例存在,固然能够直接new,保存在static变量里,但若是能够注入就更方便了,因此作了一个IoC配置文件:
redis
{ jedis_pool_config : { type : "redis.clients.jedis.JedisPoolConfig", fields : { maxTotal : 10, maxIdle : 5, maxWaitMillis : 30000, testOnBorrow : true } }, jedis_config:{ type: "com.nuanxinli.lib.cache.JedisConfig", fields:{ poolConfig:{ refer : "jedis_pool_config" }, port: 6379, host: "192.168.1.53", timeout: 30000, password:123456 } }, system_jedis_pooler : { type : "com.nuanxinli.lib.cache.JedisPooler", args : [ { refer : "jedis_config" }] } }
注意,JedisPool的API设计,是比较奇怪的,JedisPoolConfig是个配置实体类,里面却不包含host,port这些信息,要想设置IP和端口,只能在构造函数里传参,因此这里又定义了一个JedisConfig类,把须要的其余配置信息补充进去:
数据库
@IocBean public class JedisConfig { public JedisPoolConfig poolConfig; public String host; public Integer port; public String password; public Integer timeout; }
新建一个JedisPooler类,做为Pool单例的载体,本打算封装一个相似于Dao的类,后来发现Jedis由于有各类数据结构,方法太多,没法一一封装,仍是仅仅作了一个载体类
json
package com.nuanxinli.lib.cache; import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.json.Json; import org.nutz.log.Log; import org.nutz.log.Logs; import com.nuanxinli.util.StrUtil; import redis.clients.jedis.JedisPool; @IocBean(create = "getPool", depose = "onDepose") public class JedisPooler { private static final Log log = Logs.getLog(JedisPooler.class); private JedisPool pool; JedisConfig config; public JedisPooler(JedisConfig config) { this.config = config; } public synchronized JedisPool getPool() { log.debug("JedisPooler onCreate getPool..."); if (pool == null) { log.debug("即将链接redis:\r\n"+Json.toJson(config)); if (StrUtil.isNotNullOrBlank(config.password)) { pool = new JedisPool(config.poolConfig, config.host, config.port, config.timeout, config.password); } else { pool = new JedisPool(config.poolConfig, config.host, config.port, config.timeout); } } return pool; } public void onDepose() { pool.destroy(); } }
这样一个具体的业务类,在使用缓存时,注入JedisPooler对象就能够了,例如
缓存
@Inject("refer:system_jedis_pooler") private JedisPooler jedisPooler; public Double add(String username, String providerName, double point) { try (Jedis jedis = jedisPooler.getPool().getResource()) { return jedis.zincrby(KEY_PREFIX+":" + providerName, point, username); } }