Redis中的数据类型
Redis命令不区分大小写。
2.1、入门命令
2.1.1获得符合规则的键名列表
KEYS pattern
pattern支持glob风格通配符格式:
? 匹配一个字符
* 匹配任意字符(包括0个字符)
[] 匹配中括号中的字符,可以使用“-”表示一个区间,如a[a-c]可以表示“aa”,“ab”和“ac”。
\x 匹配字符x,用于转义符号。如要匹配“?”就需要使用\?
KEYS需要遍历Redis中的所有键,如果键数量比较多会影响性能,所以一般不在生产环境使用这个命令。
2.1.2判断一个键是否存在
EXISTS key:如果键存在则返回整数类型1,否则返回0。
2.1.3删除键
DEL key[key…]:可以删除一个或多个键,返回删除键的个数。
2.1.4获得键值的数据类型
TYPE key:用来获取键值的数据类型,包括string(字符串类型)、hash(散列类型)、list(列表类型)、set(集合类型)、zset(有序集合类型)。
2.2字符串类型
2.2.1命令
2.2.1.1赋值与取值
SET key value
GET key
2.2.1.2递增数字
INCR key
当存储的字符串是整数形式时,使用INCR可以将当前键值递增,如果要操作的键不存在会默认键值是0,所以第一次递增后的结果是1,如果键值不是整数会提示错误。
2.2.1.3增加指定的整数
INCRBY keyincrement
指定键值增加的值
2.2.1.4减少指定的值
DECR key
DECRBY key increment
2.2.1.5增加浮点数
INCRBYFLOAT keyincrement
INCRBYFLOAT foo2.5
INCRBYFLOAT foo5E+4
2.2.1.6向尾部增加值
APPEND key value
APPEND用于向键值的末尾追加value,如果键不存在则将该键的值设为value,即相当于SET key value,返回值是追加后字符串的长度。
2.2.1.7获取字符串长度
STRLEN key
如果键不存在就返回0。
2.2.1.8同时获取/设置多个键值
MGET key [key …]
MSET key value [keyvalue …]
2.2.1.9位操作
GETBIT keyoffset
SETBIT keyoffset value
BITCOUNT key [start][end]
BITOP operationdestkey key [key …]
一个字节由8个二进制位组成,GETBIT命令可以获取键指定位置的二进制位的值(0或1),索引从0开始,如果获取的索引超过了键值的二进制位的长度则默认位值是0。
SETBIT命令可以设置键值的二进制形式的指定位置的值,返回是该位置的旧值。
如果设置的位置超过了键值的二进制位的长度,SETBIT命令会自动将中间位置的二进制位设为0。
BITCOUNT命令可以获取字符串类型键中值是1的二进制位个数。
可以通过参数限制统计的字节范围,比如BITCOUNT foo 0 1就是只统计前2个字节中位值是1的个数。
BITOP命令可以对多个字符串类型键进行位运算,并将结果存储在destkey参数指定的键中,BITOP支持的运算操作包括AND、OR、XOR、NOT。
、
127.0.0.1:6379> get foo
(nil)
127.0.0.1:6379> get FOO
"1"
127.0.0.1:6379> keys *
1) "FOO"
127.0.0.1:6379> keys pattern
(empty list or set)
127.0.0.1:6379> keys ?
(empty list or set)
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set foo
(error) ERR wrong number of arguments for 'set' command
127.0.0.1:6379> incr foo
(integer) 1
127.0.0.1:6379> set bar 1
OK
127.0.0.1:6379> EXISTS foo
(integer) 1
127.0.0.1:6379> EXISTS FOO
(integer) 0
127.0.0.1:6379> DEL foo
(integer) 1
127.0.0.1:6379> DEL foo
(integer) 0
127.0.0.1:6379> INCR foo
(integer) 1
127.0.0.1:6379> TYPE foo
string
127.0.0.1:6379> LPUSH bar 1
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> TYPE bar
string
127.0.0.1:6379> SET KEY hello
OK
127.0.0.1:6379> GET hello
(nil)
127.0.0.1:6379> SET key hello
OK
127.0.0.1:6379> GET key
"hello"
127.0.0.1:6379> type foo
string
127.0.0.1:6379> incr foo
(integer) 2
127.0.0.1:6379> get foo
"2"
127.0.0.1:6379> INCR key
(error) ERR value is not an integer or out of range
127.0.0.1:6379> incr posts:count
(integer) 1
127.0.0.1:6379> get foo
"2"
127.0.0.1:6379> INCRBY foo 3
(integer) 5
127.0.0.1:6379> get foo
"5"
127.0.0.1:6379> DECR foo
(integer) 4
127.0.0.1:6379> DECRBY foo 5
(integer) -1
127.0.0.1:6379> INCRBYFLOAT foo 2.5
"1.5"
127.0.0.1:6379> INCRBYFLOAT foo 5E+4
"50001.5"
127.0.0.1:6379> SET key hello
OK
127.0.0.1:6379> APPEND key " world!"
(integer) 12
127.0.0.1:6379> get key
"hello world!"
127.0.0.1:6379> STRLEN key
(integer) 12
127.0.0.1:6379> MGET a 1 b 2 c 3
1) (nil)
2) (nil)
3) (nil)
4) (nil)
5) (nil)
6) (nil)
127.0.0.1:6379> MSET a 1 b 2 c 3
OK
127.0.0.1:6379> MGET a b c
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> get bar
"1"
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> GETBIT foo 0
(integer) 0
127.0.0.1:6379> GETBIT foo 1
(integer) 1
127.0.0.1:6379> GETBIT foo 2
(integer) 1
127.0.0.1:6379> GET foo 10000
(error) ERR wrong number of arguments for 'get' command
127.0.0.1:6379> GETBIT foo 10000
(integer) 0
127.0.0.1:6379> SETBIT foo 6 0
(integer) 1
127.0.0.1:6379> get foo
"`ar"
127.0.0.1:6379> SETBIT foo 7 1
(integer) 0
127.0.0.1:6379> get foo
"aar"
127.0.0.1:6379> BITCOUNT foo
(integer) 10
127.0.0.1:6379> BITCOUNT foo 0 1
(integer) 6
127.0.0.1:6379> SET foo1 bar
OK
127.0.0.1:6379> SET foo2 aar
OK
127.0.0.1:6379> BITOP OR res foo1 foo2
(integer) 3
127.0.0.1:6379> get res
"car"
127.0.0.1:6379>
2.3散列类型