赞
踩
创建JedisTests测试类,为了避免重复重建和销毁Jedis对象,使用注解创建和销毁
@Before @After
private Jedis jedis; @Before public void redisConnection(){ jedis= JedisPoolUtil.getJedis(); } @After public void redisClose(){ jedis.close(); }
string类型
@Test public void redisString(){ //添加成功返回ok System.out.println(jedis.set("k","hello redis")); //获取键的值 System.out.println(jedis.get("k"));; //获取键的数据类型 不存在返回none System.out.println(jedis.type("k")); //获取key对应value的长度 不存在返回0 System.out.println(jedis.strlen("k")); //查询key的过期时间 不存在返回-2 -1永不过期 System.out.println(jedis.ttl("k")); //设置多组键值对 jedis.mset("k1","value1","k2","value2"); //查询多个键的值 System.out.println( jedis.mget("k1","k2")); }
集合:
- @Test
- public void redisList(){
- //返回值 1
- /* System.out.println( jedis.rpush("list","小元,小满,小白"));;
- jedis.lpush("list","小李,小鸣,小佐");*/
-
- /* System.out.println(jedis.llen("list"));
- System.out.println(jedis.lpop("list"));//返回被删除的元素
- System.out.println(jedis.rpop("list"));//返回被删除的元素
- System.out.println(jedis.llen("list"));*/
-
- System.out.println(jedis.lrange("list",0,-1));
-
- System.out.println(jedis.lindex("list",0));
-
- }
set集合:
-
- @Test
- public void redisSet(){
- //返回添加的个数
- /* System.out.println(jedis.sadd("number","java"));
- System.out.println(jedis.sadd("number","redis","script","javaEE"));
- */
- //查询set中的所有元素
- System.out.println(jedis.smembers("number"));
-
-
- /*
- System.out.println(jedis.spop("number"));//随机删除左边第一个元素 返回删除的数据
- System.out.println(jedis.srem("number","java"));//指定删除
- System.out.println(jedis.smembers("number"));
- */
- //判断某元素是否存在 true存在
- System.out.println(jedis.sismember("number","java"));
-
- //随机查询
- System.out.println(jedis.srandmember("number",1));
-
- //获取长度
- System.out.println(jedis.scard("number"));
-
- }
哈希:
- @Test
- public void redisHash(){
- /* System.out.println(jedis.hset("user","username","tom")); 1
- System.out.println(jedis.hget("user","username" ));*/
-
- /* Map m =new HashMap<>();
- m.put("age","18");
- m.put("gender","男");
-
-
- System.out.println(jedis.hmset("user",m));// ok
- System.out.println(jedis.hmget("user","age","gender"));*/
-
- /* //获取所有的键
- System.out.println(jedis.hkeys("user"));
- //获取所有的值
- System.out.println(jedis.hvals("user"));
- //获取长度
- System.out.println(jedis.hlen("user"));
- //键中是否有某属性
- System.out.println(jedis.hexists("user","username"));
- */
- //查看所有的键值
- // System.out.println(jedis.hgetAll("user"));
-
- //删除某属性
- System.out.println(jedis.hdel("user","gender"));
-
-
- }
有序集合:
@Test public void redisOrderedSets(){ /* jedis.zadd("odd",10,"java"); jedis.zadd("odd",20,"javaEE"); jedis.zadd("odd",30,"javaWeb"); jedis.zadd("odd",40,"javaScprit");*/ //查询所有 System.out.println(jedis.zrange("odd",0,-1)); //分数从高到低 System.out.println(jedis.zrevrange("odd",0,-1)); //查询指定区间的数据 System.out.println(jedis.zrangeByScore("odd",30,40)); //返回指定的分数取值 System.out.println(jedis.zscore("odd","javaWeb")); //查询元素的个数 System.out.println(jedis.zcard("odd")); /* System.out.println(jedis.zrem("odd","java","javaEE"));//返回删除的个数 System.out.println(jedis.zcard("odd"));*/ }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。