redis核心数据结构及命令操作

一、centos7环境安装redis

1.wget http://download.redis.io/releases/redis‐x.x.x.tar.gz

2.tar xzf redis‐x.x.x.tar.gz

3. #进入redis目录进行编译安装

cd redis‐x.x.x

        make

4.vi redis.conf #修改配置

        daemonize yes    #后台启动

         protected‐mode no #关闭保护模式,使其他机器也能访问redis

        bind 127.0.0.1 修改为0.0.0.0 

5.启动redis

src/redis-server redis.conf

6.验证redis启动是否成功,以及启动redis客户端连接redis服务端

ps -ef|grep redis

src/redis-cli

二、redis五大数据结构及操作命令

1、String结构

1.1 字符串常用命令

SET key value      //Set the string value of a key

MSET key value [key value ...]          //Set multiple keys to multiple values

GET key      //Get the value of a key

MGET key [key ...]    //Get the values of all the given keys

SETEX key seconds value    //Set the value and expiration of a key

SETNX key value    //Set the value of a key, only if the key does not exist

1.2原子加减操作

INCR key   //Increment the integer value of a key by one

DECR key     // Decrement the integer value of a key by one

INCRBY key increment     //Increment the integer value of a key by the given amount

DECRBY key decrement    //Decrement the integer value of a key by the given number

1.3 String应用场景

单值缓存:set key value,get key

对象缓存:set user:id value(user对象json字符串);mset user:id:name rick user:id:sex male,mget user:id:name user:id:sex.

分布式锁:setnx lock:id true     //return 1,means get the lock success

                  setnx lock:id false    //return 0,means get the lock failed

                  del lock:id                //release lock

                  set lock:id true ex 10 nx   //set a timeout of 10 seconds to prevent deadlock

计数器:比如计算文章阅读数量:INCR article:readcout:{id},get article:readcout:{id}

web集群session共享:spring session+redis

分布式系统全局序列号:INCRBY userId 1000  //批量生成序列化号提升性能

2、Hash结构

2.1、常用命令

HSET key field value   //Set the string value of a hash field

HGET key field            //Get the value of a hash field

HMSET key field value [field value ...]    //Set multiple hash fields to multiple values

HMGET key field [field ...]        //Get the values of all the given hash fields

HDEL key field [field ...]          //Delete one or more hash fields

更多命令使用  help @Hash

2.2、应用场景

缓存用户对象:HMSET user 1:name rick 1:sex male

                         HMGET user 1:name 1:sex

电商购物车:用户id为key,用户所添加的商品id为field,商品数量为value。

                添加商品:hset cart:{userId} {prodId} 1

                增加商品数量:hincrby cart:{userId} {prodId} 1

                商品总数:hlen cart:{userId}

                删除商品:hdel cart:{userId} {prodId}

                获取购物车所有商品:hgetall cart:{userId}

2.3、优缺点

优点

同类数据归类整合储存,方便数据管理;相比string操作消耗内存与cpu更小;相比string储存更节省空间

缺点

过期功能不能使用在field上,只能用在key上;Redis集群架构下不适合大规模使用

3、List结构

3.1、常用操作

LPUSH key value [value ...]     //Prepend one or multiple values to a list

RPUSH key value [value ...]     //Append one or multiple values to a list

LPOP key       //Remove and get the first element in a list 

RPOP key      //Remove and get the last element in a list 

LRANGE key start stop    //Get a range of elements from a list

3.2、应用场景

 常用数据结构:stack(栈)=lpush+lpop;queue(队列)=lpush+rpop;Blocking queue=lpush+brpop

 微博、微信公众号消息流:

rick关注了xx博主,xx博主发布了一条微博消息,消息id为1:Lpush msg:{rickId} 1;rick查看微博:LRANGE msg:{rickId} 0 10

4、set结构

4.1、set常用操作 

SADD key member [member ...]   //Add one or more members to a set

SMEMBERS key     //Get all the members in a set

SDIFF key [key ...]    //Subtract multiple sets

SPOP key [count]     //Remove and return one or multiple random members from a set

4.2、应用场景

抽奖小程序:点击参与抽奖:SADD key {userId};查看参与抽奖所有用户:SMEMBERS key;抽取count名中奖者:SRANDMEMBER key [count] / SPOP key [count]

微信点赞功能:SADD like:{userId}:{msgId} {userId};取消点赞功能:SREM like:{userId}:{msgId} {userId};检查某用户是否点赞:SISMEMBER  like:{userId}:{msgId} {userId};获取点赞的用户:SMEMBERS like:{userId}:{msgId};获取点赞的用户数:SCARD  like:{userId}:{msgId};

微博微信关注模型:

rick关注的人:rickSet-{james,paul}

tony关注的人:tonySet-{james,paul,wade,anthony}

james关注的人:jamesSet-{rick,tony,paul,wade,anthony}

rick和tony共同关注:SINTER rickSet tonySet---{james,paul}

rick关注的人也关注了他(wade):SISMEMBER jamesSet wade,SISMEMBER paulSet wade。

rick可能认识的人:SDIFF jamesSet rickSet---{tony,paul,anthony}

集合操作实现电商商品筛选:SINTER seta setb setc

5、zset结构

5.1常用命令

ZADD key score member [score member ...]   //Add one or more members to a sorted set, or update its score if it already exists

ZCARD key  // Get the number of members in a sorted set

ZRANGE key start stop   //Return a range of members in a sorted set, by index

ZREM key member [member ...]  //Remove one or more members from a sorted set

ZREVRANGE key start stop  //Return a range of members in a sorted set, by index, with scores ordered from high to low

ZINCRBY key increment member  //Increment the score of a member in a sorted set

5.2 应用场景

微博排行榜:

加分操作:ZINCRBY hot:newsId 1 James

展示当日排行榜前n:ZREVRANGE hot:newsId 0 n-1 WITHSCORES 

近n日榜单搜索计算:ZUNIONSTORE  all_n n keys... 

展示n日排行榜:ZREVRANGE all_n 0 n-1 withscores

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值