码迷,mamicode.com
首页 > 其他好文 > 详细

redis五种基本类型

时间:2021-01-18 11:21:29      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:阻塞   end   苹果   sorted   队列   reg   members   rgba   最大   

String类型

set k1 aaa
append k1 "bbb"
getrange k1 0 -1
setrange k1 5(不能负数)  ccc  替换,不够就补

object help
object encoding k1 字符串是 embstr、raw、int

incr k2
incrbyfloat k3 0.5
decr k2 
msetnx k1 a k2 b nx没值才能设置成功 如果一个失败全部失败 原子性
mget k1 k2
strlen k1
setex k2 10 aa


man ascii查看ascii码表

  

setbit

127.0.0.1:6379> setbit b 1 1
(integer) 0
127.0.0.1:6379> get b
"@"
127.0.0.1:6379> setbit b 7 1
(integer) 0
127.0.0.1:6379> get b
"A"
127.0.0.1:6379> help setbit

  SETBIT key offset value
  summary: Sets or clears the bit at offset in the string value stored at key
  since: 2.2.0
  group: string

 

判断b是否在365天内登陆过

127.0.0.1:6379> setbit b 1 1
(integer) 0
127.0.0.1:6379> get b
"@"
127.0.0.1:6379> setbit b 7 1
(integer) 0
127.0.0.1:6379> get b
"A"
127.0.0.1:6379> setbit b 364 1
(integer) 0
127.0.0.1:6379> strlen b
(integer) 46
127.0.0.1:6379> get b
"A\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\b"
127.0.0.1:6379> bitcount b -2 -1
(integer) 1

 

二进制and、or

127.0.0.1:6379> setbit aa 1 1
(integer) 0
127.0.0.1:6379> setbit aa 7 1
(integer) 0
127.0.0.1:6379> get aa
"A"
127.0.0.1:6379> setbit bb 1 1 
(integer) 0
127.0.0.1:6379> setbit bb 6 1
(integer) 0
127.0.0.1:6379> get bb
"B"
127.0.0.1:6379> bitop and andkey aa bb
(integer) 1
127.0.0.1:6379> get andkey
"@"
127.0.0.1:6379> bitop or orkey aa bb
(integer) 1
127.0.0.1:6379> get orkey
"C"

 

统计活跃用户

                          日期  用户id  记录一次
127.0.0.1:6379> setbit 20190101 1  1
(integer) 0
127.0.0.1:6379> setbit 20190102 1  1
(integer) 0
127.0.0.1:6379> setbit 20190103 1  1
(integer) 0
127.0.0.1:6379> setbit 20190103 2  1
(integer) 0
127.0.0.1:6379> bitop or newkey 20190101 20190102 20190103
(integer) 1
127.0.0.1:6379> bitcount newkey 0 -1
(integer) 2
去重(值只能为0或1,为0不统计,为1只统计1次)

 

 

List类型

help @list

127.0.0.1:6379> lpush list1  a b c d e f
(integer) 6
127.0.0.1:6379> lpop list1
"f"
127.0.0.1:6379> rpop list1
"a"
1.同向命令先进后出、类似栈。
2.反向命令先进先出、类似队列。

127.0.0.1:6379> lrange list1 0 -1
1) "c"
2) "1"
3) "2"
4) "3"
5) "4"
取出下标为2的元素
127.0.0.1:6379> lindex list1 2
"2"
取出最后一个元素
127.0.0.1:6379> lindex list1 -1
"4"
类似数组

 

List移除

127.0.0.1:6379> lpush k1 a 1 a 2 a 3
(integer) 6
127.0.0.1:6379> lrange k1 0 -1
1) "3"
2) "a"
3) "2"
4) "a"
5) "1"
6) "a"
127.0.0.1:6379> lrem k1 2 a
(integer) 2
127.0.0.1:6379> lrange k1 0 -1
1) "3"
2) "2"
3) "1"
4) "a"
插入
127.0.0.1:6379> linsert k1 before 1 a
(integer) 5
127.0.0.1:6379> lrange k1 0 -1
1) "3"
2) "2"
3) "a"
4) "1"
5) "a"
127.0.0.1:6379> linsert k1 after 1 b
(integer) 6
127.0.0.1:6379> lrange k1 0 -1
1) "3"
2) "2"
3) "a"
4) "1"
5) "b"
6) "a"

 

List阻塞

阻塞队列、单播队列
blpop one 0 先阻塞先拿到

ltrim 删除两端的数据
ltrim key  0  -1 一个不删

 

 

 

Hash类型

当单个字符不使用hash时存储方式
set sean::age  18
set sean::hobby  gril
keys sean*

hset sean age 18
hmset sean age 20 address games
hget sean age
hmget sean age address
hkeys sean
hvals sean
hgetall sean
hincrbyfloat xiaoming age 18.5
hget xiaoming age
hincrbyfloat xiaoming age -1

 

 

 

 

Set类型

127.0.0.1:6379> sadd k1 xiaoke xiaowang xiaoming sean tom xiaoke
(integer) 5
127.0.0.1:6379> smembers k1
1) "sean"
2) "xiaoming"
3) "xiaowang"
4) "tom"
5) "xiaoke"
去重xiaoke

删除
127.0.0.1:6379> srem k1 xiaoke xiaowang
(integer) 2
127.0.0.1:6379> smembers k1
1) "tom"
2) "xiaoming"
3) "sean"

 

set集合操作

127.0.0.1:6379> sadd key1  1 2 3 4
(integer) 4
127.0.0.1:6379> sadd key2  3 4 5 6
(integer) 4
取交集
127.0.0.1:6379> sinter key1 key2
1) "3"
2) "4"
取并集
127.0.0.1:6379> sunion key1 key2
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
取差集
127.0.0.1:6379> sdiff key1 key2
1) "1"
2) "2"
127.0.0.1:6379> sdiff key2 key1
1) "5"
2) "6"

 

随机事件

srandmember key  count
整数: 取出一个去重的结果集(不能超过已有集合)
负数:取出一个带重复的结果集,一定满足你需要的数量
0: 不返回
案例:抽奖

127.0.0.1:6379> sadd name tom xiaoke xiaowang sean jerry
(integer) 5
127.0.0.1:6379> srandmember name 4
1) "xiaowang"
2) "tom"
3) "xiaoke"
4) "jerry"
127.0.0.1:6379> srandmember name -4
1) "xiaowang"
2) "tom"
3) "tom"
4) "tom"
127.0.0.1:6379> srandmember name 10
1) "tom"
2) "xiaoke"
3) "sean"
4) "xiaowang"
5) "jerry"
127.0.0.1:6379> srandmember name -10
 1) "tom"
 2) "sean"
 3) "xiaoke"
 4) "sean"
 5) "tom"
 6) "jerry"
 7) "xiaowang"
 8) "sean"
 9) "jerry"
10) "jerry"

从奖品堆中删除
spop key count
127.0.0.1:6379> smembers name
1) "tom"
2) "xiaowang"
3) "sean"
4) "xiaoke"
127.0.0.1:6379> spop name 2
1) "xiaowang"
2) "tom"
127.0.0.1:6379> smembers name
1) "sean"
2) "xiaoke"

 

 

Sorted_set有序集合

127.0.0.1:6379> zadd k1 8 apple 2 banana 3 orange
(integer) 3
查看所有水果
127.0.0.1:6379> zrange k1 0 -1
1) "banana"
2) "orange"
3) "apple"

查看所有水果带上价格
127.0.0.1:6379> zrange k1 0 -1 withscores
1) "banana"
2) "2"
3) "orange"
4) "3"
5) "apple"
6) "8"

查看所有水果价格在3到8之间
127.0.0.1:6379> zrangebyscore k1 3 8
1) "orange"
2) "apple"

价格由低到高前两名水果
127.0.0.1:6379> zrange k1 0 1
1) "banana"
2) "orange"

价格由高到低前两名水果
127.0.0.1:6379> zrevrange k1 0 1
1) "apple"
2) "orange"
127.0.0.1:6379> zrange k1 -2 -1
1) "orange"
2) "apple"

取出苹果的分数
127.0.0.1:6379> zscore k1 apple
"8"

取出苹果的排名
127.0.0.1:6379> zrank k1 apple
(integer) 2

集合操作

127.0.0.1:6379> zadd k1 80 tom 60 sean 70 baby
(integer) 3
127.0.0.1:6379> zadd k2 60 tom 100 sean 40 jack
(integer) 3

取交集(相加)
127.0.0.1:6379> zunionstore unkey 2 k1 k2
(integer) 4
127.0.0.1:6379> zrange unkey 0 -1 withscores
1) "jack"
2) "40"
3) "baby"
4) "70"
5) "tom"
6) "140"
7) "sean"
8) "160"

取交集权重
127.0.0.1:6379> zunionstore unkey1 2 k1 k2 weights 1 0.5
(integer) 4
127.0.0.1:6379> zrange unkey1 0 -1 withscores
1) "jack"
2) "20"
3) "baby"
4) "70"
5) "sean"
6) "110"
7) "tom"
8) "110"

取交集最大
127.0.0.1:6379> zunionstore unkey2 2 k1 k2 aggregate max
(integer) 4
127.0.0.1:6379> zrange unkey2 0 -1 withscores
1) "jack"
2) "40"
3) "baby"
4) "70"
5) "tom"
6) "80"
7) "sean"
8) "100"

排序是怎么实现的,增删改查的速度?
跳跃表,平均值(增删改查)最优

 

redis五种基本类型

标签:阻塞   end   苹果   sorted   队列   reg   members   rgba   最大   

原文地址:https://www.cnblogs.com/bigdata-familyMeals/p/14288649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!