Redis Getrange 命令用于获取存储在指定 key 中字符串的子字符串。字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)。Redis 字符串(string)
语法
redis 127.0.0.1:6379> GETRANGE KEY_NAME start end
可用版本: >= 2.4.0
返回值: 截取得到的子字符串。
示例
redis 127.0.0.1:6379> SET mykey "This is my test key"
OK
redis 127.0.0.1:6379> GETRANGE mykey 03"This"
redis 127.0.0.1:6379> GETRANGE mykey 0-1"This is my test key"
redis 127.0.0.1:6379> SETEX KEY_NAME TIMEOUT VALUE
可用版本: >= 2.0.0
返回值: 设置成功时返回 OK 。
示例
redis 127.0.0.1:6379> SETEX mykey 60 redis
OK
redis 127.0.0.1:6379> TTL mykey
60
redis 127.0.0.1:6379> GET mykey
"redis
5.Redis SET 命令 - 设置指定 key 的值
简介
Redis SET 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。Redis 字符串(string)
语法
redis 127.0.0.1:6379> SET KEY_NAME VALUE
可用版本: >= 1.0.0
返回值: 在 Redis 2.6.12 以前版本, SET 命令总是返回 OK 。
示例
# 对不存在的键进行设置
redis 127.0.0.1:6379> SET key "value"
OK
redis 127.0.0.1:6379> GET key
"value"# 对已存在的键进行设置
redis 127.0.0.1:6379> SET key "new-value"
OK
redis 127.0.0.1:6379> GET key
"new-value"
# 对不存在的 key 或字符串类型 key 进行 GET
redis> GET db
(nil)
redis> SET db redis
OK
redis> GET db
"redis"# 对不是字符串类型的 key 进行 GET
redis> DEL db
(integer)1
redis> LPUSH db redis mongodb mysql
(integer)3
redis> GET db
(error) ERR Operation against a key holding the wrong kind of value
# 对存在的数字值 key 进行 DECR
redis> SET failure_times 10
OK
redis> DECR failure_times
(integer)9# 对不存在的 key 值进行 DECR
redis> EXISTS count
(integer)0
redis> DECR count
(integer)-1# 对存在但不是数值的 key 进行 DECR
redis> SET company YOUR_CODE_SUCKS.LLC
OK
redis> DECR company
(error) ERR value is not an integer or out of range
# key 存在且是数字值
redis> SET rank 50
OK
redis> INCRBY rank 20(integer)70
redis> GET rank
"70"# key 不存在时
redis> EXISTS counter
(integer)0
redis> INCRBY counter 30(integer)30
redis> GET counter
"30"# key 不是数字值时
redis> SET book "long long ago..."
OK
redis> INCRBY book 200(error) ERR value is not an integer or out of range
redis 127.0.0.1:6379> GETSET mynewkey "This is my test key"(nil)
redis 127.0.0.1:6379> GETSET mynewkey "This is my new value to test getset""This is my test key"
redis 127.0.0.1:6379> SET key1 "hello"
OK
redis 127.0.0.1:6379> SET key2 "world"
OK
redis 127.0.0.1:6379> MGET key1 key2 someOtherKey
1)"Hello"2)"World"3)(nil)