下面代码中的JedisPoolUtil代码请参看:JedisPoolUtil
代码框架,下面的一系列示例都是这个框架中的测试方法:
public class DemoTest {
private static Jedis jedis;
@Before
public void befor() {
jedis = JedisPoolUtil.getJedis();
}
@After
public void after() {
JedisPoolUtil.release(jedis);
}
}
@Test
public void testKey() throws InterruptedException {
System.out.println("清空数据:" + jedis.flushDB());
System.out.println("判断某个键是否存在:" + jedis.exists("username"));
System.out.println("新增的键值对:" + jedis.set("username", "zhangsan"));
System.out.println("是否存在:" + jedis.exists("name"));
System.out.println("新增的键值对:" + jedis.set("password", "1234"));
Set<String> keys = jedis.keys("*");
System.out.println("系统中所有的键如下:" + keys);
System.out.println("删除键password:" + jedis.del("password"));
System.out.println("判断键password是否存在:" + jedis.exists("password"));
System.out.println("设置键username的过期时间为6s:" + jedis.expire("username", 6));
TimeUnit.SECONDS.sleep(2);
System.out.println("查看键username的剩余生存时间:" + jedis.ttl("username"));
System.out.println("移除键username的生存时间:" + jedis.persist("username"));
System.out.println("查看键username的剩余生存时间:" + jedis.ttl("username"));
System.out.println("查看键username所存储的值的类型:" + jedis.type("username"));
}
@Test
public void testString() throws InterruptedException {
jedis.flushDB();
System.out.println("===========增加数据===========");
System.out.println(jedis.set("key1", "value1"));
System.out.println(jedis.set("key2", "value2"));
System.out.println(jedis.set("key3", "value3"));
System.out.println("删除键key2:" + jedis.del("key2"));
System.out.println("获取键key2:" + jedis.get("key2"));
System.out.println("修改key1:" + jedis.set("key1", "value1Changed"));
System.out.println("获取key1的值:" + jedis.get("key1"));
System.out.println("在key3后面加入值:" + jedis.append("key3", "End"));
System.out.println("key3的值:" + jedis.get("key3"));
System.out.println("增加多个键值对:" + jedis.mset("key4", "value4", "key5", "value5", "key6", "value6"));
System.out.println("获取多个键值对:" + jedis.mget("key4", "key5", "key6"));
System.out.println("删除多个键值对:" + jedis.del("key4", "key5"));
System.out.println("获取多个键值对:" + jedis.mget("key4", "key5", "key6"));
jedis.flushDB();
System.out.println("===========新增键值对防止覆盖原先值==============");
System.out.println(jedis.setnx("key11", "value1"));
System.out.println(jedis.setnx("key22", "value2"));
System.out.println(jedis.setnx("key22", "value2-new"));
System.out.println(jedis.get("key11"));
System.out.println(jedis.get("key22"));
System.out.println("===========新增键值对并设置有效时间=============");
System.out.println(jedis.setex("key3", 4, "value3"));
System.out.println(jedis.get("key3"));
TimeUnit.SECONDS.sleep(3);
System.out.println(jedis.get("key3"));
System.out.println("===========获取原值,更新为新值==========");
System.out.println(jedis.getSet("key2", "key2GetSet"));
System.out.println(jedis.get("key2"));
System.out.println("获得key2的值的字串:" + jedis.getrange("key2", 2, 4));
}
@Test
public void testNumber() {
jedis.flushDB();
jedis.set("key1", "1");
jedis.set("key2", "2");
jedis.set("key3", "2.3");
System.out.println("key1的值:" + jedis.get("key1"));
System.out.println("key2的值:" + jedis.get("key2"));
System.out.println("key1的值加1:" + jedis.incr("key1"));
System.out.println("获取key1的值:" + jedis.get("key1"));
System.out.println("key2的值减1:" + jedis.decr("key2"));
System.out.println("获取key2的值:" + jedis.get("key2"));
System.out.println("将key1的值加上整数5:" + jedis.incrBy("key1", 5));
System.out.println("获取key1的值:" + jedis.get("key1"));
System.out.println("将key2的值减去整数5:" + jedis.decrBy("key2", 5));
System.out.println("获取key2的值:" + jedis.get("key2"));
System.out.println(jedis.incrByFloat("key3", 3.5));
}
结果:
@Test
public void testList() {
jedis.flushDB();
System.out.println("===========添加一个list===========");
jedis.lpush("list", "zhangsan", "lisi", "wanger", "mazi", "qianwu", "zhaoliu");
jedis.lpush("list", "zhangsan");
jedis.lpush("list", "mazi");
System.out.println("list的内容:" + jedis.lrange("list", 0, -1));//-1代表倒数第一个元素,-2代表倒数第二个元素
System.out.println("list区间0-3的元素:" + jedis.lrange("list", 0, 3));
System.out.println("===============================");
// 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
System.out.println("删除指定元素个数:" + jedis.lrem("list", 2, "zhangsan"));
System.out.println("list的内容:" + jedis.lrange("list", 0, -1));
System.out.println("删除下表0-3区间之外的元素:" + jedis.ltrim("list", 0, 3));
System.out.println("list的内容:" + jedis.lrange("list", 0, -1));
System.out.println("list列表出栈(左端):" + jedis.lpop("list"));
System.out.println("list的内容:" + jedis.lrange("list", 0, -1));
System.out.println("list添加元素,从列表右端,与lpush相对应:" + jedis.rpush("list", "qianwu"));
System.out.println("list的内容:" + jedis.lrange("list", 0, -1));
System.out.println("list列表出栈(右端):" + jedis.rpop("list"));
System.out.println("list的内容:" + jedis.lrange("list", 0, -1));
System.out.println("修改list指定下标1的内容:" + jedis.lset("list", 1, "mazi"));
System.out.println("list的内容:" + jedis.lrange("list", 0, -1));
System.out.println("===============================");
System.out.println("list的长度:" + jedis.llen("list"));
System.out.println("获取list下标为2的元素:" + jedis.lindex("list", 2));
System.out.println("===============================");
jedis.lpush("list2", "3", "6", "2", "0", "7", "4");
System.out.println("list2排序前:" + jedis.lrange("list2", 0, -1));
System.out.println(jedis.sort("sortedList"));
System.out.println("list2排序后:" + jedis.lrange("list2", 0, -1));
}
@Test
public void testSet() {
jedis.flushDB();
System.out.println("============向集合中添加元素============");
System.out.println(jedis.sadd("set", "e1", "e2", "e4", "e3", "e0", "e8", "e7", "e5"));
System.out.println(jedis.sadd("set", "e6"));
System.out.println(jedis.sadd("set", "e6"));
System.out.println("set的所有元素为:" + jedis.smembers("set"));
System.out.println("删除一个元素e0:" + jedis.srem("set", "e0"));
System.out.println("set的所有元素为:" + jedis.smembers("set"));
System.out.println("删除两个元素e7和e6:" + jedis.srem("set", "e7", "e6"));
System.out.println("set的所有元素为:" + jedis.smembers("set"));
System.out.println("随机的移除集合中的一个元素:" + jedis.spop("set"));
System.out.println("随机的移除集合中的一个元素:" + jedis.spop("set"));
System.out.println("set的所有元素为:" + jedis.smembers("set"));
System.out.println("set中包含元素的个数:" + jedis.scard("set"));
System.out.println("e3是否在set中:" + jedis.sismember("set", "e3"));
System.out.println("e1是否在set中:" + jedis.sismember("set", "e1"));
System.out.println("e1是否在set中:" + jedis.sismember("set", "e5"));
System.out.println("=================================");
System.out.println(jedis.sadd("set1", "e1", "e2", "e4", "e3", "e0", "e8", "e7", "e5"));
System.out.println(jedis.sadd("set2", "e1", "e2", "e4", "e3", "e0", "e8"));
System.out.println("将set1中删除e1并存入set3中:" + jedis.smove("set1", "set3", "e1"));
System.out.println("将set1中删除e2并存入set3中:" + jedis.smove("set1", "set3", "e2"));
System.out.println("set1中的元素:" + jedis.smembers("set1"));
System.out.println("set3中的元素:" + jedis.smembers("set3"));
System.out.println("============集合运算=================");
System.out.println("set1中的元素:" + jedis.smembers("set1"));
System.out.println("set2中的元素:" + jedis.smembers("set2"));
System.out.println("set1和set2的交集:" + jedis.sinter("set1", "set2"));
System.out.println("set1和set2的并集:" + jedis.sunion("set1", "set2"));
System.out.println("set1和set2的差集:" + jedis.sdiff("set1", "set2")); //set1中有,set2中没有
}
@Test
public void testSortedSet() {
jedis.flushDB();
Map<String, Double> map = new HashMap<String, Double>();
map.put("key1", 1.2);
map.put("key2", 4.5);
map.put("key3", 5.2);
map.put("key4", 3.2);
System.out.println(jedis.zadd("zset", 2.5, "key5"));
System.out.println(jedis.zadd("zset", map));
System.out.println("zset中的所有元素:" + jedis.zrange("zset", 0, -1));
System.out.println("zset中的所有元素:" + jedis.zrangeWithScores("zset", 0, -1));
System.out.println("zset中的所有元素:" + jedis.zrangeByScore("zset", 0, 10));
System.out.println("zset中的所有元素:" + jedis.zrangeByScoreWithScores("zset", 0, 10));
System.out.println("zset中key2的分值:" + jedis.zscore("zset", "key2"));
System.out.println("zset中key2的排名:" + jedis.zrank("zset", "key2"));
System.out.println("删除zset中的元素key3:" + jedis.zrem("zset", "key3"));
System.out.println("zset中的所有元素:" + jedis.zrange("zset", 0, -1));
System.out.println("zset中元素的个数:" + jedis.zcard("zset"));
System.out.println("zset中分值在1-4之间的元素的个数:" + jedis.zcount("zset", 1, 4));
System.out.println("key2的分值加上5:" + jedis.zincrby("zset", 5, "key2"));
System.out.println("key3的分值加上4:" + jedis.zincrby("zset", 4, "key3"));
System.out.println("zset中的所有元素:" + jedis.zrange("zset", 0, -1));
}
@Test
public void testHash() {
jedis.flushDB();
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
jedis.hmset("hash", map);
jedis.hset("hash", "key5", "value5");
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));//return Map
System.out.println("散列hash的所有键为:" + jedis.hkeys("hash"));//return Set
System.out.println("散列hash的所有值为:" + jedis.hvals("hash"));//return List
System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:" + jedis.hincrBy("hash", "key6", 6));
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:" + jedis.hincrBy("hash", "key6", 3));
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
System.out.println("删除一个或者多个键值对:" + jedis.hdel("hash", "key2"));
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
System.out.println("散列hash中键值对的个数:" + jedis.hlen("hash"));
System.out.println("判断hash中是否存在key2:" + jedis.hexists("hash", "key2"));
System.out.println("判断hash中是否存在key3:" + jedis.hexists("hash", "key3"));
System.out.println("获取hash中的值:" + jedis.hmget("hash", "key3"));
System.out.println("获取hash中的值:" + jedis.hmget("hash", "key3", "key4"));
}
排序
@Test
public void testSort() {
jedis.flushDB();
jedis.lpush("list", "zhangsan", "lisi", "wanger", "mazi", "qianwu", "zhaoliu");
System.out.println("list的内容:" + jedis.lrange("list", 0, -1));
SortingParams sortingParameters = new SortingParams();
System.out.println(jedis.sort("list", sortingParameters.alpha()));
System.out.println("===============================");
jedis.lpush("sortedList", "13", "26", "12", "0", "7", "24");
System.out.println("sortedList排序前:" + jedis.lrange("sortedList", 0, -1));
System.out.println("升序:" + jedis.sort("sortedList", sortingParameters.asc()));
System.out.println("升序:" + jedis.sort("sortedList", sortingParameters.desc()));
System.out.println("===============================");
jedis.lpush("ids", "33");
jedis.lpush("ids", "44");
jedis.lpush("ids", "11");
jedis.hset("user:55", "score", "55");
jedis.hset("user:55", "name", "lisi");
jedis.hset("user:44", "score", "66");
jedis.hset("user:44", "name", "zhangsan");
jedis.hset("user:33", "score", "33");
jedis.hset("user:33", "name", "wanger");
jedis.hset("user:22", "score", "79");
jedis.hset("user:22", "name", "mazi");
jedis.hset("user:11", "score", "24");
jedis.hset("user:11", "name", "qianwu");
sortingParameters = new SortingParams();
sortingParameters.get("user:*->name");
sortingParameters.get("user:*->score");
System.out.println(jedis.sort("ids", sortingParameters));
}