Java中使用Jedis操作Redis(转载)
整理
1. 字符串 添加:set keyname value 查询:get keyname 拼接:append keyname value 删除:del keyname 添加多个: mset keyname1 value1 keyname2 value2 ... 对值进行+1 操作:incr keyname2.set:
添加:sadd keyname value
删除:srem keyname value
查询所有的值:smembers keyname
判断 who 是否是 user 集合的元素:sismember keyname value
随机取值:srandmember keyname
集合数量:scard keyname3.map
添加:hmset keymame map
查询:hmget keyname mapkey [...key 可以拼接多个]
删除某个键值:hdel keyname mapkey
查询某个 map 的键个数:hlen keyname
查询某个 map 中是否存在:exists keyname
查询 map 中的所有 key:hkeys keyname
查询 mpa 中的所有 value:hvals keyname4.list
添加:lpush keyname value
rpush keyname value (有序追加)
范围取出:lrange keyname startindex endindex [endindex=-1 时查询起点之后的所有 ]
删除:del keyname
http://www.cnblogs.com/liuling/p/2014-4-19-04.html
使用 Java 操作 Redis 需要 jedis-2.1.0.jar,下载地址:https://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip
如果需要使用 Redis 连接池的话,还需 commons-pool-1.5.4.jar,下载地址:https://files.cnblogs.com/liuling/commons-pool-1.5.4.jar.zip
package com.test;import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;import org.junit.Before;
import org.junit.Test;import redis.clients.jedis.Jedis;
public class TestRedis {
private Jedis jedis;jedis.append("name", "is my lover"); //拼接 System.out.println(jedis.get("name"));@Before </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setup() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">连接redis服务器,192.168.0.100:6379</span> jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("192.168.0.100", 6379<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">权限认证</span> jedis.auth("admin"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * redis存储字符串 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testString() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">-----添加数据---------- </span> jedis.set("name","xinxin");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">向key-->name中放入了value-->xinxin </span> System.out.println(jedis.get("name"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行结果:xinxin </span>
Iterator<String> iter=jedis.hkeys("user").iterator(); while (iter.hasNext()){ String key = iter.next(); System.out.println(key+":"+jedis.hmget("user",key));} }jedis.del(</span>"name"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除某个键</span> System.out.println(jedis.get("name"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置多个键值对</span> jedis.mset("name","liuling","age","23","qq","476777XXX"<span style="color: rgba(0, 0, 0, 1)">); jedis.incr(</span>"age"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">进行加1操作</span> System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq"<span style="color: rgba(0, 0, 0, 1)">)); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * redis操作Map </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testMap() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">-----添加数据---------- </span> Map<String, String> map = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap<String, String><span style="color: rgba(0, 0, 0, 1)">(); map.put(</span>"name", "xinxin"<span style="color: rgba(0, 0, 0, 1)">); map.put(</span>"age", "22"<span style="color: rgba(0, 0, 0, 1)">); map.put(</span>"qq", "123456"<span style="color: rgba(0, 0, 0, 1)">); jedis.hmset(</span>"user"<span style="color: rgba(0, 0, 0, 1)">,map); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取出user中的name,执行结果:[minxr]-->注意结果是一个泛型的List </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第一个参数是存入redis中map对象的key,后面跟的是放入map中的对象的key,后面的key可以跟多个,是可变参数 </span> List<String> rsmap = jedis.hmget("user", "name", "age", "qq"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(rsmap); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除map中的某个键值 </span> jedis.hdel("user","age"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.hmget(</span>"user", "age")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">因为删除了,所以返回的是null </span> System.out.println(jedis.hlen("user")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回key为user的键中存放的值的个数2 </span> System.out.println(jedis.exists("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否存在key为user的记录 返回true </span> System.out.println(jedis.hkeys("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回map对象中的所有key </span> System.out.println(jedis.hvals("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回map对象中的所有value </span>
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * jedis操作List </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testList(){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">开始前,先移除所有的内容 </span> jedis.del("java framework"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"java framework",0,-1<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">先向key java framework中存放三条数据 </span> jedis.lpush("java framework","spring"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"java framework","struts"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"java framework","hibernate"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">再取出所有数据jedis.lrange是按范围取出, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有 </span> System.out.println(jedis.lrange("java framework",0,-1<span style="color: rgba(0, 0, 0, 1)">)); jedis.del(</span>"java framework"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"java framework","spring"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"java framework","struts"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"java framework","hibernate"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"java framework",0,-1<span style="color: rgba(0, 0, 0, 1)">)); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * jedis操作Set </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSet(){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加 </span> jedis.sadd("user","liuling"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"user","xinxin"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"user","ling"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"user","zhangxinxin"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"user","who"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">移除noname </span> jedis.srem("user","who"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.smembers(</span>"user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取所有加入的value </span> System.out.println(jedis.sismember("user", "who"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断 who 是否是user集合的元素 </span> System.out.println(jedis.srandmember("user"<span style="color: rgba(0, 0, 0, 1)">)); //随机从set中取一个值 System.out.println(jedis.scard(</span>"user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回集合的元素个数 </span>
}
@Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> test() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InterruptedException { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">jedis 排序 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的) </span> jedis.del("a");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">先清除数据,再加入数据进行测试 </span> jedis.rpush("a", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a","6"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a","3"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a","9"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"a",0,-1));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [9, 3, 6, 1] </span> System.out.println(jedis.sort("a")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[1, 3, 6, 9] </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输入排序后结果 </span> System.out.println(jedis.lrange("a",0,-1<span style="color: rgba(0, 0, 0, 1)">)); } @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testRedisPool() { RedisUtil.getJedis().set(</span>"newname", "中文测试"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(RedisUtil.getJedis().get(</span>"newname"<span style="color: rgba(0, 0, 0, 1)">)); }
}
Redis 连接池:
package com.test;import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;public final class RedisUtil {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Redis服务器IP</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> String ADDR = "192.168.0.100"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Redis的端口号</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> PORT = 6379<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">访问密码</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> String AUTH = "admin"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">可用连接实例的最大数目,默认值为8; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> MAX_ACTIVE = 1024<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> MAX_IDLE = 200<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> MAX_WAIT = 10000<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> TIMEOUT = 10000<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span> TEST_ON_BORROW = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> JedisPool jedisPool = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 初始化Redis连接池 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { JedisPoolConfig config </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig(); config.setMaxActive(MAX_ACTIVE); config.setMaxIdle(MAX_IDLE); config.setMaxWait(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); jedisPool </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPool(config, ADDR, PORT, TIMEOUT, AUTH); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 获取Jedis实例 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Jedis getJedis() { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (jedisPool != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { Jedis resource </span>=<span style="color: rgba(0, 0, 0, 1)"> jedisPool.getResource(); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> resource; } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; } } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 释放jedis资源 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jedis </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> returnResource(<span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> Jedis jedis) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (jedis != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { jedisPool.returnResource(jedis); } }
}
package com.wujintao.redis;import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.SortingParams;import com.wujintao.redis.util.RedisUtil;
public class TestCase {
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 在不同的线程中使用相同的Jedis实例会发生奇怪的错误。但是创建太多的实现也不好因为这意味着会建立很多sokcet连接, * 也会导致奇怪的错误发生。单一Jedis实例不是线程安全的。为了避免这些问题,可以使用JedisPool, * JedisPool是一个线程安全的网络连接池。可以用JedisPool创建一些可靠Jedis实例,可以从池中拿到Jedis的实例。 * 这种方式可以解决那些问题并且会实现高效的性能 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ...when closing your application: </span>
RedisUtil.getPool().destroy();
} </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> Hello() { Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 向key-->name中放入了value-->minxr </span> jedis.set("name", "minxr"<span style="color: rgba(0, 0, 0, 1)">); String ss </span>= jedis.get("name"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(ss); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 很直观,类似map 将jintao append到已经有的value之后 </span> jedis.append("name", "jintao"<span style="color: rgba(0, 0, 0, 1)">); ss </span>= jedis.get("name"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(ss); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2、直接覆盖原来的数据 </span> jedis.set("name", "jintao"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.get(</span>"jintao"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除key对应的记录 </span> jedis.del("name"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.get(</span>"name"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 执行结果:null </span> <span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * mset相当于 jedis.set("name","minxr"); jedis.set("jarorwar","aaa"); </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> jedis.mset(</span>"name", "minxr", "jarorwar", "aaa"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.mget(</span>"name", "jarorwar"<span style="color: rgba(0, 0, 0, 1)">)); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { RedisUtil.getPool().returnResource(jedis); } } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testKey() { Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); System.out.println(</span>"=============key=========================="<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清空数据 </span>
System.out.println(jedis.flushDB());
System.out.println(jedis.echo("foo"));
// 判断 key 否存在
System.out.println(jedis.exists("foo"));
jedis.set("key", "values");
System.out.println(jedis.exists("key"));
}</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testString() { System.out.println(</span>"==String=="<span style="color: rgba(0, 0, 0, 1)">); Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> String </span> jedis.set("key", "Hello World!"<span style="color: rgba(0, 0, 0, 1)">); String value </span>= jedis.get("key"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(value); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { RedisUtil.getPool().returnResource(jedis); } System.out.println(</span>"=============String=========================="<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清空数据 </span>
System.out.println(jedis.flushDB());
// 存储数据
jedis.set("foo", "bar");
System.out.println(jedis.get("foo"));
// 若 key 不存在,则存储
jedis.setnx("foo", "foo not exits");
System.out.println(jedis.get("foo"));
// 覆盖数据
jedis.set("foo", "foo update");
System.out.println(jedis.get("foo"));
// 追加数据
jedis.append("foo", "hello, world");
System.out.println(jedis.get("foo"));
// 设置 key 的有效期,并存储数据
jedis.setex("foo", 2, "foo not exits");
System.out.println(jedis.get("foo"));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
System.out.println(jedis.get("foo"));
// 获取并更改数据
jedis.set("foo", "foo update");
System.out.println(jedis.getSet("foo", "foo modify"));
// 截取 value 的值
System.out.println(jedis.getrange("foo", 1, 3));
System.out.println(jedis.mset("mset1", "mvalue1", "mset2", "mvalue2",
"mset3", "mvalue3", "mset4", "mvalue4"));
System.out.println(jedis.mget("mset1", "mset2", "mset3", "mset4"));
System.out.println(jedis.del(new String[] { "foo", "foo1", "foo3" }));
}</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testList() { System.out.println(</span>"==List=="<span style="color: rgba(0, 0, 0, 1)">); Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 开始前,先移除所有的内容 </span> jedis.del("messages"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"messages", "Hello how are you?"<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"messages", "Fine thanks. I'm having fun with redis."<span style="color: rgba(0, 0, 0, 1)">); jedis.rpush(</span>"messages", "I should look into this NOSQL thing ASAP"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 再取出所有数据jedis.lrange是按范围取出, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有 </span> List<String> values = jedis.lrange("messages", 0, -1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(values); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { RedisUtil.getPool().returnResource(jedis); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清空数据 </span>
System.out.println(jedis.flushDB());
// 添加数据
jedis.lpush("lists", "vector");
jedis.lpush("lists", "ArrayList");
jedis.lpush("lists", "LinkedList");
// 数组长度
System.out.println(jedis.llen("lists"));
// 排序
System.out.println(jedis.sort("lists"));
// 字串
System.out.println(jedis.lrange("lists", 0, 3));
// 修改列表中单个值
jedis.lset("lists", 0, "hello list!");
// 获取列表指定下标的值
System.out.println(jedis.lindex("lists", 1));
// 删除列表指定下标的值
System.out.println(jedis.lrem("lists", 1, "vector"));
// 删除区间以外的数据
System.out.println(jedis.ltrim("lists", 0, 1));
// 列表出栈
System.out.println(jedis.lpop("lists"));
// 整个列表值
System.out.println(jedis.lrange("lists", 0, -1));
}</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSet() { System.out.println(</span>"==Set=="<span style="color: rgba(0, 0, 0, 1)">); Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { jedis.sadd(</span>"myset", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"myset", "2"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"myset", "3"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"myset", "4"<span style="color: rgba(0, 0, 0, 1)">); Set</span><String> setValues = jedis.smembers("myset"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(setValues); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 移除noname </span> jedis.srem("myset", "4"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.smembers(</span>"myset"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取所有加入的value </span> System.out.println(jedis.sismember("myset", "4"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断 minxr </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否是sname集合的元素 </span> System.out.println(jedis.scard("sname"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回集合的元素个数 </span> } <span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { RedisUtil.getPool().returnResource(jedis); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清空数据 </span>
System.out.println(jedis.flushDB());
// 添加数据
jedis.sadd("sets", "HashSet");
jedis.sadd("sets", "SortedSet");
jedis.sadd("sets", "TreeSet");
// 判断 value 是否在列表中
System.out.println(jedis.sismember("sets", "TreeSet"));
;
// 整个列表值
System.out.println(jedis.smembers("sets"));
// 删除指定元素
System.out.println(jedis.srem("sets", "SortedSet"));
// 出栈
System.out.println(jedis.spop("sets"));
System.out.println(jedis.smembers("sets"));
//
jedis.sadd("sets1", "HashSet1");
jedis.sadd("sets1", "SortedSet1");
jedis.sadd("sets1", "TreeSet");
jedis.sadd("sets2", "HashSet2");
jedis.sadd("sets2", "SortedSet1");
jedis.sadd("sets2", "TreeSet1");
// 交集
System.out.println(jedis.sinter("sets1", "sets2"));
// 并集
System.out.println(jedis.sunion("sets1", "sets2"));
// 差集
System.out.println(jedis.sdiff("sets1", "sets2"));
}</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> sortedSet() { System.out.println(</span>"==SoretedSet=="<span style="color: rgba(0, 0, 0, 1)">); Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { jedis.zadd(</span>"hackers", 1940, "Alan Kay"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"hackers", 1953, "Richard Stallman"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"hackers", 1965, "Yukihiro Matsumoto"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"hackers", 1916, "Claude Shannon"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"hackers", 1969, "Linus Torvalds"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"hackers", 1912, "Alan Turing"<span style="color: rgba(0, 0, 0, 1)">); Set</span><String> setValues = jedis.zrange("hackers", 0, -1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(setValues); Set</span><String> setValues2 = jedis.zrevrange("hackers", 0, -1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(setValues2); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { RedisUtil.getPool().returnResource(jedis); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清空数据 </span>
System.out.println(jedis.flushDB());
// 添加数据
jedis.zadd("zset", 10.1, "hello");
jedis.zadd("zset", 10.0, ":");
jedis.zadd("zset", 9.0, "zset");
jedis.zadd("zset", 11.0, "zset!");
// 元素个数
System.out.println(jedis.zcard("zset"));
// 元素下标
System.out.println(jedis.zscore("zset", "zset"));
// 集合子集
System.out.println(jedis.zrange("zset", 0, -1));
// 删除元素
System.out.println(jedis.zrem("zset", "zset!"));
System.out.println(jedis.zcount("zset", 9.5, 10.5));
// 整个集合值
System.out.println(jedis.zrange("zset", 0, -1));
}Iterator<String> iter = jedis.hkeys("kid").iterator(); while (iter.hasNext()) { String key = iter.next(); System.out.println(key + ":" + jedis.hmget("kid", key));}</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testHsh() { System.out.println(</span>"==Hash=="<span style="color: rgba(0, 0, 0, 1)">); Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { Map</span><String, String> pairs = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap<String, String><span style="color: rgba(0, 0, 0, 1)">(); pairs.put(</span>"name", "Akshi"<span style="color: rgba(0, 0, 0, 1)">); pairs.put(</span>"age", "2"<span style="color: rgba(0, 0, 0, 1)">); pairs.put(</span>"sex", "Female"<span style="color: rgba(0, 0, 0, 1)">); jedis.hmset(</span>"kid"<span style="color: rgba(0, 0, 0, 1)">, pairs); List</span><String> name = jedis.hmget("kid", "name");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 结果是个泛型的LIST </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> jedis.hdel("kid","age"); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除map中的某个键值 </span> System.out.println(jedis.hmget("kid", "pwd")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 因为删除了,所以返回的是null </span> System.out.println(jedis.hlen("kid")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回key为user的键中存放的值的个数 </span> System.out.println(jedis.exists("kid"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否存在key为user的记录 </span> System.out.println(jedis.hkeys("kid"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回map对象中的所有key </span> System.out.println(jedis.hvals("kid"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回map对象中的所有value </span>
List</span><String> values = jedis.lrange("messages", 0, -1<span style="color: rgba(0, 0, 0, 1)">); values </span>= jedis.hmget("kid", <span style="color: rgba(0, 0, 255, 1)">new</span> String[] { "name", "age", "sex"<span style="color: rgba(0, 0, 0, 1)"> }); System.out.println(values); Set</span><String> setValues = jedis.zrange("hackers", 0, -1<span style="color: rgba(0, 0, 0, 1)">); setValues </span>= jedis.hkeys("kid"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(setValues); values </span>= jedis.hvals("kid"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(values); pairs </span>= jedis.hgetAll("kid"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(pairs); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { RedisUtil.getPool().returnResource(jedis); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 清空数据 </span>
System.out.println(jedis.flushDB());
// 添加数据
jedis.hset("hashs", "entryKey", "entryValue");
jedis.hset("hashs", "entryKey1", "entryValue1");
jedis.hset("hashs", "entryKey2", "entryValue2");
// 判断某个值是否存在
System.out.println(jedis.hexists("hashs", "entryKey"));
// 获取指定的值
System.out.println(jedis.hget("hashs", "entryKey")); // 批量获取指定的值
System.out.println(jedis.hmget("hashs", "entryKey", "entryKey1"));
// 删除指定的值
System.out.println(jedis.hdel("hashs", "entryKey"));
// 为 key 中的域 field 的值加上增量 increment
System.out.println(jedis.hincrBy("hashs", "entryKey", 123l));
// 获取所有的 keys
System.out.println(jedis.hkeys("hashs"));
// 获取所有的 values
System.out.println(jedis.hvals("hashs"));
}</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> testOther() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InterruptedException { Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> keys中传入的可以用通配符 </span> System.out.println(jedis.keys("*")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回当前库中所有的key [sose, sanme, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> name, jarorwar, foo, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> sname, java framework, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> user, braand] </span> System.out.println(jedis.keys("*name"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回的sname [sname, name] </span> System.out.println(jedis.del("sanmdde"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除key为sanmdde的对象 删除成功返回1 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除失败(或者不存在)返回 0 </span> System.out.println(jedis.ttl("sname"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回给定key的有效时间,如果是-1则表示永远有效 </span> jedis.setex("timekey", 10, "min");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通过此方法,可以指定key的存活(有效时间) 时间为秒 </span> Thread.sleep(5000);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 睡眠5秒后,剩余时间将为<=5 </span> System.out.println(jedis.ttl("timekey")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 输出结果为5 </span> jedis.setex("timekey", 1, "min"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设为1后,下面再看剩余时间就是1了 </span> System.out.println(jedis.ttl("timekey")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 输出结果为1 </span> System.out.println(jedis.exists("key"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 检查key是否存在 </span> System.out.println(jedis.rename("timekey", "time"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(jedis.get(</span>"timekey"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 因为移除,返回为null </span> System.out.println(jedis.get("time")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 因为将timekey 重命名为time </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 所以可以取得值 min </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> jedis 排序 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的) </span> jedis.del("a");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 先清除数据,再加入数据进行测试 </span> jedis.rpush("a", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a", "6"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a", "3"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"a", "9"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.lrange(</span>"a", 0, -1));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [9, 3, 6, 1] </span> System.out.println(jedis.sort("a")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [1, 3, 6, 9] </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输入排序后结果 </span> System.out.println(jedis.lrange("a", 0, -1<span style="color: rgba(0, 0, 0, 1)">)); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> { RedisUtil.getPool().returnResource(jedis); } } @org.junit.Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testUnUsePipeline() { </span><span style="color: rgba(0, 0, 255, 1)">long</span> start = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date().getTime(); Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < 10000; i++<span style="color: rgba(0, 0, 0, 1)">) { jedis.set(</span>"age1" + i, i + ""<span style="color: rgba(0, 0, 0, 1)">); jedis.get(</span>"age1" + i);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 每个操作都发送请求给redis-server </span>
}
long end = new Date().getTime();System.out.println(</span>"unuse pipeline cost:" + (end - start) + "ms"<span style="color: rgba(0, 0, 0, 1)">); RedisUtil.getPool().returnResource(jedis); } @org.junit.Test </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 参考:</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">http://blog.csdn.net/freebird_lb/article/details/7778919</span> <span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testUsePipeline() { </span><span style="color: rgba(0, 0, 255, 1)">long</span> start = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date().getTime(); Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); jedis.flushDB(); Pipeline p </span>=<span style="color: rgba(0, 0, 0, 1)"> jedis.pipelined(); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < 10000; i++<span style="color: rgba(0, 0, 0, 1)">) { p.set(</span>"age2" + i, i + ""<span style="color: rgba(0, 0, 0, 1)">); System.out.println(p.get(</span>"age2" +<span style="color: rgba(0, 0, 0, 1)"> i)); } p.sync();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 这段代码获取所有的response </span> <span style="color: rgba(0, 0, 255, 1)">long</span> end = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date().getTime(); System.out.println(</span>"use pipeline cost:" + (end - start) + "ms"<span style="color: rgba(0, 0, 0, 1)">); RedisUtil.getPool().returnResource(jedis); } @org.junit.Test </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 时间复杂度: O(N+M*log(M)), N 为要排序的列表或集合内的元素数量, M 为要返回的元素数量。 如果只是使用 SORT 命令的 GET 选项获取数据而没有进行排序,时间复杂度 O(N)。 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSort1() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 排序默认以数字作为对象,值被解释为双精度浮点数,然后进行比较 </span> Jedis redis =<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 一般SORT用法 最简单的SORT使用方法是SORT key。 </span> redis.lpush("mylist", "1"<span style="color: rgba(0, 0, 0, 1)">); redis.lpush(</span>"mylist", "4"<span style="color: rgba(0, 0, 0, 1)">); redis.lpush(</span>"mylist", "6"<span style="color: rgba(0, 0, 0, 1)">); redis.lpush(</span>"mylist", "3"<span style="color: rgba(0, 0, 0, 1)">); redis.lpush(</span>"mylist", "0"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> List<String> list = redis.sort("sort");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 默认是升序 </span> SortingParams sortingParameters = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SortingParams(); sortingParameters.desc(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> sortingParameters.alpha();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当数据集中保存的是字符串值时,你可以用 ALPHA </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修饰符(modifier)进行排序。 </span> sortingParameters.limit(0, 2);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 可用于分页查询 </span> List<String> list = redis.sort("mylist", sortingParameters);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 默认是升序 </span> <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < list.size(); i++<span style="color: rgba(0, 0, 0, 1)">) { System.out.println(list.get(i)); } redis.flushDB(); RedisUtil.closeJedis(redis); } @org.junit.Test </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * sort list * LIST结合hash的排序 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSort2() { Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); jedis.del(</span>"user:66", "user:55", "user:33", "user:22", "user:11"<span style="color: rgba(0, 0, 0, 1)">, </span>"userlist"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"userlist", "33"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"userlist", "22"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"userlist", "55"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"userlist", "11"<span style="color: rgba(0, 0, 0, 1)">); jedis.hset(</span>"user:66", "name", "66"<span style="color: rgba(0, 0, 0, 1)">); jedis.hset(</span>"user:55", "name", "55"<span style="color: rgba(0, 0, 0, 1)">); jedis.hset(</span>"user:33", "name", "33"<span style="color: rgba(0, 0, 0, 1)">); jedis.hset(</span>"user:22", "name", "79"<span style="color: rgba(0, 0, 0, 1)">); jedis.hset(</span>"user:11", "name", "24"<span style="color: rgba(0, 0, 0, 1)">); jedis.hset(</span>"user:11", "add", "beijing"<span style="color: rgba(0, 0, 0, 1)">); jedis.hset(</span>"user:22", "add", "shanghai"<span style="color: rgba(0, 0, 0, 1)">); jedis.hset(</span>"user:33", "add", "guangzhou"<span style="color: rgba(0, 0, 0, 1)">); jedis.hset(</span>"user:55", "add", "chongqing"<span style="color: rgba(0, 0, 0, 1)">); jedis.hset(</span>"user:66", "add", "xi'an"<span style="color: rgba(0, 0, 0, 1)">); SortingParams sortingParameters </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SortingParams(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 符号 "->" 用于分割哈希表的键名(key name)和索引域(hash field),格式为 "key->field" 。 </span> sortingParameters.get("user:*->name"<span style="color: rgba(0, 0, 0, 1)">); sortingParameters.get(</span>"user:*->add"<span style="color: rgba(0, 0, 0, 1)">);
// sortingParameters.by("user:->name");
// sortingParameters.get("#");
List<String> result = jedis.sort("userlist", sortingParameters);
for (String item : result) {
System.out.println("item...." + item);
}
/**
* 对应的 redis 客户端命令是:sort ml get user->name sort ml get user:->name get
* user:->add
*/
}jedis.sort("mylist", sortingParameters, "mylist");// 排序后指定排序结果到一个 KEY 中,这里讲结果覆盖原来的 KEY List<String> list = jedis.lrange("mylist", 0, -1); for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i)); }@org.junit.Test </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * sort set * SET结合String的排序 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSort3() { Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); jedis.del(</span>"tom:friend:list", "score:uid:123", "score:uid:456"<span style="color: rgba(0, 0, 0, 1)">, </span>"score:uid:789", "score:uid:101", "uid:123", "uid:456"<span style="color: rgba(0, 0, 0, 1)">, </span>"uid:789", "uid:101"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"tom:friend:list", "123"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> tom的好友列表 </span> jedis.sadd("tom:friend:list", "456"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"tom:friend:list", "789"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"tom:friend:list", "101"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"score:uid:123", "1000"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 好友对应的成绩 </span> jedis.set("score:uid:456", "6000"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"score:uid:789", "100"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"score:uid:101", "5999"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"uid:123", "{'uid':123,'name':'lucy'}"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 好友的详细信息 </span> jedis.set("uid:456", "{'uid':456,'name':'jack'}"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"uid:789", "{'uid':789,'name':'jay'}"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"uid:101", "{'uid':101,'name':'jolin'}"<span style="color: rgba(0, 0, 0, 1)">); SortingParams sortingParameters </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SortingParams(); sortingParameters.desc(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> sortingParameters.limit(0, 2); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 注意GET操作是有序的,GET user_name_* GET user_password_* </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 和 GET user_password_* GET user_name_*返回的结果位置不同 </span> sortingParameters.get("#");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> GET 还有一个特殊的规则—— "GET #" </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ,用于获取被排序对象(我们这里的例子是 user_id )的当前元素。 </span> sortingParameters.get("uid:*"<span style="color: rgba(0, 0, 0, 1)">); sortingParameters.get(</span>"score:uid:*"<span style="color: rgba(0, 0, 0, 1)">); sortingParameters.by(</span>"score:uid:*"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 对应的redis 命令是./redis-cli sort tom:friend:list by score:uid:* get # get </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> uid:* get score:uid:* </span> List<String> result = jedis.sort("tom:friend:list"<span style="color: rgba(0, 0, 0, 1)">, sortingParameters); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String item : result) { System.out.println(</span>"item..." +<span style="color: rgba(0, 0, 0, 1)"> item); } } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * * 只获取对象而不排序 BY 修饰符可以将一个不存在的 key 当作权重,让 SORT 跳过排序操作。 * 该方法用于你希望获取外部对象而又不希望引起排序开销时使用。 # 确保fake_key不存在 redis> EXISTS fake_key * (integer) 0 # 以fake_key作BY参数,不排序,只GET name 和 GET password redis> SORT * user_id BY fake_key GET # GET user_name_* GET user_password_* 1) "222" # * id 2) "hacker" # user_name 3) "hey,im in" # password 4) "59230" 5) "jack" * 6) "jack201022" 7) "2" 8) "huangz" 9) "nobodyknows" 10) "1" 11) "admin" * 12) "a_long_long_password" </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSort4() { } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 保存排序结果 默认情况下, SORT 操作只是简单地返回排序结果,如果你希望保存排序结果,可以给 STORE 选项指定一个 key * 作为参数,排序结果将以列表的形式被保存到这个 key 上。(若指定 key 已存在,则覆盖。) redis> EXISTS * user_info_sorted_by_level # 确保指定key不存在 (integer) 0 redis> SORT user_id BY * user_level_* GET # GET user_name_* GET user_password_* STORE * user_info_sorted_by_level # 排序 (integer) 12 # 显示有12条结果被保存了 redis> LRANGE * user_info_sorted_by_level 0 11 # 查看排序结果 1) "59230" 2) "jack" 3) * "jack201022" 4) "2" 5) "huangz" 6) "nobodyknows" 7) "222" 8) "hacker" 9) * "hey,im in" 10) "1" 11) "admin" 12) "a_long_long_password" 一个有趣的用法是将 SORT * 结果保存,用 EXPIRE 为结果集设置生存时间,这样结果集就成了 SORT 操作的一个缓存。 这样就不必频繁地调用 SORT * 操作了,只有当结果集过期时,才需要再调用一次 SORT 操作。 * 有时候为了正确实现这一用法,你可能需要加锁以避免多个客户端同时进行缓存重建(也就是多个客户端,同一时间进行 SORT * 操作,并保存为结果集),具体参见 SETNX 命令。 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSort5() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 排序默认以数字作为对象,值被解释为双精度浮点数,然后进行比较 </span> Jedis jedis =<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 一般SORT用法 最简单的SORT使用方法是SORT key。 </span> jedis.lpush("mylist", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"mylist", "4"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"mylist", "6"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"mylist", "3"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"mylist", "0"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> List<String> list = redis.sort("sort");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 默认是升序 </span> SortingParams sortingParameters = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SortingParams(); sortingParameters.desc(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> sortingParameters.alpha();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当数据集中保存的是字符串值时,你可以用 ALPHA </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修饰符(modifier)进行排序。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> sortingParameters.limit(0, 2);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">可用于分页查询 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 没有使用 STORE 参数,返回列表形式的排序结果. 使用 STORE 参数,返回排序结果的元素数量。 </span>
jedis.sadd(</span>"tom:friend:list", "123"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> tom的好友列表 </span> jedis.sadd("tom:friend:list", "456"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"tom:friend:list", "789"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"tom:friend:list", "101"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"score:uid:123", "1000"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 好友对应的成绩 </span> jedis.set("score:uid:456", "6000"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"score:uid:789", "100"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"score:uid:101", "5999"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"uid:123", "{'uid':123,'name':'lucy'}"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 好友的详细信息 </span> jedis.set("uid:456", "{'uid':456,'name':'jack'}"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"uid:789", "{'uid':789,'name':'jay'}"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"uid:101", "{'uid':101,'name':'jolin'}"<span style="color: rgba(0, 0, 0, 1)">); sortingParameters </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SortingParams(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> sortingParameters.desc(); </span> sortingParameters.get("#");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> GET 还有一个特殊的规则—— "GET #" </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ,用于获取被排序对象(我们这里的例子是 user_id )的当前元素。 </span> sortingParameters.by("score:uid:*"<span style="color: rgba(0, 0, 0, 1)">); jedis.sort(</span>"tom:friend:list", sortingParameters, "tom:friend:list"<span style="color: rgba(0, 0, 0, 1)">); List</span><String> result = jedis.lrange("tom:friend:list", 0, -1<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String item : result) { System.out.println(</span>"item..." +<span style="color: rgba(0, 0, 0, 1)"> item); } jedis.flushDB(); RedisUtil.closeJedis(jedis); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testMore(){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">ZRANGE取出最新的10个项目。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用LPUSH + LTRIM,确保只取出最新的1000条项目。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">HINCRBY key field increment,为哈希表 key 中的域 field 的值加上增量 increment </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">INCRBY,HINCRBY等等,redis有了原子递增(atomic increment),你可以放心的加上各种计数,用GETSET重置,或者是让它们过期。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> LREM greet 2 morning # 移除从表头到表尾,最先发现的两个 morning,这个可以用来删除特定评论 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> zrevrank test a 查看a在sorted set中倒排序时排在第几名,查询结果按照INDEX,所以INDEX是3表示排在第四名 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> zrank test a 相反,表示正排序时候的名次 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> zscore test one表示one这个元素在sorted set中的score为多少 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> zrevrange test 0 -1 表示sorted set倒排序,zrange test 0 -1表示正排序 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将一个或多个 member 元素及其 score 值加入到有序集 key 当中。如果某个 member 已经是有序集的成员,那么更新这个 member 的 score 值,并通过重新插入这个 member 元素,来保证该 member 在正确的位置上。 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">zrem test one删除sorted set中某个元素 </span>
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> List<String> get_latest_comments(<span style="color: rgba(0, 0, 255, 1)">int</span> start, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> num_items){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取最新评论 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">LPUSH latest.comments <ID> </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">-我们将列表裁剪为指定长度,因此Redis只需要保存最新的5000条评论: </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">LTRIM latest.comments 0 5000 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">们做了限制不能超过5000个ID,因此我们的获取ID函数会一直询问Redis。只有在start/count参数超出了这个范围的时候,才需要去访问数据库。 </span> Jedis jedis =<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); List</span><String> id_list = jedis.lrange("latest.comments",start,start+num_items-1<span style="color: rgba(0, 0, 0, 1)">) ; </span><span style="color: rgba(0, 0, 255, 1)">if</span>(id_list.size()<<span style="color: rgba(0, 0, 0, 1)">num_items){ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">id_list = SQL.EXECUTE("SELECT ... ORDER BY time LIMIT ..."); </span>
}
return id_list;
}@Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testDB() { Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); System.out.println(jedis.select(</span>0));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> select db-index </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通过索引选择数据库,默认连接的数据库所有是0,默认数据库数是16个。返回1表示成功,0失败 </span> System.out.println(jedis.dbSize());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> dbsize 返回当前数据库的key数量 </span> System.out.println(jedis.keys("*")); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回匹配指定模式的所有key </span>
System.out.println(jedis.randomKey());
jedis.flushDB();// 删除当前数据库中所有 key, 此方法不会失败。慎用
jedis.flushAll();// 删除所有数据库中的所有 key,此方法不会失败。更加慎用
}jedis.rpush("ids", "aa"); jedis.rpush("ids", "bb"); jedis.rpush("ids", "cc");@Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testMget() { Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); jedis.flushDB();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除当前数据库中所有key,此方法不会失败。慎用 </span>
List</span><String> ids = jedis.lrange("ids", 0, -1<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"aa", "{'name':'zhoujie','age':20}"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"bb", "{'name':'yilin','age':28}"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"cc", "{'name':'lucy','age':21}"<span style="color: rgba(0, 0, 0, 1)">); List</span><String> list = jedis.mget(ids.toArray(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> String[ids.size()])); System.out.println(list); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 可以利用lrange对list进行分页操作 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> queryPageBy() { </span><span style="color: rgba(0, 0, 255, 1)">int</span> pageNo = 6<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">int</span> pageSize = 6<span style="color: rgba(0, 0, 0, 1)">; Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); jedis.del(</span>"a"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 1; i <= 30; i++<span style="color: rgba(0, 0, 0, 1)">) { jedis.rpush(</span>"a", i + ""<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">int</span> start = pageSize * (pageNo - 1);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 因为redis中list元素位置基数是0 </span> <span style="color: rgba(0, 0, 255, 1)">int</span> end = start + pageSize - 1<span style="color: rgba(0, 0, 0, 1)">; List</span><String> results = jedis.lrange("a", start, end);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从start算起,start算一个元素,到结束那个元素 </span> <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String str : results) { System.out.println(str); } } @Test </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * [向Redis list压入ID而不是实际的数据] 在上面的例子里 ,我们将“对象”(此例中是简单消息)直接压入Redis list,但通常不应这么做, 由于对象可能被多次引用:例如在一个list中维护其时间顺序,在一个集合中保存它的类别,只要有必要,它还会出现在其他list中,等等。 让我们回到reddit.com的例子,将用户提交的链接(新闻)添加到list中,有更可靠的方法如下所示: $ redis-cli incr next.news.id (integer) 1 $ redis-cli set news:1:title "Redis is simple" OK $ redis-cli set news:1:url "</span><span style="color: rgba(0, 128, 0, 1); text-decoration: underline">http://code.google.com/p/redis</span><span style="color: rgba(0, 128, 0, 1)">" OK $ redis-cli lpush submitted.news 1 OK 我们自增一个key,很容易得到一个独一无二的自增ID,然后通过此ID创建对象–为对象的每个字段设置一个key。最后将新对象的ID压入submitted.news list。 这只是牛刀小试。在命令参考文档中可以读到所有和list有关的命令。你可以删除元素,旋转list,根据索引获取和设置元素,当然也可以用LLEN得到list的长度。 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testListStrUsage() { String title </span>= "太阳能是绿色能源4"<span style="color: rgba(0, 0, 0, 1)">; String url </span>= "http://javacreazyer.iteye.com"<span style="color: rgba(0, 0, 0, 1)">; Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); </span><span style="color: rgba(0, 0, 255, 1)">long</span> adInfoId = jedis.incr("ad:adinfo:next.id"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"ad:adinfo:" + adInfoId + ":title"<span style="color: rgba(0, 0, 0, 1)">, title); jedis.set(</span>"ad:adinfo:" + adInfoId + ":url"<span style="color: rgba(0, 0, 0, 1)">, url); jedis.lpush(</span>"ad:adinfo"<span style="color: rgba(0, 0, 0, 1)">, String.valueOf(adInfoId)); String resultTitle </span>= jedis.get("ad:adinfo:" + adInfoId + ":title"<span style="color: rgba(0, 0, 0, 1)">); String resultUrl </span>= jedis.get("ad:adinfo:" + adInfoId + ":url"<span style="color: rgba(0, 0, 0, 1)">); List</span><String> ids = jedis.lrange("ad:adinfo", 0, -1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(resultTitle); System.out.println(resultUrl); System.out.println(ids); </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * dbsize返回的是所有key的数目,包括已经过期的, 而redis-cli keys "*"查询得到的是有效的key数目 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> System.out.println(jedis.dbSize()); jedis.flushAll(); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 下面是一个简单的方案:对每个想加标签的对象,用一个标签ID集合与之关联,并且对每个已有的标签,一组对象ID与之关联。 例如假设我们的新闻ID * 1000被加了三个标签tag 1,2,5和77,就可以设置下面两个集合: $ redis-cli sadd news:1000:tags 1 * (integer) 1 $ redis-cli sadd news:1000:tags 2 (integer) 1 $ redis-cli * sadd news:1000:tags 5 (integer) 1 $ redis-cli sadd news:1000:tags 77 * (integer) 1 $ redis-cli sadd tag:1:objects 1000 (integer) 1 $ redis-cli * sadd tag:2:objects 1000 (integer) 1 $ redis-cli sadd tag:5:objects 1000 * (integer) 1 $ redis-cli sadd tag:77:objects 1000 (integer) 1 * 要获取一个对象的所有标签,如此简单: $ redis-cli smembers news:1000:tags 1. 5 2. 1 3. 77 4. * 2 而有些看上去并不简单的操作仍然能使用相应的Redis命令轻松实现。例如我们也许想获得一份同时拥有标签1, 2, * 10和27的对象列表。这可以用SINTER命令来做,他可以在不同集合之间取出交集。因此为达目的我们只需: $ redis-cli sinter * tag:1:objects tag:2:objects tag:10:objects tag:27:objects ... no result * in our dataset composed of just one object ... * 在命令参考文档中可以找到和集合相关的其他命令,令人感兴趣的一抓一大把。一定要留意SORT命令,Redis集合和list都是可排序的。 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)"> @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSetUsage() { Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); jedis.sadd(</span>"zhongsou:news:1000:tags", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:news:1000:tags", "2"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:news:1000:tags", "5"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:news:1000:tags", "77"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:news:2000:tags", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:news:2000:tags", "2"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:news:2000:tags", "5"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:news:2000:tags", "77"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:news:3000:tags", "2"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:news:4000:tags", "77"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:news:5000:tags", "1"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:news:6000:tags", "5"<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:tag:1:objects", 1000 + ""<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:tag:2:objects", 1000 + ""<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:tag:5:objects", 1000 + ""<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:tag:77:objects", 1000 + ""<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:tag:1:objects", 2000 + ""<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:tag:2:objects", 2000 + ""<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:tag:5:objects", 2000 + ""<span style="color: rgba(0, 0, 0, 1)">); jedis.sadd(</span>"zhongsou:tag:77:objects", 2000 + ""<span style="color: rgba(0, 0, 0, 1)">); Set</span><String> sets = jedis.sinter("zhongsou:tag:1:objects"<span style="color: rgba(0, 0, 0, 1)">, </span>"zhongsou:tag:2:objects", "zhongsou:tag:5:objects"<span style="color: rgba(0, 0, 0, 1)">, </span>"zhongsou:tag:77:objects"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(sets); jedis.flushAll(); } @Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSortedSetUsage() { Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> RedisUtil.getJedis(); jedis.zadd(</span>"zhongsou:hackers", 1940, "Alan Kay"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"zhongsou:hackers", 1953, "Richard Stallman"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"zhongsou:hackers", 1943, "Jay"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"zhongsou:hackers", 1920, "Jellon"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"zhongsou:hackers", 1965, "Yukihiro Matsumoto"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"zhongsou:hackers", 1916, "Claude Shannon"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"zhongsou:hackers", 1969, "Linus Torvalds"<span style="color: rgba(0, 0, 0, 1)">); jedis.zadd(</span>"zhongsou:hackers", 1912, "Alan Turing"<span style="color: rgba(0, 0, 0, 1)">); Set</span><String> hackers = jedis.zrange("zhongsou:hackers", 0, -1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(hackers); Set</span><String> hackers2 = jedis.zrevrange("zhongsou:hackers", 0, -1<span style="color: rgba(0, 0, 0, 1)">); System.out.println(hackers2); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 区间操作,我们请求Redis返回score介于负无穷到1920年之间的元素(两个极值也包含了)。 </span> Set<String> hackers3 = jedis.zrangeByScore("zhongsou:hackers", "-inf"<span style="color: rgba(0, 0, 0, 1)">, </span>"1920"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(hackers3); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ZREMRANGEBYSCORE 这个名字虽然不算好,但他却非常有用,还会返回已删除的元素数量。 </span> <span style="color: rgba(0, 0, 255, 1)">long</span> num = jedis.zremrangeByScore("zhongsou:hackers", "-inf", "1920"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(num); jedis.flushAll(); }
}
/**
- 获取连接池.
- @return 连接池实例
*/
private static JedisPool getPool(String ip,int port) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(RedisConfig.getMaxactive());
config.setMaxIdle(RedisConfig.getMaxidle());
config.setMaxWait(RedisConfig.getMaxwait());
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
try{
/**
* 如果你遇到 java.net.SocketTimeoutException: Read timed out exception 的异常信息
请尝试在构造 JedisPool 的时候设置自己的超时值. JedisPool 默认的超时时间是 2 秒 (单位毫秒)
/
pool = new JedisPool(config, ip, port,RedisConfig.getTimeout());
} catch(Exception e) {
e.printStackTrace();
}
return pool;
}package com.zhongsou.vertportal.util;
import java.util.HashMap;
import java.util.Map;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;import com.zhongsou.vertportal.conf.BaseConfig;
import com.zhongsou.vertportal.conf.RedisConfig;/**
Redis 工具类, 用于获取 RedisPool.
参考官网说明如下:
You shouldn't use the same instance from different threads because you'll have strange errors.
And sometimes creating lots of Jedis instances is not good enough because it means lots of sockets and connections,
which leads to strange errors as well. A single Jedis instance is not threadsafe!
To avoid these problems, you should use JedisPool, which is a threadsafe pool of network connections.
This way you can overcome those strange errors and achieve great performance.
To use it, init a pool:
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
You can store the pool somewhere statically, it is thread-safe.
JedisPoolConfig includes a number of helpful Redis-specific connection pooling defaults.
For example, Jedis with JedisPoolConfig will close a connection after 300 seconds if it has not been returned.
@author wujintao
*/
public class JedisUtil {
protected Logger log = LoggerFactory.getLogger(getClass());/**
- 私有构造器.
*/
private JedisUtil() {}
private static Map<String,JedisPool> maps = new HashMap<String,JedisPool>();/**
- 获取连接池.
- @return 连接池实例
*/
private static JedisPool getPool(String ip,int port) {
String key = ip+":" +port;
JedisPool pool = null;
if(!maps.containsKey(key)) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(RedisConfig.getMaxactive());
config.setMaxIdle(RedisConfig.getMaxidle());
config.setMaxWait(RedisConfig.getMaxwait());
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
try{
/**
* 如果你遇到 java.net.SocketTimeoutException: Read timed out exception 的异常信息
请尝试在构造 JedisPool 的时候设置自己的超时值. JedisPool 默认的超时时间是 2 秒 (单位毫秒)
/
pool = new JedisPool(config, ip, port,RedisConfig.getTimeout());
maps.put(key, pool);
} catch(Exception e) {
e.printStackTrace();
}
}else{
pool = maps.get(key);
}
return pool;
}/
* 类级的内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例
没有绑定关系,而且只有被调用到时才会装载,从而实现了延迟加载。
/
private static class RedisUtilHolder{
/
* 静态初始化器,由 JVM 来保证线程安全
*/
private static JedisUtil instance = new JedisUtil();
}/**
* 当 getInstance 方法第一次被调用的时候,它第一次读取
*RedisUtilHolder.instance,导致 RedisUtilHolder 类得到初始化;而这个类在装载并被初始化的时候,会初始化它的静
* 态域,从而创建 RedisUtil 的实例,由于是静态的域,因此只会在虚拟机装载类的时候初始化一次,并由虚拟机来保证它的线程安全性。
这个模式的优势在于,getInstance 方法并没有被同步,并且只是执行一个域的访问,因此延迟初始化并没有增加任何访问成本。
/
public static JedisUtil getInstance() {
return RedisUtilHolder.instance;
}/**
- 获取 Redis 实例.
- @return Redis 工具类实例
*/
public Jedis getJedis(String ip,int port) {
Jedis jedis = null;
int count =0;
do{
try{
jedis = getPool(ip,port).getResource();
//log.info("get redis master1!");
} catch (Exception e) {
log.error("get redis master1 failed!", e);
// 销毁对象
getPool(ip,port).returnBrokenResource(jedis);
}
count++;
}while(jedis==null&&count<BaseConfig.getRetryNum());
return jedis;
}/**
- 释放 redis 实例到连接池.
- @param jedis redis 实例
*/
public void closeJedis(Jedis jedis,String ip,int port) {
if(jedis != null) {
getPool(ip,port).returnResource(jedis);
}
}
}