当前位置: 首页 > news >正文

Redis缓存数据库(一)

目录

一、概述

1、Redis

 2、Redis的安装

Redis Windows环境设置

3、String: 字符串

3.1、字符串

3.2、数值

3.3、bitmap

4、Hash: 散列

5、List: 列表

6、Set: 集合

7、Sorted Set: 有序集合


一、概述

常识:

磁盘:1.寻址:ms(毫秒)2.带宽:MB/s
内存:1.寻址:ns    (纳秒) 2.带宽:GB/s
秒-->毫秒-->微妙-->纳秒 
磁盘比内存在寻址上慢了10W倍

内存带宽

前端总线频率/工作频率最高带宽

Pentium4

400MHz

3.2GB/s

Pentium4

533MHz

4.2GB/s

Pentium4

800MHz

6.4GB/s

DDR266

266MHz

2.1GB/s

双通道DDR266

266MHz

4.2GB/s

DDR333

333MHz

2.7GB/s

双通道DDR333

333MHz

5.4GB/s

DDR400

400MHz

3.2GB/s

双通道DDR400

400MHz

6.4GB/s

磁盘 内存 常识

基础知识:
关系型数据库建表:必须给出schema,类型:字节宽度,存储:倾向于行级存储
面试题:数据库:表很大,性能下降?
1、增删改变慢
2、查询速度呢?
    1个或少量查询依然很快
    2.并发大的时候会受硬盘带宽影响速度

SAP HANA:内存级别的关系型数据库 2T容量  贵:定制服务机器

数据在磁盘和内存体积不一样的

折中方案
memcached
redis
...
2个基础设施
1.冯诺依曼体系的硬件
2.以太网,tcp/ip的网络

DB-Engines 数据库引擎

1、Redis

NoSQL(NoSQL = Not Only SQL),意即"不仅仅是SQL",泛指非关系型的数据库。随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站已经显得力不从心,暴漏了很多难以克服的问题,而非关系型的数据库则由于其本身的特点得到了非常迅速的发展。NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,尤其是大数据应用难题,包括超大规模数据的存储。(例如谷歌每天为它们的用户收集万亿比特的数据)。这些类型的数据存储不需要固定的模式,无需多余操作就可以横向扩展。

Redis官网

Redis中文网站

Redis ( Remote DIctionary Server ) 通常被称为数据结构服务器,因为值 ( value ) 可以是字符串 ( String ) , 哈希 ( Map ) , 列表 ( list ) , 集合 ( Sets ) 或有序集合 ( Sorted Sets ) 等类型。

redis 是二进制安全的

 2、Redis的安装

Docker中安装redis  Spring Boot入门+深入(六)-Docker

Redis默认有16个库,库的进入

127.0.0.1:6379> set k1 hello
OK
127.0.0.1:6379> get k1
"hello"
127.0.0.1:6379> select 8
OK
127.0.0.1:6379[8]> get k1
(nil)
127.0.0.1:6379[8]>
==============================
直接redis-cli -n 8进入指定8库
#因为我环境在docker中前面添加了docker exec -it 16d9d3a548a1:其他环境不用添加
[root@localhost ~]# docker exec -it 16d9d3a548a1 redis-cli -n 8
SELECT 8 failed: NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379[8]>

帮助的使用

127.0.0.1:6379> help
redis-cli 7.0.11
To get help about Redis commands type:"help @<group>" to get a list of commands in <group>"help <command>" for help on <command>"help <tab>" to get a list of possible help topics"quit" to exitTo set redis-cli preferences:":set hints" enable online hints":set nohints" disable online hints
Set your preferences in ~/.redisclirc
127.0.0.1:6379> help @setSADD key member [member ...]summary: Add one or more members to a setsince: 1.0.0SCARD keysummary: Get the number of members in a setsince: 1.0.0SDIFF key [key ...]summary: Subtract multiple setssince: 1.0.0SDIFFSTORE destination key [key ...]summary: Subtract multiple sets and store the resulting set in a keysince: 1.0.0SINTER key [key ...]summary: Intersect multiple setssince: 1.0.0SINTERCARD numkeys key [key ...] [LIMIT limit]summary: Intersect multiple sets and return the cardinality of the resultsince: 7.0.0SINTERSTORE destination key [key ...]summary: Intersect multiple sets and store the resulting set in a keysince: 1.0.0SISMEMBER key membersummary: Determine if a given value is a member of a setsince: 1.0.0SMEMBERS keysummary: Get all the members in a setsince: 1.0.0SMISMEMBER key member [member ...]summary: Returns the membership associated with the given elements for a setsince: 6.2.0SMOVE source destination membersummary: Move a member from one set to anothersince: 1.0.0SPOP key [count]summary: Remove and return one or multiple random members from a setsince: 1.0.0SRANDMEMBER key [count]summary: Get one or multiple random members from a setsince: 1.0.0SREM key member [member ...]summary: Remove one or more members from a setsince: 1.0.0SSCAN key cursor [MATCH pattern] [COUNT count]summary: Incrementally iterate Set elementssince: 2.8.0SUNION key [key ...]summary: Add multiple setssince: 1.0.0SUNIONSTORE destination key [key ...]summary: Add multiple sets and store the resulting set in a keysince: 1.0.0127.0.0.1:6379>

清空缓存

127.0.0.1:6379> keys *
1) "k1"
127.0.0.1:6379> FLUSHDB
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379>

Redis Windows环境设置

1、redis 注册为服务
redis-server.exe --service-install redis.windows.conf --service-name redis --port 63792、redis 服务卸载
redis-server.exe --service-uninstall --service-name redis3、redis 设置密码
redis.windows.conf 文件中的requirepass password 
requirepass后为密码设置

3、String: 字符串

3.1、字符串

正反向索引:正向:从0开始依次递增。反向:最后一位为-1,前一位为-2,一直到最前方。

127.0.0.1:6379> help setSET key value [NX|XX] [GET] [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|KEEPTTL]summary: Set the string value of a keysince: 1.0.0group: string127.0.0.1:6379> set k1 hello
OK
127.0.0.1:6379> get k1
"hello"
127.0.0.1:6379> set k1 world nx   #nx:不存在key,才能设置成功, 使用场景:1.分布式锁2.只能新建
(nil)
127.0.0.1:6379> get k1
"hello"
127.0.0.1:6379> set k2 hello xx   #xx:存在key,才能设置成功,只能更新
(nil)
127.0.0.1:6379> get k2
(nil)
127.0.0.1:6379> mset k3 a k4 b
OK
127.0.0.1:6379> mget k3 k4
1) "a"
2) "b"
127.0.0.1:6379> APPEND k1 world
(integer) 10
127.0.0.1:6379> get k1
"helloworld"
#正反向索引
127.0.0.1:6379> GETRANGE k1 5 9
"world"
127.0.0.1:6379> GETRANGE k1 5 -1
"world"
127.0.0.1:6379> GETRANGE k1 0 -1
"helloworld"
127.0.0.1:6379> SETRANGE k1 5 ' man'
(integer) 10
127.0.0.1:6379> GETRANGE k1 0 -1
"hello mand"
127.0.0.1:6379> STRLEN k1
(integer) 10
127.0.0.1:6379> get k1
"hello mand"
127.0.0.1:6379> type k1
string
127.0.0.1:6379> FLUSHDB
OK
127.0.0.1:6379> help setSET key value [NX|XX] [GET] [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|KEEPTTL]summary: Set the string value of a keysince: 1.0.0group: string127.0.0.1:6379> set k1 99
OK
127.0.0.1:6379> type k1
string
127.0.0.1:6379> set k2 hello
OK
127.0.0.1:6379> type k2
string
127.0.0.1:6379> help OBJECTOBJECTsummary: A container for object introspection commandssince: 2.2.3group: genericOBJECT ENCODING keysummary: Inspect the internal encoding of a Redis objectsince: 2.2.3group: genericOBJECT FREQ keysummary: Get the logarithmic access frequency counter of a Redis objectsince: 4.0.0group: genericOBJECT HELPsummary: Show helpful text about the different subcommandssince: 6.2.0group: genericOBJECT IDLETIME keysummary: Get the time since a Redis object was last accessedsince: 2.2.3group: genericOBJECT REFCOUNT keysummary: Get the number of references to the value of the keysince: 2.2.3group: generic127.0.0.1:6379> OBJECT help1) OBJECT <subcommand> [<arg> [value] [opt] ...]. Subcommands are:2) ENCODING <key>3)     Return the kind of internal representation used in order to store the value4)     associated with a <key>.5) FREQ <key>6)     Return the access frequency index of the <key>. The returned integer is7)     proportional to the logarithm of the recent access frequency of the key.8) IDLETIME <key>9)     Return the idle time of the <key>, that is the approximated number of
10)     seconds elapsed since the last access to the key.
11) REFCOUNT <key>
12)     Return the number of references of the value associated with the specified
13)     <key>.
14) HELP
15)     Prints this help.
127.0.0.1:6379> OBJECT ENCODING k2
"embstr"
127.0.0.1:6379> OBJECT ENCODING k1
"int"
127.0.0.1:6379> get k1
"99"
127.0.0.1:6379> set k3 aa
OK
127.0.0.1:6379> OBJECT ENCODING k3
"embstr"
127.0.0.1:6379> APPEND k3 bb
(integer) 4
127.0.0.1:6379> OBJECT ENCODING k3
"raw"
127.0.0.1:6379> set k10 中
OK
127.0.0.1:6379> get k10    #redis 是二进制安全的
"\xe4\xb8\xad"
127.0.0.1:6379> exit
[root@localhost ~]# docker exec -it 16d9d3a548a1 redis-cli --raw
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get k10
中
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> keys *127.0.0.1:6379> MSETNX k1 a k2 b
1
127.0.0.1:6379> mget k1 k2
a
b
127.0.0.1:6379> MSETNX k2 c k3 d
0
127.0.0.1:6379> mget k1 k2 k3
a
b127.0.0.1:6379>

3.2、数值

字符串中有数值

127.0.0.1:6379> get k1
"99"
127.0.0.1:6379> INCR k1
(integer) 100
127.0.0.1:6379> get k1
"100"
127.0.0.1:6379> help incrINCR keysummary: Increment the integer value of a key by onesince: 1.0.0group: string127.0.0.1:6379> incrby k1 2
(integer) 102
127.0.0.1:6379> get k1
"102"
127.0.0.1:6379> DECR k1
(integer) 101
127.0.0.1:6379> DECR k1 2
(error) ERR wrong number of arguments for 'decr' command
127.0.0.1:6379> DECRBY k1 2
(integer) 99
127.0.0.1:6379> INCRBYFLOAT k1 0.5
"99.5"
127.0.0.1:6379> get k1
"99.5"
127.0.0.1:6379>

使用场景:抢购,秒杀,详情页,点赞,评论,规避并发下,对数据库的事物操作,完全由redis内存操作代替。

3.3、bitmap

127.0.0.1:6379> help setbitSETBIT key offset value    #offset:二进制位的偏移量,二进制位也是有索引的从0开始依次递增summary: Sets or clears the bit at offset in the string value stored at keysince: 2.2.0group: bitmap127.0.0.1:6379> setbit k1 1 1  #把二进位索引为1的位置的值设置为1,只能设置为1或0
0
127.0.0.1:6379> STRLEN k1
1
127.0.0.1:6379> get k1    #ascii码0100 0000代表@
@
127.0.0.1:6379> setbit k1 7 1
0
127.0.0.1:6379> STRLEN k1 
1
127.0.0.1:6379> get k1  #ascii码0100 0001代表A
A
127.0.0.1:6379> setbit k1 9 1 #长度增1超过8位
0
127.0.0.1:6379> STRLEN k1 
2
127.0.0.1:6379> get k1
A@
127.0.0.1:6379> help bitposBITPOS key bit [start [end [BYTE|BIT]]]  #start,end 二进制 字节索引summary: Find first bit set or clear in a stringsince: 2.8.7group: bitmap127.0.0.1:6379> bitpos k1 1 0 0  #二进制位1第一次出现的位置在0-0字节索引中
1
127.0.0.1:6379> bitpos k1 1 1 1 #二进制位1第一次出现的位置在1-1字节索引中
9
127.0.0.1:6379> bitpos k1 1 0 1 #二进制位1第一次出现的位置在0-1字节索引中
1
127.0.0.1:6379> help bitcountBITCOUNT key [start end [BYTE|BIT]] #start,end 二进制 字节索引summary: Count set bits in a stringsince: 2.6.0group: bitmap127.0.0.1:6379> bitcount k1 0 1  #k1的ascii码0100 0001 0100 0000 索引0-1字节有3个1
3
127.0.0.1:6379> bitcount k1 0 0
2
127.0.0.1:6379> bitcount k1 1 1
1
127.0.0.1:6379> help bitopBITOP operation destkey key [key ...]summary: Perform bitwise operations between stringssince: 2.6.0group: bitmap127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> setbit k1 1 1
0
127.0.0.1:6379> setbit k1 7 1
0
127.0.0.1:6379> get k1
A
127.0.0.1:6379> setbit k2 1 1
0
127.0.0.1:6379> setbit k2 6 1
0
127.0.0.1:6379> get k2
B
127.0.0.1:6379> bitop and andkey k1 k2
1
127.0.0.1:6379> get andkey
@
127.0.0.1:6379> bitop or orkey k1 k2
1
127.0.0.1:6379> get orkey
C
127.0.0.1:6379>

ASCII

[root@localhost ~]# yum install man man-pages  # 安装man man-pages
.....
[root@localhost ~]# man ascii
ASCII(7)                                                                        Linux Programmer's Manual                                                                       ASCII(7)NAMEascii - ASCII character set encoded in octal, decimal, and hexadecimalDESCRIPTIONASCII  is  the  American Standard Code for Information Interchange.  It is a 7-bit code.  Many 8-bit codes (such as ISO 8859-1, the Linux default character set) contain ASCII astheir lower half.  The international counterpart of ASCII is known as ISO 646.The following table contains the 128 ASCII characters.C program '\X' escapes are noted.Oct   Dec   Hex   Char                        Oct   Dec   Hex   Char────────────────────────────────────────────────────────────────────────000   0     00    NUL '\0'                    100   64    40    @001   1     01    SOH (start of heading)      101   65    41    A002   2     02    STX (start of text)         102   66    42    B003   3     03    ETX (end of text)           103   67    43    C004   4     04    EOT (end of transmission)   104   68    44    D005   5     05    ENQ (enquiry)               105   69    45    E006   6     06    ACK (acknowledge)           106   70    46    F007   7     07    BEL '\a' (bell)             107   71    47    G010   8     08    BS  '\b' (backspace)        110   72    48    H011   9     09    HT  '\t' (horizontal tab)   111   73    49    I012   10    0A    LF  '\n' (new line)         112   74    4A    J013   11    0B    VT  '\v' (vertical tab)     113   75    4B    K014   12    0C    FF  '\f' (form feed)        114   76    4C    L015   13    0D    CR  '\r' (carriage ret)     115   77    4D    M016   14    0E    SO  (shift out)             116   78    4E    N017   15    0F    SI  (shift in)              117   79    4F    O020   16    10    DLE (data link escape)      120   80    50    P021   17    11    DC1 (device control 1)      121   81    51    Q022   18    12    DC2 (device control 2)      122   82    52    R023   19    13    DC3 (device control 3)      123   83    53    S024   20    14    DC4 (device control 4)      124   84    54    T025   21    15    NAK (negative ack.)         125   85    55    U026   22    16    SYN (synchronous idle)      126   86    56    V027   23    17    ETB (end of trans. blk)     127   87    57    W030   24    18    CAN (cancel)                130   88    58    X031   25    19    EM  (end of medium)         131   89    59    Y032   26    1A    SUB (substitute)            132   90    5A    Z033   27    1B    ESC (escape)                133   91    5B    [034   28    1C    FS  (file separator)        134   92    5C    \  '\\'035   29    1D    GS  (group separator)       135   93    5D    ]036   30    1E    RS  (record separator)      136   94    5E    ^037   31    1F    US  (unit separator)        137   95    5F    _040   32    20    SPACE                       140   96    60    `041   33    21    !                           141   97    61    aManual page ascii(7) line 1 (press h for help or q to quit)

bitmap使用场景:1.统计用户登录天数,且窗口随机

#对user1用户进行登录统计  按照bitmap进行存储,46字节存储了一个用户一年的是否登录
#         1  2....7..........364  #代表天数
# user1   0  1 ...1..........364
# user2   ......................
#....用户数...
127.0.0.1:6379> setbit user1 1 1  
0
127.0.0.1:6379> setbit user1 7 1
0
127.0.0.1:6379> setbit user1 364 1
0
127.0.0.1:6379> STRLEN user1
46
127.0.0.1:6379> bitcount user1 -2 -1
1
127.0.0.1:6379>

使用场景2:统计系统中的活跃用户,1-3号之间的活跃用户总数

#统计20600101-20600103之间的用户活跃数,每一位代表一个用户
#            u1    u2    u3 .....u7..u9.......
#20600101    0    1        0  ...0...0........
#20600102    0    1        0  ...0...1........
#20600103    0    0        0  ...1...0........
127.0.0.1:6379> setbit 20600101 1 1
0
127.0.0.1:6379> setbit 20600102 9 1
0
127.0.0.1:6379> setbit 20600102 1 1
0
127.0.0.1:6379> setbit 20600103 7 1
0
127.0.0.1:6379> bitop or destkey 20600101 20600102 20600103   #或 去重
2
127.0.0.1:6379> bitcount destkey 0 -1
3
127.0.0.1:6379>

4、Hash: 散列

map(k-v):可对field进行数值计算

使用场景:点赞,收藏,详情页

127.0.0.1:6379> help @hashHDEL key field [field ...]summary: Delete one or more hash fieldssince: 2.0.0HEXISTS key fieldsummary: Determine if a hash field existssince: 2.0.0HGET key fieldsummary: Get the value of a hash fieldsince: 2.0.0HGETALL keysummary: Get all the fields and values in a hashsince: 2.0.0HINCRBY key field incrementsummary: Increment the integer value of a hash field by the given numbersince: 2.0.0HINCRBYFLOAT key field incrementsummary: Increment the float value of a hash field by the given amountsince: 2.6.0HKEYS keysummary: Get all the fields in a hashsince: 2.0.0HLEN keysummary: Get the number of fields in a hashsince: 2.0.0HMGET key field [field ...]summary: Get the values of all the given hash fieldssince: 2.0.0HMSET key field value [field value ...]summary: Set multiple hash fields to multiple valuessince: 2.0.0HRANDFIELD key [count [WITHVALUES]]summary: Get one or multiple random fields from a hashsince: 6.2.0HSCAN key cursor [MATCH pattern] [COUNT count]summary: Incrementally iterate hash fields and associated valuessince: 2.8.0HSET key field value [field value ...]summary: Set the string value of a hash fieldsince: 2.0.0HSETNX key field valuesummary: Set the value of a hash field, only if the field does not existsince: 2.0.0HSTRLEN key fieldsummary: Get the length of the value of a hash fieldsince: 3.2.0HVALS keysummary: Get all the values in a hashsince: 2.0.0127.0.0.1:6379> hset u1 name zwj
(integer) 1
127.0.0.1:6379> hmset u1 age 18 address dl
OK
127.0.0.1:6379> hget u1 name
"zwj"
127.0.0.1:6379> hmget u1 name age
1) "zwj"
2) "18"
127.0.0.1:6379> hkeys u1
1) "name"
2) "age"
3) "address"
127.0.0.1:6379> hvals u1
1) "zwj"
2) "18"
3) "dl"
127.0.0.1:6379> hgetall u1
1) "name"
2) "zwj"
3) "age"
4) "18"
5) "address"
6) "dl"
127.0.0.1:6379> HINCRBYFLOAT u1 age 0.5
"18.5"
127.0.0.1:6379> hget u1 age
"18.5"
127.0.0.1:6379> HINCRBYFLOAT u1 age -1
"17.5"
127.0.0.1:6379> hget u1 age
"17.5"
127.0.0.1:6379>

5、List: 列表

List:可以描述栈:同向命令

List:可以描述队列:反向命令

List:可以描述数组

List:阻塞,单播队列 FIFO

127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> lpush k1 a b c d e f  #lpush:从左边放元素
(integer) 6
127.0.0.1:6379> lpop k1
"f"
127.0.0.1:6379> lpop k1
"e"
127.0.0.1:6379> lpop k1
"d"
127.0.0.1:6379> lpush k1 a b c d e f
(integer) 6
127.0.0.1:6379> LRANGE k1 0 -1
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> rpush k2 a b c d e f
(integer) 6
127.0.0.1:6379> LRANGE k2 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"
127.0.0.1:6379> help lindexLINDEX key index   #根据索引取,类似数组summary: Get an element from a list by its indexsince: 1.0.0group: list127.0.0.1:6379> help lsetLSET key index element  #根据索引更新summary: Set the value of an element in a list by its indexsince: 1.0.0group: list127.0.0.1:6379> LRANGE k1 0 -1
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> LINDEX k1 2
"d"
127.0.0.1:6379> LINDEX k1 -1
"a"
127.0.0.1:6379> lset k1 3 xxx
OK
127.0.0.1:6379> LRANGE k1 0 -1
1) "f"
2) "e"
3) "d"
4) "xxx"
5) "b"
6) "a"
127.0.0.1:6379> help @listBLMOVE source destination LEFT|RIGHT LEFT|RIGHT timeoutsummary: Pop an element from a list, push it to another list and return it; or                                                                                                              block until one is availablesince: 6.2.0BLMPOP timeout numkeys key [key ...] LEFT|RIGHT [COUNT count]summary: Pop elements from a list, or block until one is availablesince: 7.0.0BLPOP key [key ...] timeoutsummary: Remove and get the first element in a list, or block until one is ava                                                                                                             ilablesince: 2.0.0BRPOP key [key ...] timeoutsummary: Remove and get the last element in a list, or block until one is avai                                                                                                             lablesince: 2.0.0BRPOPLPUSH source destination timeoutsummary: Pop an element from a list, push it to another list and return it; or                                                                                                              block until one is availablesince: 2.2.0LINDEX key indexsummary: Get an element from a list by its indexsince: 1.0.0LINSERT key BEFORE|AFTER pivot element   # 插入元素在前/后summary: Insert an element before or after another element in a listsince: 2.2.0LLEN keysummary: Get the length of a listsince: 1.0.0LMOVE source destination LEFT|RIGHT LEFT|RIGHTsummary: Pop an element from a list, push it to another list and return itsince: 6.2.0LMPOP numkeys key [key ...] LEFT|RIGHT [COUNT count]summary: Pop elements from a listsince: 7.0.0LPOP key [count]summary: Remove and get the first elements in a listsince: 1.0.0LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]summary: Return the index of matching elements on a listsince: 6.0.6LPUSH key element [element ...]summary: Prepend one or multiple elements to a listsince: 1.0.0LPUSHX key element [element ...]summary: Prepend an element to a list, only if the list existssince: 2.2.0LRANGE key start stopsummary: Get a range of elements from a listsince: 1.0.0LREM key count element  #移除元素  count为正,从头开始移除,为负,从后开始移除summary: Remove elements from a listsince: 1.0.0LSET key index elementsummary: Set the value of an element in a list by its indexsince: 1.0.0LTRIM key start stopsummary: Trim a list to the specified rangesince: 1.0.0RPOP key [count]summary: Remove and get the last elements in a listsince: 1.0.0RPOPLPUSH source destinationsummary: Remove the last element in a list, prepend it to another list and ret                                                                                                             urn itsince: 1.2.0RPUSH key element [element ...]summary: Append one or multiple elements to a listsince: 1.0.0RPUSHX key element [element ...]summary: Append an element to a list, only if the list existssince: 2.2.0127.0.0.1:6379>

6、Set: 集合

set:去重

127.0.0.1:6379> help @setSADD key member [member ...]summary: Add one or more members to a setsince: 1.0.0SCARD keysummary: Get the number of members in a setsince: 1.0.0SDIFF key [key ...]summary: Subtract multiple setssince: 1.0.0SDIFFSTORE destination key [key ...]summary: Subtract multiple sets and store the resulting set in a keysince: 1.0.0SINTER key [key ...]summary: Intersect multiple setssince: 1.0.0SINTERCARD numkeys key [key ...] [LIMIT limit]summary: Intersect multiple sets and return the cardinality of the resultsince: 7.0.0SINTERSTORE destination key [key ...]summary: Intersect multiple sets and store the resulting set in a keysince: 1.0.0SISMEMBER key membersummary: Determine if a given value is a member of a setsince: 1.0.0SMEMBERS keysummary: Get all the members in a setsince: 1.0.0SMISMEMBER key member [member ...]summary: Returns the membership associated with the given elements for a setsince: 6.2.0SMOVE source destination membersummary: Move a member from one set to anothersince: 1.0.0SPOP key [count]summary: Remove and return one or multiple random members from a setsince: 1.0.0SRANDMEMBER key [count]summary: Get one or multiple random members from a setsince: 1.0.0SREM key member [member ...]summary: Remove one or more members from a setsince: 1.0.0SSCAN key cursor [MATCH pattern] [COUNT count]summary: Incrementally iterate Set elementssince: 2.8.0SUNION key [key ...]summary: Add multiple setssince: 1.0.0SUNIONSTORE destination key [key ...]summary: Add multiple sets and store the resulting set in a keysince: 1.0.0127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> sadd k1 t a s d f fdsf we s t
(integer) 7
127.0.0.1:6379> SMEMBERS k1
1) "fdsf"
2) "s"
3) "f"
4) "d"
5) "a"
6) "we"
7) "t"
127.0.0.1:6379> SREM k1 s f   #移除元素
(integer) 2
127.0.0.1:6379> SMEMBERS k1
1) "fdsf"
2) "d"
3) "a"
4) "we"
5) "t"
127.0.0.1:6379> sadd k2 1 2 3 4 5
(integer) 5
127.0.0.1:6379> sadd k3 4 5 6 7 8
(integer) 5
127.0.0.1:6379> SINTER k2 k3  #集合的交集
1) "4"
2) "5"
127.0.0.1:6379> SINTERSTORE k4 k2 k3  #集合的交集并存储到key中
(integer) 2
127.0.0.1:6379> SMEMBERS k4
1) "4"
2) "5"
127.0.0.1:6379> SDIFF k2 k3  #差集 前后顺序,取前面的差集
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> SDIFF k3 k2  #差集
1) "6"
2) "7"
3) "8"
127.0.0.1:6379> help SRANDMEMBERSRANDMEMBER key [count]  #取出随机数 #正数:取出一个去重的结果集(不能超过已有集)#负数:取出一个带有重复的结果集,一定满足你要的数量#0:不返回#使用场景:抽奖summary: Get one or multiple random members from a setsince: 1.0.0group: set127.0.0.1:6379> sadd k10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
(integer) 33
127.0.0.1:6379> SRANDMEMBER k10 6 #模拟双色球等抽奖
1) "20"
2) "9"
3) "32"
4) "29"
5) "14"
6) "6"
127.0.0.1:6379> help SPOPSPOP key [count]  #取出1个,符合抽奖过程summary: Remove and return one or multiple random members from a setsince: 1.0.0group: set127.0.0.1:6379>

使用场景:抽奖

7、Sorted Set: 有序集合

Sorted Set:物理内存左小右大,不随命令发生变化

127.0.0.1:6379> help @sorted-setBZMPOP timeout numkeys key [key ...] MIN|MAX [COUNT count]summary: Remove and return members with scores in a sorted set or block until one is availablesince: 7.0.0BZPOPMAX key [key ...] timeoutsummary: Remove and return the member with the highest score from one or more sorted sets, or block until one is availablesince: 5.0.0BZPOPMIN key [key ...] timeoutsummary: Remove and return the member with the lowest score from one or more sorted sets, or block until one is availablesince: 5.0.0ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]summary: Add one or more members to a sorted set, or update its score if it already existssince: 1.2.0ZCARD keysummary: Get the number of members in a sorted setsince: 1.2.0ZCOUNT key min maxsummary: Count the members in a sorted set with scores within the given valuessince: 2.0.0ZDIFF numkeys key [key ...] [WITHSCORES]summary: Subtract multiple sorted setssince: 6.2.0ZDIFFSTORE destination numkeys key [key ...]summary: Subtract multiple sorted sets and store the resulting sorted set in a new keysince: 6.2.0ZINCRBY key increment membersummary: Increment the score of a member in a sorted setsince: 1.2.0ZINTER numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]summary: Intersect multiple sorted setssince: 6.2.0ZINTERCARD numkeys key [key ...] [LIMIT limit]summary: Intersect multiple sorted sets and return the cardinality of the resultsince: 7.0.0ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]summary: Intersect multiple sorted sets and store the resulting sorted set in a new keysince: 2.0.0ZLEXCOUNT key min maxsummary: Count the number of members in a sorted set between a given lexicographical rangesince: 2.8.9ZMPOP numkeys key [key ...] MIN|MAX [COUNT count]summary: Remove and return members with scores in a sorted setsince: 7.0.0ZMSCORE key member [member ...]summary: Get the score associated with the given members in a sorted setsince: 6.2.0ZPOPMAX key [count]summary: Remove and return members with the highest scores in a sorted setsince: 5.0.0ZPOPMIN key [count]summary: Remove and return members with the lowest scores in a sorted setsince: 5.0.0ZRANDMEMBER key [count [WITHSCORES]]summary: Get one or multiple random elements from a sorted setsince: 6.2.0ZRANGE key start stop [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]summary: Return a range of members in a sorted setsince: 1.2.0ZRANGEBYLEX key min max [LIMIT offset count]summary: Return a range of members in a sorted set, by lexicographical rangesince: 2.8.9ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]summary: Return a range of members in a sorted set, by scoresince: 1.0.5ZRANGESTORE dst src min max [BYSCORE|BYLEX] [REV] [LIMIT offset count]summary: Store a range of members from sorted set into another keysince: 6.2.0ZRANK key membersummary: Determine the index of a member in a sorted setsince: 2.0.0ZREM key member [member ...]summary: Remove one or more members from a sorted setsince: 1.2.0ZREMRANGEBYLEX key min maxsummary: Remove all members in a sorted set between the given lexicographical rangesince: 2.8.9ZREMRANGEBYRANK key start stopsummary: Remove all members in a sorted set within the given indexessince: 2.0.0ZREMRANGEBYSCORE key min maxsummary: Remove all members in a sorted set within the given scoressince: 1.2.0ZREVRANGE key start stop [WITHSCORES]summary: Return a range of members in a sorted set, by index, with scores ordered from high to lowsince: 1.2.0ZREVRANGEBYLEX key max min [LIMIT offset count]summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.since: 2.8.9ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]summary: Return a range of members in a sorted set, by score, with scores ordered from high to lowsince: 2.2.0ZREVRANK key membersummary: Determine the index of a member in a sorted set, with scores ordered from high to lowsince: 2.0.0ZSCAN key cursor [MATCH pattern] [COUNT count]summary: Incrementally iterate sorted sets elements and associated scoressince: 2.8.0ZSCORE key membersummary: Get the score associated with the given member in a sorted setsince: 1.2.0ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]summary: Add multiple sorted setssince: 6.2.0ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]summary: Add multiple sorted sets and store the resulting sorted set in a new keysince: 2.0.0127.0.0.1:6379> FLUSHALL
OK
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"
127.0.0.1:6379> ZINCRBY k1 2.5 banana
"4.5"
127.0.0.1:6379> ZRANGE k1 0 -1 withscores
1) "-1"
2) "0"
3) "orange"
4) "3"
5) "banana"
6) "4.5"
7) "apple"
8) "8"
127.0.0.1:6379> zrem k1 -1
(integer) 1
127.0.0.1:6379> ZRANGE k1 0 -1 withscores
1) "orange"
2) "3"
3) "banana"
4) "4.5"
5) "apple"
6) "8"
127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> zadd k1 80 a 60 b 70 c
(integer) 3
127.0.0.1:6379> zadd k2 60 b 100 d 40 e
(integer) 3
127.0.0.1:6379> ZUNIONSTORE k3 2 k1 k2
(integer) 5
127.0.0.1:6379> ZRANGE k3 0 -1 withscores1) "e"2) "40"3) "c"4) "70"5) "a"6) "80"7) "d"8) "100"9) "b"
10) "120"
127.0.0.1:6379> ZUNIONSTORE k4 2 k1 k2 weights 1 0.5  # 并集 权重
(integer) 5
127.0.0.1:6379> ZRANGE k4 0 -1 withscores 1) "e"2) "20"3) "d"4) "50"5) "c"6) "70"7) "a"8) "80"9) "b"
10) "90"
127.0.0.1:6379> ZUNIONSTORE k4 2 k1 k2 aggregate max  # 并集 取最大值
(integer) 5
127.0.0.1:6379> ZRANGE k4 0 -1 withscores1) "e"2) "40"3) "b"4) "60"5) "c"6) "70"7) "a"8) "80"9) "d"
10) "100"
127.0.0.1:6379> ZRANGE k4 0 -1
1) "e"
2) "b"
3) "c"
4) "a"
5) "d"

Sorted Set:排序是怎么实现的增删改查的速度?-->skip list:跳跃表

干我们这行,啥时候懈怠,就意味着长进的停止,长进的停止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!

相关文章:

Redis缓存数据库(一)

目录 一、概述 1、Redis 2、Redis的安装 Redis Windows环境设置 3、String: 字符串 3.1、字符串 3.2、数值 3.3、bitmap 4、Hash: 散列 5、List: 列表 6、Set: 集合 7、Sorted Set: 有序集合 一、概述 常识&#xff1a; 磁盘&#xff1a;1.寻址&#xff1a;ms&…...

物联网|uart串口相关寄存器|波特率设置及计算|发送处理代码|串口接收中断处理函数|物联网之蓝牙4.0 BLE基础-学习笔记(7)

文章目录 13 uart串口基础开发基本电路图&#xff1a;实验相关寄存器波特率设置及计算计算过程&#xff1a;设置中断发送处理代码串口接收中断处理函数main.c 13 uart串口基础开发 基本电路图&#xff1a; 实验相关寄存器 相关寄存器UxCSR、UxCSR、UxGCR、UxBUF、UxBAUD、CLK…...

有数·智享未来 | 新华三重磅发布绿洲平台3.0

5月10日&#xff0c;紫光股份旗下新华三集团以“有数智享未来”为主题&#xff0c;成功举办绿洲平台3.0新品发布会。全新一代绿洲平台实现内核进阶&#xff0c;以五大技术能力升级、五大行业方案沉淀、六类服务能力保障&#xff0c;三位一体构筑更领先的用数底座、更落地的用数…...

在Apex中获取Site URL

Foreword 目前SF暂未提供直接有效的方法在Apex获取SiteURL&#xff0c;我们可以在Idea (Access URL for a Site or Community from Apex)页面投票&#xff0c;除了下面提供的一种hack思路&#xff0c;当然也可以通过Custom Label手动维护。 Format of Site URL Sandbox site …...

【电子学会】2023年03月图形化三级 -- 比大小.md

文章目录 比大小1. 准备工作2. 功能实现3. 设计思路与实现&#xff08;1&#xff09;角色分析&#xff08;2&#xff09;背景分析&#xff08;3&#xff09;所用积木块介绍a. 运动类b. 外观类c. 事件类d. 控制类e. 运算类f. 变量类 &#xff08;4&#xff09;角色、舞台背景设置…...

Kali-linux使用Nessus

Nessus号称是世界上最流行的漏洞扫描程序&#xff0c;全世界有超过75000个组织在使用它。该工具提供完整的电脑漏洞扫描服务&#xff0c;并随时更新其漏洞数据库。Nessus不同于传统的漏洞扫描软件&#xff0c;Nessus可同时在本机或远端上遥控&#xff0c;进行系统的漏洞分析扫描…...

青训营 x 训练营结营测试题目(前端方向)

文章目录 &#x1f4cb;前言&#x1f3af;选择题&#xff08;含多选&#xff09;&#x1f4dd;最后 &#x1f4cb;前言 这篇文章的内容是23年6月青训营 x 训练营结营题目&#xff0c;题目一共有25题&#xff0c;题目类型为选择题&#xff0c;包括了单选题和多选题&#xff0c;…...

虚拟化技术介绍-VMware和Docker的区别

都说今天是一个云时代&#xff0c;其实云的本质就是由基础架构提供商提供基础架构&#xff0c;应用开发商不再关心基础架构。我们可以类比人类刚刚发明电的时候&#xff0c;工厂需要自己建电站&#xff0c;而现在只需要电线和插座就可以使用电。云时代让我们可以在分钟、甚至秒…...

TinyHttpd 运行过程出现的问题

最近拉了个 TinyHttpd 的工程下来&#xff0c;不过好像各个都有些改动&#xff0c;最后挑了篇阅读量最多的。工程也是从这里面给的链接下载的。 参考自&#xff1a;https://blog.csdn.net/jcjc918/article/details/42129311 拿下来在编译运行前&#xff0c;按这里说的&#x…...

【Linux】shell编程—数组

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 一、shell数组1,数组的概念2.数组的定义 二、Shell数组操作1. 获取数组的所有元素的列表2. 获取数组的所有元素下标3.取数组的元素个数4. 获取数组的某个元素的值5.…...

Maven仓库与Maven插件

目录 Maven 仓库 本地仓库 中央仓库 远程仓库 Maven 依赖搜索顺序 Maven 阿里云(Aliyun)仓库 gradle 配置指南 Maven 插件 插件类型 实例 Maven 仓库 在 Maven 的术语中&#xff0c;仓库是一个位置&#xff08;place&#xff09;。 Maven 仓库是项目中依赖的第三方库…...

【溯源反制】CDN域前置云函数-流量分析|溯源

文章目录 CDN隐藏C2地址环境搭建上传至威胁感知平台直接分析使用DNSQuerySniffer和Process Monitor定位进程网络流量分析文件属性(IDAPro Ollydbg) 域前置隐藏环境搭建威胁感知流量分析 云服务API网关/云函数云函数使用HTTPcs的流量可以简单的分为三个阶段 云函数使用HTTPS 总结…...

【Vue】学习笔记-全局事件总线

全局事件总线(GlobalEventBus) 一种可以在任意组件通信的方式&#xff0c;本质上就是一个对象&#xff0c;它必须满足以下条件 所有的组件对象都必须能看见他这个对象必须能够使用$ on $ emit $ off方法取绑定、触发和解绑事件 使用步骤 定义全局事件总线 //创建VUE new V…...

MATLAB数值运算(六)

目录 实验目的 实验内容 原创代码&#xff0c;仅供参考&#xff0c;不要直接CV呀 ~_~ 实验目的 1&#xff09;掌握定义符号对象和创建符号表达式的方法&#xff1b; 2&#xff09;掌握符号运算基本命令和规则&#xff1b; 3&#xff09;掌握符号表达式的运算法则以及符号矩阵…...

某医院Pad网络故障分析

分析背景 某医院为了加强信息安全管理&#xff0c;防止病人隐私信息泄露&#xff0c;采用部署“零信任”安全架构设计理念的企业移动安全支撑平台方案。 但在部署前期测试时&#xff0c;遇到了严重的性能问题。 在本次测试环境中&#xff0c;通过PAD访问患者转运业务&#x…...

git 撤销中间某次提交,保留其他提交的方法

今天上班脑抽了&#xff0c;吧test直接合到了uat,因为项目近期就我一个人开发&#xff0c;自己拉个三个分支再改不同的东西&#xff0c;最后都是发到test分支发测试&#xff0c;发生产的时候一个个和嫌麻烦&#xff0c;直接吧test分支怼到了uat&#xff0c;结果生产就出问题了&…...

空中下载技术(OTA)电控信息安全

随着汽车电子控制系统功能复杂度和数据颗粒度呈阶梯式增加&#xff0c;其发展速度逐渐超越网络安全防护方法、技术和标准的发展&#xff0c;现阶段汽车电子正面临巨大的网络信息安全风险&#xff0c;对功能安全的潜在影响也仍在探索和解决中&#xff0c;信息安全问题已经成为影…...

数据库sql语句(count(*)和count(字段))

例题&#xff1a; 创建如下两张表 分别命名为books和persons &#xff08;1&#xff09;按照书名&#xff0c;姓名的顺序列出字里包含‘德’字的人物的姓名&#xff0c;书名和字。 select name 姓名,bookname 书名,style 字 from books,persons where style like %德% and bo…...

短视频矩阵源码系统

短视频矩阵源码系统开发要则&#xff1a; 1. 需求分析&#xff1a;对短视频平台的需求进行全面分析&#xff0c;确立系统开发目标和方向。 2. 技术选型&#xff1a;选用最适合的技术开发短视频矩阵系统&#xff0c;如前端框架、数据库、服务器等。 3. 系统设计&#xff1a;按…...

检测数据类型

//typeof() 对于基本数据类型没问题&#xff0c;遇到引用数据类型不管用 console.log(typeof 666) //number console.log(typeof [1,2,3]) //object //instanceof() 只能判断引用数据类型&#xff0c;不能判断基本数据类型 console.log( [] instanceof Array) //true …...

【2023春招】4399 web后台-Java后端开发

目录 一、JVM1.类加载过程2.static和final变量的初始化时机 二、依赖1.Spring Boot 自动装配2.使用依赖过程中遇到问题如何排查3.引入的某个依赖不符合预期&#xff0c;如何处理 三、数据库&#xff1a;1.InnoDB 和 MyISAM 索引的区别2.字符串类型字段&#xff0c;WHERE 数字&a…...

干货分享:PCB防静电设计的必要性

平时通过走路穿衣等日常活动带来的摩擦&#xff0c;会产生不同幅值的静电电压&#xff0c;但其能量很小不会对人体产生伤害&#xff0c;不过对于电子元器件来说&#xff0c;这种静电能量却是不能忽视的。 在干燥的环境下&#xff0c;人体静电&#xff08;ESD&#xff09;的电压…...

电脑压缩包文件不见了怎么办?2种办法轻松找回电脑丢失文件!

一般情况下&#xff0c;为了节省磁盘空间或者传送文件时能够更快点&#xff0c;我们都会选择把文件进行压缩&#xff0c;这样会更加方便。 但时间一长&#xff0c;由于各种各样的原因&#xff0c;比如&#xff1a; 清理积累下来的压缩包时不小心把需要的压缩文件删除了&#x…...

如何申请gpt4.0-如何接入ChatGPT4

如何接入ChatGPT4 ChatGPT-4是OpenAI公司推出的最新自然语言处理技术&#xff0c;采用深度学习算法&#xff0c;旨在提供更加高效、准确的人工智能语言处理能力。如果您想要接入ChatGPT-4&#xff0c;您可以按照以下步骤&#xff1a; 注册OpenAI账号并申请API密钥&#xff1a;…...

设计模式-备忘录模式

备忘录模式 文章目录 备忘录模式什么是备忘录模式为什么要用备忘录模式如何使用备忘录模式总结 什么是备忘录模式 在不违背封装原则的前提下&#xff0c;捕获一个对象的内部状态&#xff0c;并在该对象之外保存这个状态&#xff0c;以便之后恢复对象为先前的状态。   在我看来…...

阿里、京东等大厂年薪50w的测试都是什么水平?

各位做测试的朋友&#xff0c;但凡经历过几次面试&#xff0c;那么你一定曾被问到过以下问题&#xff1a; 1、在Linux环境下&#xff0c;怎么执行web自动化测试&#xff1f; 2、Shell如何&#xff0c;Docker熟悉吗&#xff1f; 3、全链路的压测实操过吗&#xff0c;如何推进与开…...

Java PECS(Producer Extends Consumer Super)原则

在看 Alibaba 开发手册时遇到 PECS 原则&#xff0c;刚开始阅读时感觉比较绕&#xff0c;也搜索了一些博文参考&#xff0c;个人觉得 Stackoverflow 的这篇文章比较实用 —— What is PECS (Producer Extends Consumer Super)? 后面结合 JDK 源码梳理了下 // java/util/List…...

Learn RabbitMQ with SpringBoot

文章目录 What is RabbitMQ?RabbitMQ Core conceptRabbitMQ ArchitectureInstall and setup RabbitMQ using DockerExplore RabbitMQ using management UICreate and setup Springboot3 project in intellijSpringboot and RabbitMQ Basic MessageConnection between Springbo…...

定时器 POSIX Timer定时器和setitimer定时器

POSIX 可移植 POSIX&#xff1a;可移植操作系统接口&#xff08;Portable Operating System Interface of UNIX&#xff0c;缩写为 POSIX 。 POSIX Timer C API 总结POSIX系统的C标准库&#xff1a; 函数描述clock_settime()通过指定Value设置clock的分辨率clock_gettime()…...

DeSD:用于3D医学图像分割的深度自蒸馏自监督学习

文章目录 DeSD: Self-Supervised Learning with Deep Self-Distillation for 3D Medical Image Segmentation摘要本文方法Deep Self-DistillationDownstream Transfer Learning 实验结果 DeSD: Self-Supervised Learning with Deep Self-Distillation for 3D Medical Image Seg…...