字符创常见操作
[[email protected] ~]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set string1 "hello the word" //设置字符串变量
OK
127.0.0.1:6379> get string1 //查看字符串变量
"hello the word"
127.0.0.1:6379> set string2 "hello" ex 5 //设置字符串变量,并设置过期时间为5秒
OK
127.0.0.1:6379> get string2 //查看字符串变量
"hello"
127.0.0.1:6379> get string2 //字符串过期后,查看该值为空
(nil)
127.0.0.1:6379> get string1
"hello the word"
127.0.0.1:6379> set string1 hello nx //仅当string1不存在时,才执行set指令
(nil)
127.0.0.1:6379> set string1 hello xx //仅当string1存在时,才执行set指令
OK
127.0.0.1:6379> get string1 //查看修改后string1的值
"hello"
127.0.0.1:6379> set string1 "hello the world" //修改string1的值
OK
127.0.0.1:6379> get string1
"hello the world"
127.0.0.1:6379> setrange string1 6 "Redis" //从第6个字符开始替换string1的值
(integer) 15
127.0.0.1:6379> get string1
"hello Redisorld"
127.0.0.1:6379> strlen string1 //计算string1的长度
(integer) 15
127.0.0.1:6379> append string1 xxx //对string1进行追加操作
(integer) 18
127.0.0.1:6379> get string1
"hello Redisorldxxx"
127.0.0.1:6379> append string1 " xxx"
(integer) 22
127.0.0.1:6379> get string1
"hello Redisorldxxx xxx"
127.0.0.1:6379> setbit string2 0 1 //按位设置string2的值,0位为1
(integer) 0
127.0.0.1:6379> setbit string2 1 1 //按位设置string2的值,1位为1
(integer) 0
127.0.0.1:6379> setbit string2 2 1
(integer) 0
127.0.0.1:6379> setbit string2 3 0
(integer) 0
127.0.0.1:6379> get string2 //不可以查看所有的值
"\xe0"
127.0.0.1:6379> bitcount string2 //统计string2中1的个数
(integer) 3
127.0.0.1:6379> getbit string2 0 //查看string2第0位的值
(integer) 1
127.0.0.1:6379> getbit string2 1 //查看string2第1位的值
(integer) 1
127.0.0.1:6379> decr string3 //递减运算,初始值为0
(integer) -1
127.0.0.1:6379> decr string3
(integer) -2
127.0.0.1:6379> decr string3
(integer) -3
127.0.0.1:6379> decr string3
(integer) -4
127.0.0.1:6379> decr string3
(integer) -5
127.0.0.1:6379> set string4 10 //自定义变量初始值为10
OK
127.0.0.1:6379> decr string4 //对自定义变量进行递减运算
(integer) 9
127.0.0.1:6379> decr string4
(integer) 8
127.0.0.1:6379> decr string4
(integer) 7
127.0.0.1:6379> decrby string4 2 //对变量进行递减2运算
(integer) 5
127.0.0.1:6379> decrby string4 2
(integer) 3
127.0.0.1:6379> get string4
"3"
127.0.0.1:6379> set string5 "hello the world" //设置字符串变量
OK
127.0.0.1:6379> getrange string5 0 4 //查看字串的第0至第4位
"hello"
127.0.0.1:6379> getrange string5 -3 -1 //查看字串的倒数第3位至倒数第1位
"rld"
127.0.0.1:6379> incr page //对变量进行递增运算,初始值为0
(integer) 1
127.0.0.1:6379> incr page
(integer) 2
127.0.0.1:6379> incr page
(integer) 3
127.0.0.1:6379> incr page
(integer) 4
127.0.0.1:6379> set string6 10 //设置字符串变量为10
OK
127.0.0.1:6379> incr string6 //对变量进行递增运算
(integer) 11
127.0.0.1:6379> incr string6
(integer) 12
127.0.0.1:6379> incrby string6 2 //对变量进行递增2运算
(integer) 14
127.0.0.1:6379> incrby string6 2
(integer) 16
127.0.0.1:6379> incrby string6 2
(integer) 18
127.0.0.1:6379> incrby string6 2
(integer) 20
127.0.0.1:6379> set num 16.1 //设置浮点数变量
OK
127.0.0.1:6379> incrbyfloat num 1.1 //对浮点数进行递增1.1运算
"17.2"
127.0.0.1:6379> incrbyfloat num 1.1
"18.3"
127.0.0.1:6379> incrbyfloat num 1.1
"19.4"
127.0.0.1:6379> incrbyfloat num 1.1
"20.5"
Hash表常见操作指南
127.0.0.1:6379> hset hkey google “www.g.cn”
//设置hash表hkey,google列的值为www.g.cn
(integer) 1
127.0.0.1:6379> hset hkey baidu “www.baidu.com”
//设置hash表hkey,baidu列的值为www.baidu.com
(integer) 1
127.0.0.1:6379> hget hkey google //查看hash表hkey中google列的值
"www.g.cn"
127.0.0.1:6379> hget hkey baidu //查看hash表hkey中baidu列的值
"www.baidu.com"
127.0.0.1:6379> hmset site google "www.g.cn" baidu "www.baidu.com"
OK
//一次性设置hash表site的多个列与值
127.0.0.1:6379> hmget site google baidu
1) "www.g.cn"
2) "www.baidu.com"
//一次性查看hash表site的多个列值
127.0.0.1:6379> hgetall site //查看site表中所有的列与值
1) "google"
2) "www.g.cn"
3) "baidu"
4) "www.baidu.com"
127.0.0.1:6379> hdel site google //删除site表中google列
(integer) 1
127.0.0.1:6379> hgetall site //验证删除效果
1) "baidu"
2) "www.baidu.com"
127.0.0.1:6379> hmset site google "www.g.cn" baidu "www.baidu.com" sina "www.sina.com"
OK
127.0.0.1:6379> hkeys site //查看site表的所有列
1) "baidu"
2) "google"
3) "sina"
127.0.0.1:6379> hvals site //查看site表中所有列的值
1) "www.baidu.com"
2) "www.g.cn"
3) "www.sina.com"
127.0.0.1:6379> hmget site google baidu //一次性查看site表中的多个列值
1) "www.g.cn"
2) "www.baidu.com"
List列表常见操作指南
127.0.0.1:6379> lpush list1 a b c //创建列表并赋值
(integer) 3
127.0.0.1:6379> lpush list2 a //创建列表并赋值
(integer) 1
127.0.0.1:6379> lpush list2 b //给列表追加新值
(integer) 2
127.0.0.1:6379> lpush list2 c //给列表追加新值
(integer) 3
127.0.0.1:6379> lrange list1 0 -1
//查看列表list1中的所有值,从0位到最后1位
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> lrange list1 1 2 //查看列表中第1和2位的值
1) "b"
2) "a"
127.0.0.1:6379> lrange list1 1 -1 //查看列表中第1至最后1位的值
1) "b"
127.0.0.1:6379> lrange list2 0 -1 //查看所有的值
1) "a"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> lrange list2 0 -1
1) "t"
2) "a"
3) "c"
4) "b"
5) "a"
127.0.0.1:6379> lpop list2
//返回list2列表头元素数据,并将该值从列表中删除,key不存在则返回nil
"t"
127.0.0.1:6379> lrange list2 0 -1 //验证结果
1) "a"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> lpop list2 //接续删除头部元素
"a"
127.0.0.1:6379> lrange list2 0 -1 //验证结果
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> lpop list2
"c"
127.0.0.1:6379> lrange list2 0 -1
1) "b"
2) "a"
127.0.0.1:6379> lrange list4 0 -1 //查看全部数据如下
1) "f"
2) "e"
3) "d"
4) "a"
5) "c"
127.0.0.1:6379> lindex list4 0 //返回list4中第0个值
"f"
127.0.0.1:6379> lindex list4 1 //返回list4中第1个值
"e"
127.0.0.1:6379> lindex list4 -1 //返回list4中最后1个值
"c"
127.0.0.1:6379> lindex list4 -2 //返回list4中倒数第2个值
"a"
127.0.0.1:6379> lrange list4 0 -1
1) "f"
2) "e"
3) "d"
4) "a"
5) "c"
127.0.0.1:6379> lset list4 0 test //给list4的第0为插入值,值为test
OK
127.0.0.1:6379> lrange list4 0 -1 //验证结果
1) "test"
2) "d"
3) "a"
其他操作指南
127.0.0.1:6379> set mykey "hello" //定义字符串变量
OK
127.0.0.1:6379> get mykey //查看变量
"hello"
127.0.0.1:6379> del mykey //删除变量
(integer) 1
127.0.0.1:6379> get mykey //验证结果
(nil)
127.0.0.1:6379> set mykey "hello" //定义变量即值
OK
127.0.0.1:6379> get mykey //查看有值
"hello"
127.0.0.1:6379> get mykey
"hello"
127.0.0.1:6379> expire mykey 10 //定义过期时间
(integer) 1
127.0.0.1:6379> get mykey //10秒后查看,无值
(nil)
127.0.0.1:6379> set mykey "hello" //设置变量
OK
127.0.0.1:6379> persist mykey //重新定义过期时间为,永久有效
(integer) 1
127.0.0.1:6379> get mykey
"hello"
127.0.0.1:6379> get mykey
"hello"
127.0.0.1:6379> ttl mykey
(integer) -1 //永不过期
127.0.0.1:6379> expire mykey 10 //定义过期时间
(integer) 1
127.0.0.1:6379> ttl mykey //查看过期时间
(integer) 9
127.0.0.1:6379> ttl mykey
(integer) 8
127.0.0.1:6379> ttl mykey
(integer) 7
127.0.0.1:6379> ttl mykey
(integer) 6
127.0.0.1:6379> ttl mykey
(integer) 5
127.0.0.1:6379> ttl mykey
(integer) 4
127.0.0.1:6379> ttl mykey
(integer) 3
127.0.0.1:6379> ttl mykey
(integer) 3
127.0.0.1:6379> ttl mykey
(integer) 2
127.0.0.1:6379> ttl mykey
(integer) 1
127.0.0.1:6379> ttl mykey
(integer) -2 //已经过期
127.0.0.1:6379> get mykey //查看mykey的值已经为空
(nil)
127.0.0.1:6379> set mykey "hello"
OK
127.0.0.1:6379> keys //查看数据库下所有数据
1) "string6"
2) "list7"
3) "mykey"
4) "string4"
5) "db"
6) "num"
7) "result"
8) "hkey"
9) "string5"
10) "string1"
11) "bit1"
12) "page"
13) "bit2"
14) "site"
15) "string2"
16) "list1"
17) "string3"
18) "list6"
127.0.0.1:6379> keys li
1) "list7"
2) "list1"
3) "list6"
127.0.0.1:6379> keys s
1) "string6"
2) "string4"
3) "string5"
4) "string1"
5) "site"
6) "string2"
7) "string3"
127.0.0.1:6379> keys string[15] //查看string1或string5
1) "string5"
2) "string1"
127.0.0.1:6379> keys string[0-9] //查看string0值9的数据
1) "string6"
2) "string4"
3) "string5"
4) "string1"
5) "string2"
6) "string3"
127.0.0.1:6379> keys ?it //使用通配符所有数据
1) "bit1"
2) "bit2"
127.0.0.1:6379> select 1 //进入1数据库,默认数据库为0
OK
127.0.0.1:6379[1]> keys //在新数据库中查看数据为空
(empty list or set)
127.0.0.1:6379[1]> set test "test" //在数据库1中创建变量
OK
127.0.0.1:6379[1]> get test //查看变量的值
"test"
127.0.0.1:6379[1]> select 2 //进入2数据库
OK
127.0.0.1:6379[2]> keys //查看所有数据
(empty list or set)
127.0.0.1:6379[2]> select 1
OK
127.0.0.1:6379[1]> keys
1) "test"
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> keys
1) "string6"
2) "list7"
3) "mykey"
4) "string4"
5) "db"
6) "num"
7) "result"
8) "hkey"
9) "string5"
10) "string1"
11) "bit1"
12) "page"
13) "bit2"
14) "site"
15) "string2"
16) "list1"
17) "string3"
18) "list6"
127.0.0.1:6379> flushall //清空所有数据
OK
127.0.0.1:6379> keys //验证结果
127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> set mykey "hello"
OK
127.0.0.1:6379> keys
1) "mykey"
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> keys
1) "mykey"
127.0.0.1:6379> move mykey 1 //将数据库0中的mykey变量移动至数据库1
(integer) 1
127.0.0.1:6379> keys //在数据库0中查看为空
(empty list or set)
127.0.0.1:6379> select 1 //进入数据库1
OK
127.0.0.1:6379[1]> keys //查看所有数据库
1) "mykey"
127.0.0.1:6379[1]> lpush cost 1 8 7 2 5 //创建列表cost
(integer) 5
127.0.0.1:6379[1]> sort cost //对列表值进行排序
1) "1"
2) "2"
3) "5"
4) "7"
5) "8"
原文地址:http://blog.51cto.com/13587169/2069586