实例:
package com.mylearn.util; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.jd.data.redis.RedisUtils; import com.jd.data.redis.connection.RedisAccessException; import org.hibernate.annotations.Immutable; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.unitils.spring.annotation.SpringBean; import redis.clients.jedis.ShardedJedis; import java.util.List; import java.util.Map; import java.util.Set; /** * Created by IntelliJ IDEA. * User: yingkuohao * Date: 14-1-13 * Time: 下午6:20 * CopyRight:360buy * Descrption: * http://javacrazyer.iteye.com/blog/1840161 * http://blog.sina.com.cn/s/blog_4cfe78830101inoh.html * http://redis.readthedocs.org/en/2.4/index.html * To change this template use File | Settings | File Templates. */ public class TestRedis extends BaseTestCase { @SpringBean("redisUtils") private RedisUtils redisUtils; public void test11() { try { // redisUtils.del("test"); redisUtils.lpush("test", "111"); redisUtils.lpush("test", "222"); redisUtils.lpush("test", "333"); redisUtils.lpush("test", "444"); redisUtils.ltrim("test",0,2); System.out.println("test;" + redisUtils.get("test")); } catch (RedisAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } public void testBase() { //redis中value最大容量是512M try { //1.字符串处理,如果key已存在,则覆盖原有的值 redisUtils.set("name", "zhangsan"); String name = redisUtils.get("name"); System.out.println("name = " + name); // zhangsan // 2.追加 redisUtils.append("name", "lisi"); System.out.println("name = " + redisUtils.get("name")); // zhangsanlisi // 3. 删除 // redisUtils.del("name"); System.out.println("name = " + redisUtils.get("name")); // null //4. 判断key是否存在, System.out.println("exists=" + redisUtils.exists("name"));//boolean //5. setnx:set not exist :若key不存在,则存储 redisUtils.setnx("sex", "boy"); redisUtils.setnx("name", "test"); System.out.println("name =" + redisUtils.get("name") + ",sex=" + redisUtils.get("sex")); //6. 设置key的有效时间,并存储数据 redisUtils.setex("name", 2, "wangwu"); System.out.println(redisUtils.get("name")); //7. 设置key的过期时间,单位:秒,如果key在超时之前被修改,则该键关联的超时将被移除。 redisUtils.expire("name",2); //8. 查看剩余时间 System.out.println(redisUtils.ttl("name")); try { Thread.sleep(3001); } catch (InterruptedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } System.out.println(redisUtils.get("name")); //null //7. getSet:获取value,并重新赋值 redisUtils.set("age", "20"); System.out.println(redisUtils.getSet("age", "30")); System.out.println(redisUtils.get("age")); //8. 截取value值 System.out.println(redisUtils.getrange("age", 1, 3)); } catch (RedisAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } /** * list类型是按照插入顺序排序的字符串链表,和数据结构中的普通链表一样,我们可以在其头部(left)和尾部(right) * 添加新的元素。在插入时,如果该键不存在,Redis将为该键创建一个新的链表。与此相反,如果链表中的所有元素均被移除, * 那么该键也将会被从数据库中删除。List中可包含的最大元素数量 是42亿。 */ public void testList() { try { //1.存入list,rpush:右侧入队 redisUtils.del("msg"); redisUtils.rpush("msg", "test1"); redisUtils.rpush("msg", "test2"); redisUtils.rpush("msg", "test3"); //2. 截取list,0代表链表头部的第一个元素,-1代表最后一个,-2代表倒数第二个,在获取时,start和end的值都会被取出 List System.out.println("lst = " + Joiner.on(",").join(lst)); //test1,test2,test3 //3. 左侧入队 redisUtils.lpush("msg", "test4"); List System.out.println("lst = " + Joiner.on(",").join(lst2)); //test4,test1,test2,test3 //4. llen:长度 System.out.println("lst.length=" + redisUtils.llen("msg"));//4 //5.lrange:截取 System.out.println("lst.lrange=" + redisUtils.lrange("msg", 1, 3));//test1,test2,test3 //6.lset:指定某个index,设值 redisUtils.lset("msg", 1, "release");//test1,test2,test3 //7.lrem:指定某个index,删除 System.out.println("lst.lrange=" + redisUtils.lrem("msg", 2, "test2"));//test1,test2,test3 List System.out.println("lst3 = " + Joiner.on(",").join(lst3)); //test4,release,test3 //8. ltrim:截取,删除区间以外的值,和lrange的区别就是,lrange相当于一个备份。 redisUtils.ltrim("msg", 1, 2); List System.out.println("lst4 = " + Joiner.on(",").join(lst4)); //release,test3 //9. lpop:从左侧出队 System.out.println("lpop:" + redisUtils.lpop("msg")); //release //10 rpop:从右侧出队 System.out.println("lpop:" + redisUtils.rpop("msg")); //test3 List System.out.println("lst5 = " + Joiner.on(",").join(lst5)); //null } catch (RedisAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } /** * set:字符集合,和list的区别是,元素不能重复,提供了合并交叉方法,比较高效 */ public void testSet() { try { redisUtils.srem("setinfo", "test1", "test2", "test3"); //1. 增加 redisUtils.sadd("setinfo", "test1"); redisUtils.sadd("setinfo", "test2"); redisUtils.sadd("setinfo", "test3"); redisUtils.sadd("setinfo", "test3"); //2. smembers:获取所有 Set System.out.println("setvalues:" + set); //3.删除 redisUtils.srem("setinfo", "test2"); System.out.println("setvalues:" + redisUtils.smembers("setinfo")); //4. 判断是否属于集合的元素 System.out.println("sismember:" + redisUtils.sismember("setinfo", "test1")); //5. scard: 返回集合的元素个数 System.out.println("scard:" + redisUtils.scard("setinfo")); //6. spop:出栈 String s = redisUtils.spop("setinfo"); System.out.println("spop=" + s); System.out.println("setvalues:" + redisUtils.smembers("setinfo")); //7.还有一些合并交叉的,我们自己的redisutil没有封装,可以参考jedis的api } catch (RedisAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } /** * 有序集合: * 与set的主要差别就是每一个成员都有一个分数(score)与之关联,redis正是通过分数来为集合中的成员进行排序。值得注意的是 * value是必须唯一的,但score却可以重复。 * 在sorted-set中的添加、删除、更新一个成员都是非常快速的操作,时间复杂度是集合成员数量的对数。 */ public void testSortedSet() { try { //1. 按照scorce删除 redisUtils.zremrangeByScore("pin", 1, 9); //2. 按照range删除 redisUtils.zremrangeByRank("pin", 1, 3); //3.添加元素 redisUtils.zadd("pin", 1, "0001"); redisUtils.zadd("pin", 1, "0002"); redisUtils.zadd("pin", 2, "0003"); redisUtils.zadd("pin", 2, "0004"); redisUtils.zadd("pin", 3, "0005"); redisUtils.zadd("pin", 3, "0006"); redisUtils.zadd("pin", 9, "0009"); //4.zrange:截取,-1为全部 Set System.out.println("setValues=" + setValues); //5. zrevrange:截取,并翻转 Set System.out.println("setValues2=" + setValues2); //6. zcard:获取长度 System.out.println("zcard:" + redisUtils.zcard("pin")); //7. zscore:根据score获取 System.out.println("zscore:" + redisUtils.zscore("pin", "0003")); //8. zcount: 根据评分截取之后获取长度 System.out.println("zcount:" + redisUtils.zcount("pin", 1, 2)); System.out.println("zcount:" + redisUtils.zrange("pin", 1, 2)); //9.根据score截取 System.out.println("zcount:" + redisUtils.zrangeByScore("pin", 1, 3)); } catch (RedisAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } /** * map操作,相当于存的, */ public void testHsh() { Map try { //1.hmset: 加入map redisUtils.hmset("mapA", mapA); //2. hmget:获取map指定的key对应的value List System.out.println("name=" + name); //3. hdel:删除指定的key redisUtils.hdel("mapA", "b"); System.out.println("name=" + redisUtils.hmget("mapA", "b")); //4. hlen:获取指定map的长度 System.out.println("hlen=" + redisUtils.hlen("mapA")); //5. exists:判断指定的map是否存在 System.out.println("exists=" + redisUtils.exists("mapA")); //6. hkeys:获取指定的key集合 System.out.println("hkeys=" + redisUtils.hkeys("mapA")); //7. hvals:获取指定的value集合 System.out.println("hvals=" + redisUtils.hvals("mapA")); //8.设值,为指定的key设置Field/value对,如果不存在创建新key,如果filed在key中已存在,则覆盖原有值。 redisUtils.hset("mapb", "key1", "value1"); redisUtils.hset("mapb", "key2", "value3"); redisUtils.hset("mapb", "key3", "value4"); redisUtils.hset("mapb", "123", "1234"); //9. hget: System.out.println("hget=" + redisUtils.hget("mapb", "key1")); redisUtils.hset("mapb", "key1", "value5"); System.out.println("hget=" + redisUtils.hget("mapb", "key1")); //10.hmget:获取多个filed对应的值 System.out.println("hmget=" + redisUtils.hmget("mapb", "key1", "key2")); //11. hgetAll:获取map指定key的所有值 System.out.println("hgetAll=" + redisUtils.hgetAll("mapb")); //12. hdel:删除 System.out.println("hdel=" + redisUtils.hdel("mapb", "key3")); //13.hincrBy:增加,value必须为integer System.out.println("hincrBy=" + redisUtils.hincrBy("mapb", "123", 123l)); System.out.println("hget=" + redisUtils.hget("mapb", "key1")); //14. hsetnx:当key或field不存在时,设值,否则不做任何操作 redisUtils.hsetnx("mapb","key3","new"); } catch (RedisAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } public void testOthers() { try { //1. 获取key的失效时间,-1为永久 System.out.println("time="+redisUtils.ttl("mapb") ); //2. 设定指定key的有效时间,单位为秒 redisUtils.setex("mapb", 10, "min"); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } System.out.println("time=" + redisUtils.ttl("mapb"));//5 //3. // redisUtils } catch (RedisAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } } |