
Redis作为缓存具有极其丰富的功能,比如计数器、好友关系以及排行榜等等。而Redis之所能够实现如此强大的功能,完全是依赖着它的基本数据结构。今天我们来看看Redis的五种基本的数据结构,分别是字符串(strings), 散列(hashes), 列表(lists), 集合(sets)以及有序集合(zset)。
1、字符串(strings)
绝大部分编程语言中都少不了的基本数据类型就是字符串(strings),数据库Redis也不例外。下面我们来看一下关于字符串的基本操作:
set key value 设置值
get key 获取某个key的值
mset key1 value1 key2 value2 批量设置并且是原子的,可以用来减少网络时间消耗
mget key1 key2 批量获取并且是原子的,可以用来减少网络时间消耗
incr key 自增指定key的值
decr key 自减指定key的值
incrby key value 自增指定数值
decrby key value 自减指定数值
incrbyfloat key floatvalue 增加指定浮点数,前面几个操作就可以用来实现计数器的功能。
setnx key value 如果不存在该key则可以设置成功,否则会失败,加上过期时间限制,则是redis实现分布式锁的一种方式(后面会提到)。
set key value xx 与前面相反,如果存在则设置成功,否则失败(相当于更新操作)
2、散列(hashes)
我们可以把hash理解为小型Redis,Redis在底层实现上和Java中的HashMap差不多,都是使用数组+链表的二维结构实现的。不一样的是,在Redis中字典的值只能是字符串,而且他们rehash的方式也不同,在Redis中使用的是渐进式rehash。
hset key field value 设置字典中某个key的值
hsetnx key field value 设置字典中某个key的值(不存在的)
hmset key field1 value1 field2 value2 … 批量设置
hget key field 获取字典中某个key的值
hmget key field1 field2 批量获取
hgetall key 获取全部
hdel key field 删除某个key
hexists key field 判断是否存在
hlen key 获取指定key对应的字典中的存储个数
hvals key 返回所有的value
hkeys key 返回所有的key
hincrby key field increValue 增加某个value的值(也可以增加负数)
hincrbyfloat key field floatValue 增加某个value的值(浮点数)
3、列表(lists)
Redis中的列表相当于Java中的LinkedList,也就是底层是通过链表来实现的,所以对于list来说插入删除操作很快,但索引定位非常慢。Redis提供了许多对于list的操作,如出入等操作,你可以充分利用它们来实现一个栈或者队列。
lpush key item1 item2 item3… 从左入栈
rpush key item1 item2 item3… 从右入栈
lpop key 从左出栈
rpop key 从右出栈
lindex key index 获取指定索引的元素 O(n)谨慎使用
lrange key start end 获取指定范围的元素 O(n)谨慎使用
linsert key before|after item newitem 在指定元素的前面或者后面添加新元素
lrem key count value 删除指定个数值为value的元素
count = 0 :删除所有值为value的元素
count > 0 :从左到右删除 count 个值为 value 的元素
count < 0 :从右到做删除 |count| 个值为 value 的元素
ltrim key start end 保留指定范围的元素
lset key index newValue 更新某个索引的值
blpop key timeout 没有则阻塞(timeout指定阻塞时间 为0代表永久)
brpop key timeout 没有则阻塞(timeout指定阻塞时间 为0代表永久) 这两个可以用来实现消费者生产者
4、集合(sets)
Redis中的set相当于Java中的HashSet(无序集合),其中里面的元素不可以重复,我们可以利用它实现一些去重的功能。我们还有对几个集合进行取交集,取并集等操作,这些操作就可以获取不同用户之间的共同好友、共同爱好等等。
sadd key value 添加元素
sdel key value 删除某个元素
sismember key value 判断是否是集合中的元素
srandmember key count 随机获取指定个数的元素(不会影响集合结构)
spop key count 从集合中随机弹出元素(会破坏结合结构)
smembers key 获取集合所有元素 O(n)复杂度
scard key 获取集合个数
sinter set1 set2 … 获取所有集合中的交集
sdiff set1 set2 … 获取所有集合中的差集
sunion set1 set2 … 获取所有集合中的并集
5、有序集合(zset)
Redis 中的 zset 是一个有序集合,通过它可以实现很多有意思的功能,比如学生成绩排行榜,视频播放量排行榜等等。zset 中是使用 跳表 来实现的,我们知道只有数组这种连续的空间才能使用二分查找进行快速的定位,而链表是不可以的。跳表帮助链表查找的时候节省了很多时间(使用跳的方式来遍历索引来进行有序插入)。
郑州不孕不育医院哪家技术好?男性输精管堵塞会导致不育吗:https://news.fh21.com.cn/yldt/zztlyy/16711032.html
开封不孕不育医院哪家口碑好?做输卵管造影检查疼吗:https://news.fh21.com.cn/yldt/zztlyy/16711033.html
患上卵巢性不孕的原因是什么?洛阳不孕不育医院哪家好:https://news.fh21.com.cn/yldt/zztlyy/16711035.html
平顶山不孕不育哪个医院好?死精症有什么预防的措施:https://news.fh21.com.cn/yldt/zztlyy/16711036.html
cheap viagra mastercard buy viagra online legally cheap viagra online without prescription
cephalexin 500 mg used for cephalexin cefalin cephalexin std
keflex 500 mg precio are keflex and cefazolin in the same family keflex bandage
amoxicillin 400mg/5ml dosage for child ear infection what is similar to amoxicillin over the counter amoxicillin dosage for adults sinus infection
does amoxicillin treat trichomoniasis amoxicillin after root canal ic amoxicillin-clav
side effect of augmentin medicine augmentin et glucose augmentin 625 mg for what
augmentin bustine quanto costa mono and augmentin rash how long for augmentin antibiotics to work
amoxicillin for balanitis how much is amoxicillin at walgreens 1000 mg amoxicillin
side effects of prednisone in cats what’s prednisone can you drink alcohol while on prednisone
over the counter ciprofloxacin can i use ciprofloxacin for throat infection ciprofloxacin for tooth infection dosage
ciprofloxacin oral suspension ciprofloxacin and prednisone ciprofloxacin and sunshine
can i take benzonatate with azithromycin azithromycin 250 mg dosage side effects of azithromycin
prices on cialis when will generic cialis be available cialis black 800mg
cheap viagra online india how do you get viagra cheap viagra fast shipping
sildenafil 5 mg where to buy generic viagra in usa can i buy viagra from canada
cialis vi cialis 10mg reviews cheap tadalafil no prescription
cialis coupon rite aid tadalafil and dapoxetine in india cialis no prescription arizona
sildenafil for sale canada viagra over the counter us online viagra order canada
sildenafil price in usa viagra 100mg tablet price in india female viagra online uk
essay writers toronto writing essay services custom essay station
medical school essay help buy essay cheap how to make a hook for an essay
essay form transition words for essay personal essay for college
how to write an essay example essay about military service essay writer service
how to write a compare and contrast essay topics argumentative essay mba application essay writing service
$2,000 niche no essay scholarship essay about community service cheap essays online
rx warehouse pharmacy kroger pharmacy crestor bupropion xl pharmacy
neoral pharmacy walgreen pharmacy hours by store Alesse
australia online pharmacy viagra online pharmacy store in delhi cost of viagra in pharmacy
pharmacy that will fill oxycodone cellcept online pharmacy sildenafil online pharmacy
online pharmacy spironolactone Zantac inhouse pharmacy propecia
why tadalafil cause headaches tadalafil research chemical lowest prices on tadalafil
tadalafil doses available max dose of tadalafil what is tadalafil 10mg
swiss chems tadalafil tadalafil bph dose shelf life of tadalafil
mylan-tadalafil side effects tadalafil (cialis) price daily tadalafil dose
tadalafil how long before does tadalafil reduce blood pressure tadalafil as pre workout
offshore pharmacy tadalafil daily tadalafil therapy: a new treatment option for peyronie’s disease does tadalafil work on females
reddit tadalafil dr reddy tadalafil reviews tadalafil tablets for sale
tadalafil cvs price tadalafil 10 tadalafil customer reviews
tadalafil after prostatectomy how to make tadalafil work better sildenafil & tadalafil