import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import junit.framework.TestCase; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import redis.clients.jedis.Jedis; public class TestRedis extends TestCase { private Jedis jedis; @BeforeClass public void setUp()throws Exception{ //连接redis服务器,192.168.0.100:6379 jedis = new Jedis("192.168.91.234", 6379); //权限认证 //jedis.auth("admin"); } @AfterClass public void tearDown(){ } @Test public void testString() throws Exception{ //-----添加数据---------- jedis.set("name","xinxin"); //向key-->name中放入了value-->xinxin jedis.set("name","yueer"); // 如果已经存在,覆盖掉原先的数据 jedis.setnx("name","yueyue"); // 如果已经存在,不覆盖掉原先的数据 jedis.setnx("xingming","zhangdongyue"); // 如果不存在则增加此键值对 Boolean xingIs = jedis.exists("xing"); // 判断key值是否存在 System.out.println("xing is exists : " + xingIs); System.out.println(jedis.get("name")); System.out.println(jedis.get("xingming")); jedis.append("name", " is my lover"); //拼接 System.out.println(jedis.get("name")); jedis.del("name"); //删除某个键 System.out.println(jedis.get("name")); //设置多个键值对 jedis.mset("name","liuling","age","23","qq","476777XXX"); jedis.incr("age"); //进行加1操作 System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq")); jedis.decr("age"); //进行减1操作 System.out.println("减1操作 : " + jedis.get("age")); jedis.flushDB(); //清空数据 System.out.println(jedis.get("xingming")); //设置超时时间 单位 秒 jedis.setex("expirtime",1,"value"); System.out.println(jedis.get("expirtime")); Thread.sleep(1200); System.out.println(jedis.get("expirtime")); } @Test public void testList() throws Exception{ jedis.flushDB(); //清空数据 jedis.rpush("l_name","pan"); jedis.rpush("l_name","teng"); //右端插入 jedis.lpush("l_name","姓名:"); //左端插入 // 再取出所有数据jedis.lrange是按范围取出, // 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有 List<String> values = jedis.lrange("l_name", 0, -1); System.out.println(values); System.out.println(values.get(0)); // 数组长度 System.out.println("数组长度: " + jedis.llen("l_name")); // 排序 jedis.rpush("l_num","4"); jedis.rpush("l_num","3"); jedis.rpush("l_num","5"); jedis.rpush("l_num","6"); System.out.println("排序: " + jedis.sort("l_num")); //只能对数字排序 // 修改列表中单个值 jedis.lset("l_name", 0, "name: "); values = jedis.lrange("l_name", 0, -1); System.out.println("修改列表中的值:" + values); // 获取列表指定下标的值 System.out.println("指定下标值: " + jedis.lindex("l_name", 0)); // 删除区间以外的数据 jedis.ltrim("l_name", 0, 1); values = jedis.lrange("l_name", 0, -1); System.out.println("删除区间以外的资源: " + values); // 列表出栈 //System.out.println(jedis.lpop("l_name")); //弹出左端的值 System.out.println(jedis.rpop("l_name")); //弹出右端的值 values = jedis.lrange("l_name", 0, -1); System.out.println("列表出栈: " + values); //删除指定元素 jedis.rpush("l_elements","pan"); jedis.rpush("l_elements","pan"); jedis.rpush("l_elements","zzz"); jedis.rpush("l_elements","zzz"); jedis.lrem("l_elements",0,"pan"); //删除list中所有的值为pan的元素 第二个参数表示删除多少个,0表示所有 values = jedis.lrange("l_elements", 0, -1); System.out.println("删除元素: " + values); } @Test public void testSet() throws Exception{ jedis.flushDB(); //清空数据 jedis.sadd("myset", "1"); jedis.sadd("myset", "2"); jedis.sadd("myset", "3"); jedis.sadd("myset", "4"); Set<String> setValues = jedis.smembers("myset"); System.out.println(setValues); // 移除noname jedis.srem("myset", "4"); System.out.println("删除Set 元素:" + jedis.smembers("myset")); //判断2是否存在于 set中 System.out.println(jedis.sismember("myset", "4")); //遍历所有元素 Set<String> redisSet = jedis.smembers("myset"); Iterator it = redisSet.iterator(); System.out.println("遍历map"); while(it.hasNext()){ System.out.println(it.next()); } //集合求 交并差 Jedis jedis2; jedis2 = new Jedis("192.168.91.234", 6379); jedis2.sadd("myset2","2"); jedis2.sadd("myset2","3"); jedis2.sadd("myset2","6"); jedis2.sadd("myset2","7"); System.out.println("集合求交集:" + jedis.sinter("myset","myset2")); System.out.println("集合求并集:" + jedis.sunion("myset","myset2")); System.out.println("集合求差集myset - myset2: " + jedis.sdiff("myset","myset2")); System.out.println("集合求差集myset2 - myset: " + jedis.sdiff("myset2","myset")); } @Test public void testSortSet(){ jedis.flushDB(); jedis.zadd("sort_set",1,"first"); jedis.zadd("sort_set",4,"fourth"); jedis.zadd("sort_set",2,"second"); jedis.zadd("sort_set",3,"third"); jedis.zadd("sort_set",5,"fifth"); jedis.zadd("sort_set",6,"sixth"); jedis.zadd("sort_set",7,"seventh"); jedis.zadd("sort_set",8,"eighth"); jedis.zadd("sort_set",8,"eighth2"); jedis.zadd("sort_set",9,"nineth"); jedis.zadd("sort_set",0,"zero"); System.out.println("有序集合: " + jedis.zrange("sort_set",0,-1)); //删除集合中的元素 jedis.zrem("sort_set","third"); System.out.println("有序集合删除元素: " + jedis.zrange("sort_set",0,-1)); //统计有多少元素 System.out.println("有序集合元素个数: " + jedis.zcard("sort_set")); //取某一范围内的元素 System.out.println("根据起始结束索引获取元素: " + jedis.zrange("sort_set",0,1)); //起始索引 结束索引 //统计权重范围内的元素个数 System.out.println("统计权重范围内的元素个数: " + jedis.zcount("sort_set",7,8)); //查看元素的权重 System.out.println("查看元素的权重: " + jedis.zscore("sort_set","eighth")); // } @Test public void testHashMap(){ jedis.flushDB(); Map <String,String> map = new HashMap<String,String>(); map.put("name", "panteng"); map.put("pwd", "123456"); map.put("age", "24"); jedis.hmset("user", map); jedis.hset("user", "address","BeiJing_HaiDian"); List<String> list = jedis.hmget("user", "name","pwd","address"); System.out.println("name=" + list.get(0)); System.out.println("password=" + list.get(1)); System.out.println("address=" + list.get(2)); //删除键值对 jedis.hdel("user", "pwd"); //获取所有key System.out.println("获取hashs中所有的key:"+jedis.hkeys("user")); System.out.println("获取hashs中所有的value:"+jedis.hvals("user")); } }
时间: 2024-11-02 00:50:18