Redis高级命令操作大全--推荐
Posted 森林木马
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis高级命令操作大全--推荐相关的知识,希望对你有一定的参考价值。
redis安装和使用
redis安装
wget http://download.redis.io/redis-stable.tar.gz tar zxvf redis-stable.tar.gz cd redis-stable.tar.gz make make install
redis启动
redis-server
redis-cli shutdown
配置
方法一
redis-server /path/to/redis.conf redis-server /path/to/redis.conf --loglevel warning //设置日志级别
方法二
redis-cli redis > CONFIG SET loglevel warning
多数据库的选择
> select 1
数据库基本操作
(1)添加数据和查找数据,通过set/get命令添加和获取数据
127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> set bar 1 OK 127.0.0.1:6379[1]> get bar "1" 127.0.0.1:6379[1]> get aaa (nil)
(2)判断一个键是否存在,exists命令可以判断key是否存在,存在返回1,不存在返回0
127.0.0.1:6379[1]> exists bar (integer) 1 127.0.0.1:6379[1]> exists aaa (integer) 0
(3)删除数据,del命令删除key,返回值为成功删除key的个数
127.0.0.1:6379[1]> del aaa (integer) 0 127.0.0.1:6379[1]> del bar (integer) 1
(4)获取value的类型
127.0.0.1:6379[1]> set bar 1 OK 127.0.0.1:6379[1]> type bar string
(5)自增和自减,incr 、decr以及incrby和decrby实现整数的加减
127.0.0.1:6379[1]> INCR ab (integer) 1 127.0.0.1:6379[1]> INCR ab (integer) 2 127.0.0.1:6379[1]> INCRBY ab 2 (integer) 4 127.0.0.1:6379[1]> DECR ab (integer) 3 127.0.0.1:6379[1]> DECRBY ab 2 (integer) 1
(6)增加指定浮点数,incrbyfloat可以指定自增的浮点数
127.0.0.1:6379[1]> INCRBYFLOAT ab 1.1 "2.1"
(7)向尾部追加,append命令可以向字符尾部追加内容
127.0.0.1:6379[1]> set key hello OK 127.0.0.1:6379[1]> append key world (integer) 10 127.0.0.1:6379[1]> get key "helloworld"
(8)获取字符串长度,strlen获取value的长度
127.0.0.1:6379[1]> STRLEN key (integer) 10
(9)批量设置和获取,通过mset和mget命令可以批量执行设置和获取
127.0.0.1:6379[1]> mset key1 va1 key2 va2 OK 127.0.0.1:6379[1]> mget key1 key2 1) "va1" 2) "va2"
(10)位操作
getbit key offset setbit key offset bitcount key [start] [end] bitop operation destkey key [key ...]
散列类型
{ "id":1, "name":"test" }
基本操作
(1)存储方法,可以通过hset和hget来设置和获取数据
127.0.0.1:6379[1]> hset user id 1 (integer) 1 127.0.0.1:6379[1]> hset user name test (integer) 1 127.0.0.1:6379[1]> hget user id "1" 127.0.0.1:6379[1]> hget user name "test"
(2)判断字段是否存在,hexists命令,返回1存在,返回0不存在
127.0.0.1:6379[1]> hexists user id (integer) 1 127.0.0.1:6379[1]> hexists user age (integer) 0
(3)使用hsetnx(hset if not exists)如果不存在赋值
127.0.0.1:6379[1]> hsetnx user age 111 (integer) 1 127.0.0.1:6379[1]> hget user age "111"
(4)增加数字
127.0.0.1:6379[1]> hincrby user score 60 (integer) 60 127.0.0.1:6379[1]> hget user score "60"
(5)删除字段
127.0.0.1:6379[1]> hdel user score (integer) 1 127.0.0.1:6379[1]> hget user score (nil)
如何存储数据?
键 字段 字段值 post:id field value
127.0.0.1:6379[1]> incr userid (integer) 1 127.0.0.1:6379[1]> hset user:1 name test (integer) 1 127.0.0.1:6379[1]> hset user:1 score 90 (integer) 1 127.0.0.1:6379[1]> incr userid (integer) 2 127.0.0.1:6379[1]> hset user:2 name test2 (integer) 1 127.0.0.1:6379[1]> hset user:2 score 99 (integer) 1
添加了两条数据,id为1的名字为test分数为90,而id为2的名字为test2分数为90.
127.0.0.1:6379[1]> hmget user:1 name score 1) "test" 2) "90"
127.0.0.1:6379[1]> hgetall user:2 1) "name" 2) "test2" 3) "score" 4) "99"
127.0.0.1:6379[1]> hkeys user:1 1) "name" 2) "score" 127.0.0.1:6379[1]> hvals user:1 1) "test" 2) "90"
127.0.0.1:6379[1]> hlen user:1 (integer) 2
需要注意的是,散列类型无法获取所有存在的键值,也就是id,如果删除了中间某个id的话,只可以使用exist命令来判断key是否存在。
列表类型
基本操作
列表的基本操作命令都是以l开头的。
127.0.0.1:6379> lpush user test (integer) 1 127.0.0.1:6379> rpush user test1 (integer) 2 127.0.0.1:6379> lpop user "test" 127.0.0.1:6379> rpop user "test1" 127.0.0.1:6379>
127.0.0.1:6379> llen user (integer) 0 127.0.0.1:6379> lpush user test (integer) 1 127.0.0.1:6379> llen user (integer) 1
127.0.0.1:6379> lrange user 0 3 1) "test" 2) "test1" 3) "test2" 4) "test3" 127.0.0.1:6379> lrange user 0 2 1) "test" 2) "test1" 3) "test2"
lrem key count value @count 为删除个数,大于0的话从左边开始,小于0的话从右边开始 @value 要删除的值
127.0.0.1:6379> lrange user 0 -1 1) "test" 2) "test1" 3) "test2" 4) "test3" 127.0.0.1:6379> linsert user before test1 test0 (integer) 5 127.0.0.1:6379> linsert user after test1 test1.5 (integer) 6 127.0.0.1:6379> lrange user 0 -1 1) "test" 2) "test0" 3) "test1" 4) "test1.5" 5) "test2" 6) "test3"
rdroplpush source destination 从source列表的右侧弹出一个数据,添加到destination列表的左侧
集合类型
127.0.0.1:6379> sadd key 1 (integer) 1 127.0.0.1:6379> sadd key 2 (integer) 1 127.0.0.1:6379> sadd key 2 (integer) 0 127.0.0.1:6379> smembers key 1) "1" 2) "2" 127.0.0.1:6379> srem key 2 (integer) 1 127.0.0.1:6379> smembers key 1) "1"
127.0.0.1:6379> sismember key 1 (integer) 1 127.0.0.1:6379> sismember key 3 (integer) 0
sdiff key [key...] sinter key [key...] sunion key [key...]
127.0.0.1:6379> scard key (integer) 1
sdiffstore destination key [key...] sinterstore destination key[key...] sunionstore destination key[key...]
srandmember key [count] 127.0.0.1:6379> srandmember key 3 1) "4" 2) "2" 3) "3"
有序集合类型 sorted set
基本操作
(1)zadd添加元素,并设置分数score
127.0.0.1:6379> zadd students 60 xiaoming (integer) 1 127.0.0.1:6379> zadd students 80 daxiong (integer) 1
127.0.0.1:6379> zscore students xiaoming "60"
127.0.0.1:6379> zrange students 0 1 1) "xiaoming" 2) "daxiong"
127.0.0.1:6379> zrangebyscore students 0 70 1) "xiaoming" 127.0.0.1:6379> zrangebyscore students 0 100 1) "xiaoming" 2) "daxiong"
获取从0开始的前2个数据 127.0.0.1:6379> zrangebyscore students 0 100 limit 0 2 1) "xiaoming" 2) "daxiong" 获取从1开始的前2个数据 127.0.0.1:6379> zrangebyscore students 0 100 limit 1 2 1) "daxiong"
127.0.0.1:6379> zincrby students 5 xiaoming "65" 127.0.0.1:6379> zincrby students -5 xiaoming "60"
127.0.0.1:6379> zcard students (integer) 2
zcount key min max 127.0.0.1:6379> zcount students 0 70 (integer) 1 127.0.0.1:6379> zcount students 0 90 (integer) 2
zrem key [key...]
zremrangebyrank key start stop
zremrangebyscore key min max
127.0.0.1:6379> zrank students xiaoming
(integer) 0
事务
redis的事务是一组命令的集合。事务同命令一样都是redis的最小执行单元,一个事务中的命令要么执行要么都不执行。
首先需要multi命令来开始事务,用exec命令来执行事务。
127.0.0.1:6379> multi OK 127.0.0.1:6379> hset user:1 name xiaoming QUEUED 127.0.0.1:6379> hset user:1 name daxiong QUEUED 127.0.0.1:6379> exec 1) (integer) 0 2) (integer) 0 127.0.0.1:6379> hgetall user:1 1) "name" 2) "daxiong" 3) "score" 4) "61"
watch
127.0.0.1:6379> set key 1 OK 127.0.0.1:6379> watch key OK 127.0.0.1:6379> set key 2 OK 127.0.0.1:6379> multi OK 127.0.0.1:6379> set key 3 QUEUED 127.0.0.1:6379> exec (nil) 127.0.0.1:6379> get key "2"
生存时间
expire key seconds //设置成功返回1,失败返回0
127.0.0.1:6379> set session:aabb uid1122 OK 127.0.0.1:6379> expire session:aabb 300 (integer) 1 127.0.0.1:6379> del session:aabb (integer) 1 127.0.0.1:6379> expire session:aabb 300 (integer) 0 127.0.0.1:6379> expire session:aabb 300 (integer) 1 127.0.0.1:6379> ttl session:aabb (integer) 290
ttl key
127.0.0.1:6379> expire session:aabb 300 (integer) 1 127.0.0.1:6379> ttl session:aabb (integer) 290
127.0.0.1:6379> get session:aabb "300" 127.0.0.1:6379> ttl session:aabb (integer) 280 127.0.0.1:6379> persist session:aabb (integer) 1 127.0.0.1:6379> ttl session:aabb (integer) -1
缓存数据
排序
127.0.0.1:6379> lpush list 1 2 6 3 4 9 8 (integer) 7 127.0.0.1:6379> sort list 1) "1" 2) "2" 3) "3" 4) "4" 5) "6" 6) "8" 7) "9"
127.0.0.1:6379> zadd set 50 2 40 3 20 1 60 5 (integer) 4 127.0.0.1:6379> sort set 1) "1" 2) "2" 3) "3" 4) "5"
127.0.0.1:6379> sort set desc 1) "5" 2) "3" 3) "2" 4) "1"
BY参数
127.0.0.1:6379> lpush userids 1 2 3 (integer) 3
127.0.0.1:6379> set user_score_1 50 OK 127.0.0.1:6379> set user_score_2 30 OK 127.0.0.1:6379> set user_score_3 70 OK
127.0.0.1:6379> sort userids 1) "1" 2) "2" 3) "3" 127.0.0.1:6379> sort userids by user_score_* 1) "2" 2) "1" 3) "3" 127.0.0.1:6379> sort userids by user_score_* desc 1) "3" 2) "1" 3) "2"
GET参数
127.0.0.1:6379> sort userids by user_score_* get user_name_* 1) "xiaoming" 2) "daxiong" 3) "xiaohong" 127.0.0.1:6379> sort userids by user_score_* desc get user_name_* 1) "xiaohong" 2) "daxiong" 3) "xiaoming"
STORE参数
任务队列
BRPOP key timeout
优先级队列
brpop key [key...] timetout
发布/订阅模式
PUBLISH CHANNEL MESSAGE
SUBSCRIBE CHANNEL MESSAGE
#向channel.test发布消息 127.0.0.1:6379> publish channel.test hello (integer) 0 #返回0表明订阅者为0,没有发布消息 127.0.0.1:6379> publish channel.test hello (integer) 1 #返回n表明订阅者为n,成功发布给1个消费者
#订阅channel.test消息 127.0.0.1:6379> subscribe channel.test Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channel.test" 3) (integer) 1
#接收到来自channel.test的消息 1) "message" 2) "channel.test" 3) "hello"
管道
节省空间
以上是关于Redis高级命令操作大全--推荐的主要内容,如果未能解决你的问题,请参考以下文章