redis常用操做
string key操作
string <key:value>
set name john
get name
list
setnx <key value>
setnx gender male(分布式锁)
get gender
setnx goods_1 111
del goods_1
get goods_1
查找键
keys * //查找所有
keys *_1 //查找以“_1”结尾的
keys *a* //查找包含a的值
存在键名
exist <keys>
类型:
type <keys>
删除键:
del <keys>
清空所有
flashall
setex x 100 ruby
查看键有效期
ttl <keys> // -1永不过期,-2已经过期
设置过期时间
expire name 5
重命名
rename keys
redis默认有0-15个库一共16个, 默认是在0号数据库,库与库是隔离的;
select <数据库ID> //切换到1号库
l
list数组操作
队列、列表的子成员必须是string
lpush key value
rpush key value
linsert key after|brfore 指定元素 value
lindex key index
lrange key start stop
lset key index value
lrem key count value
添加子成员
//在左侧(前)添加一条或多条数据
lpush <key value1 value2...>
//在右侧(后)添加一条或多条数据
rpush <key value1 value2...>
1 127.0.0.1:6379> rpush names zhaosi 2 (integer) 4 3 127.0.0.1:6379> lrange names 0 -1 4 1) "wangwu" 5 2) "lisi" 6 3) "zhangsan" 7 4) "zhaosi" 8 127.0.0.1:6379> rpush name a b c 9 (integer) 3 10 127.0.0.1:6379> rpush names a b c 11 (integer) 7 12 127.0.0.1:6379> lrange names 0 -1 13 1) "wangwu" 14 2) "lisi" 15 3) "zhangsan" 16 4) "zhaosi" 17 5) "a" 18 6) "b" 19 7) "c"
//在指定元素的左侧(前)右侧(后)添加一条或多条数据
linsert key before 指定元素 value1 value2...
linsert key after 指定元素 value1 value2...
eg:
1 127.0.0.1:6379> linsert names before "zhangsan" yuan 2 (integer) 8 3 127.0.0.1:6379> lrange names 0 -1 4 1) "wangwu" 5 2) "lisi" 6 3) "yuan" 7 4) "zhangsan" 8 5) "zhaosi" 9 6) "a" 10 7) "b" 11 8) "c"View Code
1 127.0.0.1:6379> rinsert names after "b" haha 2 (error) ERR unknown command 'rinsert' 3 127.0.0.1:6379> linsert names after "b" haha 4 (integer) 9 5 127.0.0.1:6379> rinsert names after "b" haha 6 (error) ERR unknown command 'rinsert' 7 127.0.0.1:6379> lrange names 0 -1 8 1) "wangwu" 9 2) "lisi" 10 3) "yuan" 11 4) "zhangsan" 12 5) "zhaosi" 13 6) "a" 14 7) "b" 15 8) "haha" 16 9) "c"View Code
获取列表成员:
根据指定的索引获取成员的值,负数下标从右边-1开始,逐个递减
lindex key index
eg:获取brother下表为2以及-2的成员:
1 127.0.0.1:6379> del brother 2 (integer) 0 3 127.0.0.1:6379> lpush brother guanyu zhangfei liubei 4 (integer) 3 5 127.0.0.1:6379> lrange brother 0 -1 6 1) "liubei" 7 2) "zhangfei" 8 3) "guanyu" 9 127.0.0.1:6379> lindex 0 10 (error) ERR wrong number of arguments for 'lindex' command 11 127.0.0.1:6379> lindex brother 0 12 "liubei" 13 127.0.0.1:6379>View Code
1 127.0.0.1:6379> lrange brother 0 -1 2 1) "liubei" 3 2) "zhangfei" 4 3) "guanyu" 5 127.0.0.1:6379> lindex 0 6 (error) ERR wrong number of arguments for 'lindex' command 7 127.0.0.1:6379> lindex brother 0 8 "liubei" 9 127.0.0.1:6379> lindex -1 10 (error) ERR wrong number of arguments for 'lindex' command 11 127.0.0.1:6379> lindex beother -1 12 (nil) 13 127.0.0.1:6379> lindex brother -2 14 "zhangfei" 15 127.0.0.1:6379> lindex brother 2 16 "guanyu" 17 127.0.0.1:6379>View Code
获取列表切片
闭区间,包括stop
lrange key start stop
eg:获取brother下表为0-2的成员
1 127.0.0.1:6379> lrange brother 0 2 2 1) "liubei" 3 2) "zhangfei" 4 3) "guanyu" 5 127.0.0.1:6379>
eg:获取最后两位
1 127.0.0.1:6379> lrange brother -2 -1 2 1) "zhangfei" 3 2) "guanyu" 4 127.0.0.1:6379>
获取列表长度
llen key
1 127.0.0.1:6379> llen names 2 (integer) 13 3 127.0.0.1:6379> llen brother 4 (integer) 3 5 127.0.0.1:6379>
设置指定索引值
lset key index value
1 127.0.0.1:6379> lrange names 0 -1 2 1) "y" 3 2) "x" 4 3) "c" 5 4) "after" 6 5) "wangwu" 7 6) "lisi" 8 7) "yuan" 9 8) "zhangsan" 10 9) "zhaosi" 11 10) "a" 12 11) "b" 13 12) "haha" 14 13) "c" 15 127.0.0.1:6379> lset names 3 "before" 16 OK 17 127.0.0.1:6379> lrange names 0 -1 18 1) "y" 19 2) "x" 20 3) "c" 21 4) "before" 22 5) "wangwu" 23 6) "lisi" 24 7) "yuan" 25 8) "zhangsan" 26 9) "zhaosi" 27 10) "a" 28 11) "b" 29 12) "haha" 30 13) "c" 31 127.0.0.1:6379>View Code
删除指定成员
lpop key //第一个成员出列
rpop key //最后一个成员出列
1 127.0.0.1:6379> lrange name 0 -1 2 1) "x" 3 2) "b" 4 3) "after" 5 4) "a" 6 5) "b" 7 6) "c" 8 127.0.0.1:6379> lpop name 9 "x" 10 127.0.0.1:6379> lrange name 0 -1 11 1) "b" 12 2) "after" 13 3) "a" 14 4) "b" 15 5) "c" 16 127.0.0.1:6379>
1 127.0.0.1:6379> lrange name 0 -1 2 1) "b" 3 2) "after" 4 3) "a" 5 4) "b" 6 5) "c" 7 127.0.0.1:6379> rpop name 8 "c" 9 127.0.0.1:6379> lrange name 0 -1 10 1) "b" 11 2) "after" 12 3) "a" 13 4) "b" 14 127.0.0.1:6379>
开发中经常使用rpush和lpop实现队列的数据结构,实现入列和出列
lrem key count value
//count表示删除的数据,value表示要删除的成员,该命令默认表示将列表从左侧前的count个value元素移除
//count==0,表示删除列表所有值为value的成员;
//count > 0,表示删除列表左侧开始的前count个value成员;
//count <0, 表示删除列表右侧开始的前count个value成员
1 127.0.0.1:6379> lrange brother 0 -1 2 1) "A" 3 2) "B" 4 3) "C" 5 4) "A" 6 5) "B" 7 127.0.0.1:6379> lrem brother 0 A 8 (integer) 2 9 127.0.0.1:6379> lrange brother 0 -1 10 1) "B" 11 2) "C" 12 3) "B"View Code
标签:127.0,string,0.1,list,redis,brother,6379,lrange,key From: https://www.cnblogs.com/wenzm/p/16987816.html