您的位置:首页 > 数据库 > Redis

Redis基本操作

2014-11-19 19:43 197 查看
set foo bar
get foo // then return "bar"

set story "long long long ago"
get story

// also we can get the encoding of the object
object encoding foo // return "embstr"
object encoding story // return "raw"

// append
append foo " add" // append something more

// increbyfloat
set f 0.4
incrbyfloat f 0.5
get f // return "0.9" NOTE:Redis store long double as "embstr"

// strlen, to get the length of a str
strlen story


// About list
rpush numbers 1 "three" 5 // rpush, ie. right push, key is "numbers", value is [1, "three", 5]
llen numbers // return 3
lindex numbers 0 // return 1
object encoding numbers // return "ziplist"
// 当且仅当列表元素小于512个,所有字符串元素的长度都小于64字节时,才使用ziplist, 否则使用linkedlist


hset profile name "tom"
hset profile profile age 25
hset profile career "programmer"
// 会创建出一个键为profile,值为一个ziplist,或是hashtable对象。该对象中存储了{"name": "tom", "age": 25, "career": "programmer"}
// 当所有键和值的长度都小于64字节,且键值对数量小于512个时,就会使用ziplist,否则使用hashtable进行编码
hset // hset profile name "tom"
hget profile name // return “tom"
hexists profile name // return 1
hexists profile sex // return 0
hdel profile name // delete the key-value
hlen profile // return the number of key-value pairs
hgetall // return all the key-value pairs


sadd numbers 1 2 3 // 创建了intset对象,含有1,2,3三个元素
sadd fruits "apple" "orange" // 创建了fruits集合,含有"apple", "orange",该集合对象为hashtable
// 当集合对象保存的所有元素都是整数值,且元素数量不超过512个时,会使用 intset 进行编码,否则使用 hashtable 编码
sadd numbers 1 2 3
scard numbers // return 3
sismember numbers 3 // if find the target, return 1, else return 0
smembers numbers // return all the elements in this set
srandmember numbers // randomly return one element
spop numbers // randomly return one element and remove it from the set
srem numbers 3 // remove the target element from the set


关于下面的有一点要注意,就是当key比较长的时候,blah的编码会从ziplist -> skiplist

// about 有序集合 (ziplist & skiplist)
zadd blah 1.0 www 2.0 apple // add two k-v pairs
// key is "www" with the score 1.0, "apple" with the score 2.0

zcard blah // return the total pairs in blah
zcount blah 1.0 1.5 // return the number of keys whose score is between [1.0, 1.5]
zrange blah 0 1 // return the keys whose index is between [0, 1], thus return the first two keys.
zrevrange blah 0 1 // similar to zrange, but recursively print the keys
zrem blah apple // remove the node with key "apple"
zscore blah 1.0 // return the key whose score is 1.0


针对Redis数据库的操作:

select 0 // 选择0号数据库,共有 0 ~ 15 号数据库
flushdb // 清空当前数据库
randomkey // 返回任意一个键
dbsize // 返回当前数据库中键的数量
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: