Redis入门,看这一篇就够了( 四 )


列表类型的元素是有序且可以重复的 。
存储值左端存值语法:lpush key value [value …]
127.0.0.1:6379> lpush list lily sandy(integer) 2右端存值语法:rpush key value [value …]
127.0.0.1:6379> rpush list tom kitty(integer) 4索引存值语法:lset key index value
127.0.0.1:6379> lset list 3 utoOK弹出元素左端弹出语法:lpop key
127.0.0.1:6379> lpop list"sandy"右端弹出语法:rpop key
127.0.0.1:6379> rpop list"kitty"获取元素个数语法:llen key
127.0.0.1:6379> llen list(integer) 2获取列表元素两边获取语法:lrange key start stop
127.0.0.1:6379> lpush users tom kitty land pony jack maddy(integer) 6127.0.0.1:6379> lrange users 0 31) "maddy"2) "jack"3) "pony"4) "land"// 获取所有127.0.0.1:6379> lrange users 0 -11) "maddy"2) "jack"3) "pony"4) "land"5) "kitty"6) "tom"// 从右端索引127.0.0.1:6379> lrange users -3 -11) "land"2) "kitty"3) "tom"索引获取语法:lindex key index
127.0.0.1:6379> lindex list 2"ketty"// 从右端获取127.0.0.1:6379> lindex list -5"sady"删除元素根据值删除语法:lrem key count value
127.0.0.1:6379> lpush userids 111 222 111 222 222 333 222 222(integer) 8// count=0 删除所有127.0.0.1:6379> lrem userids 0 111(integer) 2// count > 0 从左端删除前count个127.0.0.1:6379> lrem userids 3 222(integer) 3// count < 0 从右端删除前count个127.0.0.1:6379> lrem userids -3 222(integer) 2范围删除语法:ltrim key start stop
// 只保留2-4之间的元素127.0.0.1:6379> ltrim list 2 4OK散列操作命令redis字符串类型键和值是字典结构形式 , 这里的散列类型其值也可以是字典结构 。
存放键值单个语法:hset key field value
127.0.0.1:6379> hset user name javastack(integer) 1多个语法:hmset key field value [field value …]
127.0.0.1:6379> hmset user name javastack age 20 address chinaOK不存在时语法:hsetnx key field value
127.0.0.1:6379> hsetnx user tall 180(integer) 0获取字段值单个语法:hget key field
127.0.0.1:6379> hget user age"20"多个语法:hmget key field [field …]
127.0.0.1:6379> hmget user name age address1) "javastack"2) "20"3) "china"获取所有键与值语法:hgetall key
127.0.0.1:6379> hgetall user1) "name"2) "javastack"3) "age"4) "20"5) "address"6) "china"获取所有字段语法:hkeys key
127.0.0.1:6379> hkeys user1) "name"2) "address"3) "tall"4) "age"获取所有值语法:hvals key
127.0.0.1:6379> hvals user1) "javastack"2) "china"3) "170"4) "20"判断字段是否存在语法:hexists key field
127.0.0.1:6379> hexists user address(integer) 1获取字段数量语法:hlen key
127.0.0.1:6379> hlen user(integer) 4递增/减语法:hincrby key field increment
127.0.0.1:6379> hincrby user tall -10(integer) 170删除字段语法:hdel key field [field …]
127.0.0.1:6379> hdel user age(integer) 1


推荐阅读