获得符合规则的健名列表 KEYS 命令
KEYS pattern
pattern支持glob 风格通配符格式,规则如下,图片均截取与《Redis入门指南》一书,下面就不做说明了:
127.0.0.1:6379> KEYS *
"name"
注:keys * 命令能获得Redis中所有的键,但需要遍历Redis中的所有键,当键较多时,会影响性能。
注:Redis不区分命令大小写
判断一个键是否存在 EXISTS 命令
EXISTS key
键存在返回1,否则返回0。
127.0.0.1:6379> EXISTS name
(integer) 1
127.0.0.1:6379> EXISTS eee
(integer) 0
删除键 DEL 命令
DEL key
可以删除一个或多个键,返回值是删除的键的个数。
127.0.0.1:6379> DEL name
(integer) 1
127.0.0.1:6379> DEL name
(integer) 0
获得键值的数据类型 TYPE 命令
TYPE key
TYPE 命令用来获得键值的数据类型,返回值可能是 string (字符串类型)、hash (散列类型) 、list (列表类型) 、set(集合类型)、zset(有序集合类型)。
127.0.0.1:6379> TYPE name
string
取值、赋值命令
SET key value
GET key
127.0.0.1:6379> SET key hello
OK
127.0.0.1:6379> GET key
"hello"
当键不存在时返回空。
递增数字 INCR 命令
INCR key
127.0.0.1:6379> INCR num
(integer) 1
127.0.0.1:6379> INCR num
(integer) 2
当要操作的键不存在时会默认键值为0,当键值不是整数时Redis 会提示错误:
127.0.0.1:6379> SET foo lorem
OK
127.0.0.1:6379> INCR foo
(error) ERR value is not an integer or out of range
增加指定的整数 INCRBY 命令
INCRBY key increment
127.0.0.1:6379> INCRBY age 3
(integer) 3
127.0.0.1:6379> INCRBY age 6
(integer) 9
减少指定的整数 DECR 命令
DECR key
DECR key decrement
127.0.0.1:6379> DECR age
(integer) 8
127.0.0.1:6379> DECRBY age 4
(interger) 4
下面就不一一举例了:
增加指定浮点数
INCRBYFLOAT key increment
向尾部追加值
APPEND key value
获取字符串长度
STRLEN KEY
同时获得/设置多个键值
MGET key [key .....]
MSET key value [key value ....]
位操作
GETBIT key offset
SETBIT key offset
BITCOUNT key [start] [end]
BITOP operation destkey key [key ...]
127.0.0.1:6379> SET foo1 bar
OK
127.0.0.1:6379> SET foo2 aar
127.0.0.1:6379> BITOP OR res foo1 foo2
(integer) 3
127.0.0.1:6379> GET res
"car"
运算过程如图:
字符串类型是Redis中最基本的数据类型,它能存储任何形式的字符串,包括二进制数据。你可以用其存储用户的邮箱,JOSN化的对象甚至是一张图片。一个字符串类型键允许存储的数据的最大容量是512MB。
字符串类型是其它4中数据类型的基础,其它数据类型和字符串类型的差别从某种角度来说只是组织字符串的形式不同。