redis通用命令
select 3#切换数据库3
info#查看redis信息
info replication#查看主从复制状况
keys *#查看所有的key
type key1#查看key数据库类型
exists key1#key是否存在
del key1#删除一个key
rename key key1#重命名
flushdb/flushall#清空数据库
string类型
C:\Users\wsh> localhost:6379> set name jack#设置key_value OK localhost:6379> mset name jack age 23 addr china OK localhost:6379> append key val#追加 (integer) 3 localhost:6379> get name#获取key的值 "jack" localhost:6379> mget name age hobby
-
"jack"
-
"23"
-
(nil) localhost:6379> r.get("name")#python实现返回字符串
-
Invalid argument(s) localhost:6379> r.mget("name","age","hobby")#返回值列表 Invalid argument(s) localhost:6379> set name jack ex 10#设置key时间10s过期 OK localhost:6379> set name jack nx#不存在时,设置key (nil) localhost:6379> srrlen name (error) ERR unknown command
srrlen
, with args beginning with:name
, localhost:6379> strlen name#获取字符串长度 (integer) 0 localhost:6379> r.strlen("name")#python实现 Invalid argument(s) localhost:6379> r.set("name","jack")#获取字符串分片 Invalid argument(s) localhost:6379> r.getrange("name",0,1) Invalid argument(s) localhost:6379> set name beikehan#设置字符串的片段值 OK localhost:6379> setrange name 1 (error) ERR wrong number of arguments for 'setrange' command localhost:6379> setrange name 1 oo (integer) 8 localhost:6379> r.setrange("name",1,"oo") Invalid argument(s) localhost:6379> set age 23 OK localhost:6379> incr age#数据加1 (integer) 24 localhost:6379> desc age (error) ERR unknown commanddesc
, with args beginning with:age
, localhost:6379> decr age#数据减1 (integer) 23 localhost:6379> set age 10 OK localhost:6379> incrby age 5#数据加5 (integer) 15 localhost:6379> decrby age 5#数据减5 (integer) 10 localhost:6379> incrbyfloat age 1,3 (error) ERR value is not a valid float localhost:6379> incrbyfloat age 1.3 "11.30000000000000071" localhost:6379> incrbyfloat age -1.3#转为字符串 "10" localhost:6379> set name jack ex 10#十秒过期 OK localhost:6379> set name jack OK localhost:6379> expire name 10 (integer) 1 localhost:6379> pexpire name 10 (integer) 1 localhost:6379> localhost:6379> ttl name#查看多久过期 (integer) -2 localhost:6379> persist name#设置永不过期 (integer) 0 localhost:6379> set sms_code:1731111111 637905 EX 300#存储生成的短信验证码 OK localhost:6379> set sms_code_flag:1731111111 1 EX 60 OK localhost:6379> get sms_code:1731111111 "637905" localhost:6379> lpush numbers 1 2 3#头插法 (integer) 3 localhost:6379> rpush numbers 8 9 0#尾插法 (integer) 6 localhost:6379> lset key index val (error) WRONGTYPE Operation against a key holding the wrong kind of value localhost:6379> lset key index val (error) WRONGTYPE Operation against a key holding the wrong kind of value -
更新列表
-
localhost:6379> lset numbers 5 10 OK
-
删除列表
-
localhost:6379> lpop numbers "3" localhost:6379> rpop numbers "10" localhost:6379> lrem number 0 2 (integer) 0 localhost:6379> lrem numbers -2 1 (integer) 1 localhost:6379> lrem numbers 2 1 (integer) 0 localhost:6379> ltrim key start stop (error) ERR value is not an integer or out of range localhost:6379> ltrim numbers 2 5 OK
-
查询列表
-
localhost:6379> lrange numbers 0 -1
-
List类型练习
-
"9" localhost:6379> lrem history:137 0 1 (integer) 0 localhost:6379> lpush history:137 0 1 (integer) 2 localhost:6379> ltrim history:137 0 1 OK localhost:6379> lrange histroy:173 0 -1 (empty list or set)
-
Hash
-
插入哈希
-
localhost:6379> hset user:123 name jack (integer) 1 localhost:6379> hmset user:123 name xiaomi age 23 gender man OK localhost:6379> hincrby key field increment (error) ERR value is not an integer or out of range localhost:6379> hincrby user:123 age 5 (integer) 28
-
查询哈希
-
localhost:6379> hget user:123 name "xiaomi" localhost:6379> hgetall user:123
-
"name"
-
"xiaomi"
-
"age"
-
"28"
-
"gender"
-
删除哈希
-
localhost:6379> hdel user:123 gender (integer) 1
-
Hash类型练习
-
localhost:6379> hincrby cart_123 3 5 (integer) 5 localhost:6379> hincrby cart_123 3 -2 (integer) 3 localhost:6379> hset cart_123 3 5 (integer) 0 localhost:6379> hgetall cart_123
-
"3"
-
"5"
-
Set
-
插入集合
-
localhost:6379> hdel cart_123 3 (integer) 1 localhost:6379> sadd workers tom lucy jack (integer) 3
-
删除集合
-
localhost:6379> srem workers tom (integer) 1
-
查询集合
-
localhost:6379> scard worders (integer) 0 localhost:6379> smembers key (error) WRONGTYPE Operation against a key holding the wrong kind of value localhost:6379> smembers workers
-
"lucy"
-
"jack" localhost:6379> sismenmber workers tom (error) ERR unknown command
sismenmber
, with args beginning with:workers
,tom
, localhost:6379> sismember workers tom (integer) 0 -
Set类型的练习
-
localhost:6379> sadd cart_selected_123 3 (integer) 1 localhost:6379> srem cart_selected_123 3 (integer) 1 localhost:6379> smembers cart_selected_123 (empty list or set) localhost:6379> scord cart_selected_123 (error) ERR unknown command
scord
, with args beginning with:cart_selected_123
, -
插入有序集合
-
localhost:6379> zadd salary 10000 jim 8000 Tom 12000 jimi 10000 Hake (integer) 4
-
删除有序集合
-
localhost:6379> zrem salary Hake (integer) 1 localhost:6379> zremrangebyrank salary 1 2 (integer) 2 localhost:6379> zrange salary 0 -1
-
更新有序集合
-
"Tom" localhost:6379> zremrangebyscore salary 10 30 (integer) 0 localhost:6379> zrange salary 0 -1
-
查询有序集合
-
"Tom" localhost:6379> zcard salary (integer) 1 localhost:6379> zcount key min max (error) ERR min or max is not a float localhost:6379> zcount salary Tom (error) ERR wrong number of arguments for 'zcount' command localhost:6379> zcount salary (8000 10000) (error) ERR min or max is not a float localhost:6379> zrank salary Tom (integer) 0 localhost:6379> zrevrank salary Tom (integer) 0 localhost:6379> zscore salary Tom "8000" localhost:6379> zrange salary 0 -1
-
"Tom" localhost:6379> zrange salary 0 -1 withscores
-
"Tom"
-
"8000" localhost:6379> zrevrange key start stop withscores (error) ERR value is not an integer or out of range localhost:6379> zrevrange salary 0 -1
-
"Tom" localhost:6379> zrevrange salary 0 -1 withscores
-
"Tom"
-
"8000" localhost:6379> zrangebyscore salary 8000 20000 limit 2 1 withscores (empty list or set) localhost:6379> zrevrangebyscore key max min withscores (error) ERR min or max is not a float localhost:6379> zrevrangebyscore salary 20000 8000
-
"Tom" localhost:6379> zremvrangebyscore salary 20000 8000 limit 2 1 withscores (error) ERR unknown command
zremvrangebyscore
, with args beginning with:salary
,20000
,8000
,limit
,2
,1
,withscores
, localhost:6379> zrevrangebyscore salary 20000 8000 limit 2 1 withscores (empty list or set) -
Zset练习
-
localhost:6379> zadd score_sheet 30 xue_1 40 xue_2 100 xue_3 87 xue_4 (integer) 4 localhost:6379> zrangebyscore score_sheet 0 60 withscores
-
"xue_1"
-
"30"
-
"xue_2"
-
"40" localhost:6379> zcount score_sheet 60 100 (integer) 2 localhost:6379> zrevrank score_sheet xue_1 (integer) 3 localhost:6379> zrevrange score_sheet 0 -1 withscores
-
"xue_3"
-
"100"
-
"xue_4"
-
"87"
-
"xue_2"
-
"40"
-
"xue_1"
-
"30" localhost:6379>