127.0.0.1:6379> help @string APPEND key value summary: Append a value to a key since: 2.0.0 BITCOUNT key [start end] summary: Count set bits in a string since: 2.6.0 BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL] summary: Perform arbitrary bitfield integer operations on strings since: 3.2.0 BITOP operation destkey key [key ...] summary: Perform bitwise operations between strings since: 2.6.0 BITPOS key bit [start] [end] summary: Find first bit set or clear in a string since: 2.8.7 DECR key summary: Decrement the integer value of a key by one since: 1.0.0 DECRBY key decrement summary: Decrement the integer value of a key by the given number since: 1.0.0 GET key summary: Get the value of a key since: 1.0.0 GETBIT key offset summary: Returns the bit value at offset in the string value stored at key since: 2.2.0 GETRANGE key start end summary: Get a substring of the string stored at a key since: 2.4.0 GETSET key value summary: Set the string value of a key and return its old value since: 1.0.0 INCR key summary: Increment the integer value of a key by one since: 1.0.0 INCRBY key increment summary: Increment the integer value of a key by the given amount since: 1.0.0 INCRBYFLOAT key increment summary: Increment the float value of a key by the given amount since: 2.6.0 MGET key [key ...] summary: Get the values of all the given keys since: 1.0.0 MSET key value [key value ...] summary: Set multiple keys to multiple values since: 1.0.1 MSETNX key value [key value ...] summary: Set multiple keys to multiple values, only if none of the keys exist since: 1.0.1 PSETEX key milliseconds value summary: Set the value and expiration in milliseconds of a key since: 2.6.0 SET key value [expiration EX seconds|PX milliseconds] [NX|XX] summary: Set the string value of a key since: 1.0.0 SETBIT key offset value summary: Sets or clears the bit at offset in the string value stored at key since: 2.2.0 SETEX key seconds value summary: Set the value and expiration of a key since: 2.0.0 SETNX key value summary: Set the value of a key, only if the key does not exist since: 1.0.0 SETRANGE key offset value summary: Overwrite part of a string at key starting at the specified offset since: 2.2.0 STRLEN key summary: Get the length of the value stored in a key since: 2.2.0
字符串操作
单个key操作
- set
- get
- append
- getset
- getrange
- setrange
- setnx
- setex
- psetex
- strlen
1、SET key value [expiration EX seconds|PX milliseconds] [NX|XX]
创建一个key-value,
EX 设置过期秒值
PX设置过期毫秒值
NX如果没有该key才创建
XX如果有该key才修改,不会自动创建
127.0.0.1:6379> set k1 1 OK 127.0.0.1:6379> set k1 11 nx (nil) 127.0.0.1:6379> get k1 "1" 127.0.0.1:6379> set k2 2 xx (nil) 127.0.0.1:6379> get k2 (nil) 127.0.0.1:6379> set k2 22 OK
2、GET key
获取该key的value
3、APPEND key value
追加字符,如果该key不存在会自动创建
127.0.0.1:6379> KEYS * (empty list or set) 127.0.0.1:6379> APPEND name tom (integer) 3 127.0.0.1:6379> GET name "tom" 127.0.0.1:6379> APPEND name " hello" (integer) 9 127.0.0.1:6379> GET name "tom hello"
4、GETSET key value
对该key设置新值并返回旧值
127.0.0.1:6379> KEYS * (empty list or set) 127.0.0.1:6379> GETSET k4 4 (nil) 127.0.0.1:6379> get k4 "4" 127.0.0.1:6379> GETSET k4 444 "4" 127.0.0.1:6379> get k4 "444"
5、GETRANGE key start end
截取value
注意end的字符也会被截取,和java截取字符串不太一样
127.0.0.1:6379> SET k1 abcdefg OK 127.0.0.1:6379> GETRANGE k1 3 -1 "defg" 127.0.0.1:6379> GETRANGE k1 3 5 "def"
6、SETRANGE key offset value
从指定的偏移位置重写value
注意不是“插入”而是“覆盖重写”
127.0.0.1:6379> set k1 123456789 OK 127.0.0.1:6379> SETRANGE k1 6 ab (integer) 9 127.0.0.1:6379> get k1 "123456ab9"
7、SETNX key value
当key不存在时才能设置成功,等同于set key value nx
127.0.0.1:6379> SETNX k1 1 (integer) 1 127.0.0.1:6379> get k1 "1" 127.0.0.1:6379> SETNX k1 11 (integer) 0 127.0.0.1:6379> get k1 "1"
8、SETEX key seconds value
设置一个会过期的key,单位是秒
127.0.0.1:6379> SETEX k1 10 11 OK 127.0.0.1:6379> TTL k1 (integer) 8 127.0.0.1:6379> get k1 "11" 127.0.0.1:6379> TTL k1 (integer) 3 127.0.0.1:6379> get k1 (nil) 127.0.0.1:6379> TTL k1 (integer) -2
9、PSETEX key milliseconds value
设置一个会过期的key,单位是毫秒
和SETEX唯一的区别就是单位
127.0.0.1:6379> PSETEX k1 10000 1 OK 127.0.0.1:6379> TTL k1 (integer) 7 127.0.0.1:6379> GET k1 "1" 127.0.0.1:6379> TTL k1 (integer) -2 127.0.0.1:6379> GET k1 (nil)
10、STRLEN key
获取key的长度
127.0.0.1:6379> set k1 aaaaa OK 127.0.0.1:6379> STRLEN k1 (integer) 5
批量操作
- mset
- mget
- msetnx
1、MSET key value [key value ...]
一次设置多个key-value
127.0.0.1:6379> MSET k1 1 k2 2 k3 3 OK 127.0.0.1:6379> KEYS * 1) "k3" 2) "k2" 3) "k1" 127.0.0.1:6379> get k1 "1"
2、MGET key [key ...]
一次获取多个key的value
127.0.0.1:6379> MSET k1 1 k2 2 k3 3 OK 127.0.0.1:6379> MGET k1 k2 k3 1) "1" 2) "2" 3) "3"
3、MSETNX key value [key value ...]
设置多个key-value,只要其中一个key已存在就全部不能创建成功
127.0.0.1:6379> MSETNX k1 1 k2 2 k3 3 (integer) 1 127.0.0.1:6379> MGET k1 k2 k3 1) "1" 2) "2" 3) "3" 127.0.0.1:6379> MSETNX k3 3 k4 4 (integer) 0 127.0.0.1:6379> KEYS * 1) "k3" 2) "k2" 3) "k1"
数值操作
- incr
- incrby
- decr
- decrby
- incrbyfloat
1、INCR key
增加1
2、INCRBY key increment
增加给定的整数值
3、DECR key
减1
4、DECRBY key decrement
减去给定的整数值
5、INCRBYFLOAT key increment
增加给定的浮点数,可以是负数
127.0.0.1:6379> set k1 10 OK 127.0.0.1:6379> get k1 "10" 127.0.0.1:6379> INCR k1 (integer) 11 127.0.0.1:6379> get k1 "11" 127.0.0.1:6379> INCRBY k1 3 (integer) 14 127.0.0.1:6379> get k1 "14" 127.0.0.1:6379> DECRBY k1 2 (integer) 12 127.0.0.1:6379> get k1 "12" 127.0.0.1:6379> INCRBYFLOAT k1 0.5 "12.5" 127.0.0.1:6379> get k1 "12.5" 127.0.0.1:6379> INCRBYFLOAT k1 -1.5 "11" 127.0.0.1:6379> get k1 "11"
位操作
- setbit
- getbit
- bitcount
- bitpos
- bitop
- bitfield
1、SETBIT key offset value
在某位上设置0或者1
2、GETBIT key offset
获取某一位上的值
3、BITCOUNT key [start end]
计算bit=1的个数,注意start、end指的是字节
4、BITPOS key bit [start] [end]
获取执行bit值的第一个索引,同样start、end指的也是字节
5、BITOP operation destkey key [key ...]
进行二进制运算
bitop and destkey key [key...]对一个或者多个key求逻辑并,并将结果保存到destkey
bitop or destkey key [key...]对一个或者多个key求逻辑或,并将结果保存到destkey
bitop xor destkey key [key...]对一个或者多个key求逻辑异或,并将结果保存到destkey
bitop not destkey key [key...]对给定key求逻辑菲,并将结果保存到destkey
6、BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]