下面这篇文章主要介绍redis常用命令
一、Redis数值常用命令操作
1、String类型常用操作
[root@master ~]# redis-cli -a 123456
127.0.0.1:6379> set key1 123
OK
127.0.0.1:6379> get key1
"123"
127.0.0.1:6379> set key1 456 //一个key对应一个value,多次赋值,会覆盖前面的value
"456"
127.0.0.1:6379>
127.0.0.1:6379> setnx key1 aaa
(integer) 0
127.0.0.1:6379> setnx key2 aaa
(integer) 1
127.0.0.1:6379> get key2
"aaa"
说明:setnx如果key存在,则返回0,不存在会直接创建这个key。
127.0.0.1:6379> setex key3 100 bbb //给key设置过期时间100秒
127.0.0.1:6379> get key3
"bbb"
127.0.0.1:6379> ttl key3 //查看剩余生存时间
(integer) 89
127.0.0.1:6379> ttl key3
(integer) 84
(integer) 70
127.0.0.1:6379> mset key1 a key2 b key3 c //批量创建
127.0.0.1:6379> mget key1 key2 key3
1) "a"
2) "b"
3) "c"
2、Hash类型常用操作
127.0.0.1:6379> hset hash1 name tpp //建立hash
127.0.0.1:6379> hset hash1 age 25
127.0.0.1:6379> hset hash1 job it
127.0.0.1:6379> hgetall hash1
1) "name"
2) "tpp"
3) "age"
4) "25"
5) "job"
6) "it"
127.0.0.1:6379> hget hash1 name
"tpp"
127.0.0.1:6379> hmset hash2 name xiaoming age 10 job student //批量建立键值对
127.0.0.1:6379> hgetall hash2
2) "xiaoming"
4) "10"
6) "student"
127.0.0.1:6379> hmget hash2 name age job
1) "xiaoming"
2) "10"
3) "student"
127.0.0.1:6379> hdel hash2 job //删除指定的filed
127.0.0.1:6379> hkeys hash2 //打印所有的key
2) "age"
127.0.0.1:6379> hvals hash2 //打印所有的values
127.0.0.1:6379> hlen hash2 //查看hash有几个filed
(integer) 2
3、List类型常用操作
127.0.0.1:6379> lpush list1 aaa //从左侧压入一个元素
127.0.0.1:6379> lpush list1 bbb
127.0.0.1:6379> lpush list1 ccc
(integer) 3
127.0.0.1:6379> lrange list1 0 -1 //0为头,-1为尾
1) "ccc"
2) "bbb"
3) "aaa"
127.0.0.1:6379> lpop list1 //从左侧取出
"ccc"
127.0.0.1:6379> lrange list1 0 -1
1) "bbb"
2) "aaa"
127.0.0.1:6379> rpush list1 1 //从右侧压入一个元素
127.0.0.1:6379> rpush list1 2
(integer) 4
127.0.0.1:6379> rpush list1 3
(integer) 5
3) "1"
4) "2"
5) "3"
127.0.0.1:6379> rpop list1 //从右侧取出
"3"
127.0.0.1:6379> linsert list1 before aaa ccc //在元素aaa前面插入一个元素ccc
2) "ccc"
4) "1"
5) "2"
127.0.0.1:6379> lset list1 4 ddd //把第五个元素修改为ddd
5) "ddd"
127.0.0.1:6379> lindex list1 2 //查看第3个元素
127.0.0.1:6379> llen list1 //查看链表中有几个元素
4、Set类型常用操作
127.0.0.1:6379> sadd set1 a //向集合set1中放入元素
127.0.0.1:6379> sadd set1 b
127.0.0.1:6379> sadd set1 c
127.0.0.1:6379> sadd set1 1
127.0.0.1:6379> sadd set1 2
127.0.0.1:6379> sadd set1 3
127.0.0.1:6379> smembers set1 //查看集合中的所有元素
1) "1"
2) "c"
3) "a"
6) "b"
127.0.0.1:6379> srem set1 c //删除元素c
127.0.0.1:6379> smembers set1
2) "2"
3) "3"
5) "b"
127.0.0.1:6379> spop set1 //随机取出一个元素,删除
"a"
127.0.0.1:6379> spop set1
"1"
1) "2"
2) "3"
3) "b"
127.0.0.1:6379> sadd set2 2
127.0.0.1:6379> sadd set2 a
127.0.0.1:6379> sadd set2 b
127.0.0.1:6379> smembers set2
127.0.0.1:6379> sdiff set1 set2 //求差集,以set1为标准
1) "3"
127.0.0.1:6379> sdiff set2 set1 //求差集,以set2为标准
127.0.0.1:6379> sdiffstore set3 set1 set2 //求差集并存储到 set3
127.0.0.1:6379> smembers set3
127.0.0.1:6379> sinter set1 set2 //求交集
127.0.0.1:6379> sinterstore set4 set1 set2 //求交集并存储到 set4
127.0.0.1:6379> smembers set4
127.0.0.1:6379> sunion set1 set2 //求并集
4) "a"
127.0.0.1:6379> sunionstore set5 set1 set2 //求并集并存储到 set5
127.0.0.1:6379> smembers set5
127.0.0.1:6379> sismember set5 2 //判断一个元素是否属于一个集合,属于返回1
127.0.0.1:6379> sismember set5 c //不属于返回0
127.0.0.1:6379> srandmember set2 //随机取出一个元素,但不删除
127.0.0.1:6379> srandmember set2
"2"
"b"
5、Zset类型常用操作
127.0.0.1:6379> zadd zset1 1 aaa //创建有序集合
127.0.0.1:6379> zadd zset1 10 bbb
127.0.0.1:6379> zadd zset1 5 ccc
127.0.0.1:6379> zadd zset1 20 ddd
127.0.0.1:6379> zadd zset1 100 eee
127.0.0.1:6379> zrange zset1 0 -1 //顺序显示所有元素
1) "aaa"
3) "bbb"
4) "ddd"
5) "eee"
127.0.0.1:6379> zrange zset1 0 -1 withscores //顺序显示所有元素和分值
2) "1"
3) "ccc"
4) "5"
5) "bbb"
6) "10"
7) "ddd"
8) "20"
9) "eee"
10) "100"
127.0.0.1:6379> zrevrange zset1 0 -1 //反序显示所有元素
1) "eee"
2) "ddd"
4) "ccc"
127.0.0.1:6379> zrem zset1 aaa //删除指定元素
127.0.0.1:6379> zrange zset1 0 -1
3) "ddd"
4) "eee"
127.0.0.1:6379> zrank zset1 ccc //按scores正向返回元素索引值,从0开始
127.0.0.1:6379> zrevrank zset1 ccc //按scores反向返回元素索引值
127.0.0.1:6379> zcard zset1 //返回集合中所有元素的个数
127.0.0.1:6379> zcount zset1 1 10 //返回分值范围1-10的元素个数
127.0.0.1:6379> zrangebyscore zset1 1 10 //返回分值范围1-10的元素
127.0.0.1:6379> zrangebyscore zset1 1 10 withscores //同上,并显示score值
2) "5"
127.0.0.1:6379> zremrangebyrank zset1 0 2 //删除索引范围0-2的元素,按score正向
127.0.0.1:6379> zremrangebyscore zset1 10 100 //删除分值范围10-100的元素
(empty list or set)
二、Redis键值相关操作
127.0.0.1:6379> keys * //取出所有key
1) "hash1"
2) "key2"
3) "set1"
4) "key4"
5) "key1"
6) "key3"
7) "zset1"
8) "list1"
127.0.0.1:6379> keys key* //模糊匹配
1) "key2"
2) "key4"
3) "key1"
4) "key3"
127.0.0.1:6379> exists set1 //键存在,返回1
127.0.0.1:6379> exists set2 //键不存在,返回0
127.0.0.1:6379> del key1 //删除一个key,成功返回1,否则返回0
127.0.0.1:6379> expire key2 20 //设置key2的剩余过期时间为20s
127.0.0.1:6379> get key2
"abcd"
127.0.0.1:6379> ttl key2 //查看过期时间,单位为s ,正数为过期时间
(integer) 8
127.0.0.1:6379> ttl key2 //返回-2,表示该键不存在
(integer) -2
127.0.0.1:6379> ttl key3 //返回-1,表示该键没有设置剩余过期时间
(integer) -1
127.0.0.1:6379> expire key3 20
127.0.0.1:6379> persist key3 //取消过期时间
127.0.0.1:6379> select 2 //切换到库2,默认进入0库,系统总共16个库
127.0.0.1:6379[2]> set key5 123
127.0.0.1:6379> move set1 2 //把set1移到库2中
127.0.0.1:6379> select 2
127.0.0.1:6379[2]> keys *
1) "key5"
2) "set1"
127.0.0.1:6379> keys *
3) "key3"
4) "zset1"
5) "list1"
127.0.0.1:6379> randomkey //随机返回一个key
"key4"
127.0.0.1:6379> randomkey
"hash1"
"zset1"
127.0.0.1:6379> rename list1 list2 //把list1重命名为list2
2) "list2"
3) "key4"
5) "zset1"
127.0.0.1:6379> type zset1 //返回键的类型
zset
127.0.0.1:6379> type hash1
hash
三、Redis服务相关操作
127.0.0.1:6379> dbsize //返回当前数据库中key的数目
127.0.0.1:6379> info //返回redis数据库状态信息
127.0.0.1:6379> flushdb //清空当前数据库中所有的键
127.0.0.1:6379> flushall //清空所有数据库中的所有的键
本文转自 M四月天 51CTO博客,原文链接:http://blog.51cto.com/msiyuetian/1719979,如需转载请自行联系原作者