字符串类型是Redis中最为基础的数据存储类型,它在Redis中是二进制安全的,这便意味着该类型可以接受任何格式的数据,如JPEG图像数据或Json对象描述信息等。在Redis中字符串类型的Value最多可以容纳的数据长度是512M。
1、SET key value
此命令用于在指定键设置值
redis 127.0.0.1:> set name tom
OK
redis 127.0.0.1:> get name
"tom"
2、GET key
用于获取指定键的值
redis 127.0.0.1:> set name tom
OK
redis 127.0.0.1:> get name
"tom"
3、MSET key1 value1 key2 value2 [...]
设置多个键和值
redis 127.0.0.1:> mset name XiaoMing language Madarin address China
OK
redis 127.0.0.1:> mget name language address
) "XiaoMing"
) "Madarin"
4、MGET key1[key2...]
得到所有给定键的值(Multi GET)
redis 127.0.0.1:> set name tom
OK
redis 127.0.0.1:> set age
OK
redis 127.0.0.1:> set addr America
OK
redis 127.0.0.1:> mget name age addr
) "tom"
) ""
) "America"
5、SETNX key value
设置键的值,只有当该键不存在
redis 127.0.0.1:> set car benz
OK
redis 127.0.0.1:> setnx car Jelly
(integer)
redis 127.0.0.1:> get car
"benz"
6、MSETNX key value [key value ...]
设置多个键多个值,只有在当没有按键的存在时
redis 127.0.0.1:> set food pizza
OK
redis 127.0.0.1:> set fruit banana
OK
redis 127.0.0.1:> msetnx food rice fruit apple
(integer)
redis 127.0.0.1:> get food
"pizza"
redis 127.0.0.1:> get fruit
"banana"
注意:如果设置的值其中一个不成功,那么所有都不会成功。
7、INCR key
增加键的整数值一次
redis 127.0.0.1:> set connections
OK
redis 127.0.0.1:> incr connections
(integer)
redis 127.0.0.1:> get connections
""
8、INCRBY key increment
由给定的数量递增键的整数值
redis 127.0.0.1:> get connections
""
redis 127.0.0.1:> incrby connections
(integer)
9、DECR key
递减键一次的整数值
redis 127.0.0.1:> get connections
""
redis 127.0.0.1:> decr connections
(integer)
10、DECRBY key decrement
由给定数目递减键的整数值
redis 127.0.0.1:> get connections
""
redis 127.0.0.1:> decrby connections
(integer)
11、STRLEN key
得到存储在键的值的长度
redis 127.0.0.1:6379> set language America
OK
redis 127.0.0.1:6379> strlen language
(integer) 7
12、GETRANGE key start end
截取指定key对应的值
redis 127.0.0.1:6379> set article 123456
OK
redis 127.0.0.1:6379> getrange article 0 2
"123"
13、APPEND key value
追加值到一个键
redis 127.0.0.1:6379> set hello world
OK
redis 127.0.0.1:6379> append hello -happy
(integer) 11
redis 127.0.0.1:6379> get hello
"world-happy"
14、GETSET key value
设置键的字符串值,并返回旧值
redis 127.0.0.1:6379> get name
"tom"
redis 127.0.0.1:6379> getset name marry
"tom"
redis 127.0.0.1:6379> get name
"marry"
redis 127.0.0.1:6379>
参考:http://www.cnblogs.com/stephen-liu74/archive/2012/03/14/2349815.html