Redis一些常用的数据结构和操作

 

package com.newcode.wenda.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.newcode.wenda.model.User;
import redis.clients.jedis.BinaryClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

public class JedisAdapter {
    public static void print(int index,Object obj){
        System.out.println(String.format("%d,%s",index,obj.toString()));
    }

    public static void main(String[] args) {
        Jedis jedis=new Jedis("redis://localhost:6379/0");
        jedis.flushDB();



        //l打头是list,h打头是hashset,s打头是set,z打头是优先队列

        //list适合放一个列表,hashset适合放一些不定向的属性值(如存放某些属性值可能为空的user),优先队列适合排序(如排行榜),set适合去重以及求两个对象的异同(如共同关注功能)

        //get,set
        jedis.set("hello","world");
        print(0,jedis.get("hello"));
        jedis.rename("hello","newhello");
        print(1,jedis.get("newhello"));
        jedis.setex("hello2",15,"world");  //设置了过期时间




        jedis.set("pv","200");
        jedis.incr("pv");
        print(1,jedis.get("pv"));
        jedis.incrBy("pv",5);
        print(2,jedis.get("pv"));
        jedis.decrBy("pv",8);
        print(3,jedis.get("pv"));


        print(4,jedis.keys("*"));


        //list
        String listName="list";
        jedis.del(listName);
        for (int i=0;i<10;i++){
            jedis.lpush(listName,"a"+String.valueOf(i));
        }
        print(5,jedis.lrange(listName,0,12));

        print(6,jedis.llen(listName));          //求队列长度
        print(7,jedis.lpop(listName));          //队列中推出一个成员
        print(8,jedis.llen(listName));
        print(9,jedis.lindex(listName,8));        //根据下标找队列中成员
        print(10,jedis.linsert(listName, BinaryClient.LIST_POSITION.AFTER,"a4","xxxxxxxxxx"));      //在成员后面插入数据
        print(11,jedis.linsert(listName, BinaryClient.LIST_POSITION.BEFORE,"a4","xxxxxxxxxx"));    //在成员后面插入数据
        print(12,jedis.lrange(listName,0,12));      //打印队列,从下标0到下标12的位置,下标可以自己在合理范围内设置
        print(13,jedis.keys("*"));



        //hash
        String userKey = "userxx";
        jedis.hset(userKey,"name","ji");    //设置kv键值对
        jedis.hset(userKey,"age","180");
        jedis.hset(userKey,"phone","110");
        print(14,jedis.hget(userKey,"name"));    //根据键值获取value
        print(15,jedis.hgetAll(userKey));         //获取所有内容
        jedis.hdel(userKey,"phone");     //删除一个成员
        print(16,jedis.hgetAll(userKey));
        print(17,jedis.hexists(userKey,"age"));         //查询是否存在key值
        print(18,jedis.hexists(userKey,"email"));
        print(19,jedis.hkeys(userKey));    //显示所有键值
        print(20,jedis.hvals(userKey));    //显示所有成员遍历
        jedis.hsetnx(userKey,"school","Tsinghua");      //仅设置不存在的键值
        jedis.hsetnx(userKey,"name","others");
        print(21,jedis.hgetAll(userKey));



        //set
        String likeKey1 = "commentLike1";
        String likeKey2 = "commentLike2";
        for (int i=0;i<10;i++){
            jedis.sadd(likeKey1,String.valueOf(i));
            jedis.sadd(likeKey2,String.valueOf(i*i));
        }
        print(22,jedis.smembers(likeKey1));  //显示所有成员
        print(23,jedis.smembers(likeKey2));
        print(24,jedis.sunion(likeKey1,likeKey2));  //求并
        print(25,jedis.sdiff(likeKey1,likeKey2));  //求前一个集合中有而后一个集合中没有的
        print(26,jedis.sdiff(likeKey2,likeKey1));
        print(27,jedis.sinter(likeKey1,likeKey2));    //求交
        print(28,jedis.sismember(likeKey1,"12"));      //查询是否存在成员
        print(29,jedis.sismember(likeKey2,"16"));
        jedis.srem(likeKey1,"5");           //删除一个成员
        print(30,jedis.smembers(likeKey1));

        jedis.smove(likeKey2,likeKey1,"25");
        print(31,jedis.smembers(likeKey1));
        print(32,jedis.smembers(likeKey2));
        print(33,jedis.scard(likeKey1));   //求总数


        //优先队列
        String rankKey = "rankKey";
        jedis.zadd(rankKey,15,"Lee");    //添加成员
        jedis.zadd(rankKey,60,"Ben");
        jedis.zadd(rankKey,100,"Ji");
        jedis.zadd(rankKey,80,"Lucy");
        jedis.zadd(rankKey,90,"Jack");
        jedis.zadd(rankKey,100,"Liu");
        print(34,jedis.zcard(rankKey));   //计算优先对列中成员个数
        print(35,jedis.zcount(rankKey,61,100));  //计算分值61到100的成员个数
        print(36,jedis.zscore(rankKey,"Lucy"));    //获取某个成员的分数
        jedis.zincrby(rankKey,2,"Lucy");           //将某个成员的分数加2
        print(37,jedis.zscore(rankKey,"Lucy"));
        jedis.zincrby(rankKey,2,"Luc");            //如果要加分的成员不存在,则添加改成员,默认值为0
        print(38,jedis.zscore(rankKey,"Luc"));     //如果不存在,会报错
        print(39,jedis.zcard(rankKey));
        print(40,jedis.zrange(rankKey,0,100));
        print(41,jedis.zrevrange(rankKey,0,1)); //显示按分数最高排名的成员,显示前两名
        for (Tuple tuple:jedis.zrangeByScoreWithScores(rankKey,60,100)){
            print(42,tuple.getElement() + ":" + String.valueOf(tuple.getScore()));
        }


        print(43,jedis.zrank(rankKey,"Ben"));    //获取Ben的排名,按分值从低到高排名
        print(44,jedis.zrevrank(rankKey,"Ben")); //获取Ben的排名,按分值从高到低


        String setKey="zset";
        jedis.zadd(setKey,1,"a");
        jedis.zadd(setKey,1,"b");
        jedis.zadd(setKey,1,"c");
        jedis.zadd(setKey,1,"d");
        jedis.zadd(setKey,1,"e");

        print(45,jedis.zlexcount(setKey,"-","+"));   //计算从负无穷到正无穷区间内,成员个数
        print(46,jedis.zlexcount(setKey,"(b","[d"));    //计算b到d间成员个数,此处不包含b
        print(47,jedis.zlexcount(setKey,"[b","[d"));    //此处包含b
        jedis.zrem(setKey,"b");                               //删除一个成员
        print(48,jedis.zrange(setKey,0,10));
        jedis.zremrangeByLex(setKey,"(c","+");                //按字典顺序删除成员
        print(49,jedis.zrange(setKey,0,2));



        //连接池
        /*JedisPool pool=new JedisPool();   //默认连接池有八条线程
        for (int i =0;i<100;i++){
            Jedis j=pool.getResource();
            print(50,j.get("pv"));
            j.close();
        }*/

        User user=new User();
        user.setName("xx");
        user.setPassword("pop");
        user.setHeadUrl("a.png");
        user.setSalt("salt");
        user.setId(1);
        print(51,JSONObject.toJSON(user));
        jedis.set("user1", JSONObject.toJSONString(user));   //将user转为json存放


        String value = jedis.get("user1");
        User user2= JSON.parseObject(value,User.class);  //解析出redis中存放的user1
        print(52,user2);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值