国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Redis緩存數(shù)據(jù)庫

這篇具有很好參考價值的文章主要介紹了Redis緩存數(shù)據(jù)庫。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

目錄

一、概述

1、Redis

?2、Redis的安裝

Redis Windows環(huán)境設(shè)置

3、String: 字符串

3.1、字符串

3.2、數(shù)值

3.3、bitmap

4、Hash: 散列

5、List: 列表

6、Set: 集合

7、Sorted Set: 有序集合


一、概述

常識:

磁盤:1.尋址:ms(毫秒)2.帶寬:MB/s
內(nèi)存:1.尋址:ns? ? (納秒)?2.帶寬:GB/s
秒-->毫秒-->微妙-->納秒?
磁盤比內(nèi)存在尋址上慢了10W倍

內(nèi)存帶寬

前端總線頻率/工作頻率 最高帶寬

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

磁盤 內(nèi)存 常識

基礎(chǔ)知識:
關(guān)系型數(shù)據(jù)庫建表:必須給出schema,類型:字節(jié)寬度,存儲:傾向于行級存儲
面試題:數(shù)據(jù)庫:表很大,性能下降?
1、增刪改變慢
2、查詢速度呢?
?? ?1個或少量查詢依然很快
?? ?2.并發(fā)大的時候會受硬盤帶寬影響速度

SAP HANA:內(nèi)存級別的關(guān)系型數(shù)據(jù)庫 2T容量 ?貴:定制服務(wù)機器

數(shù)據(jù)在磁盤和內(nèi)存體積不一樣的

折中方案
memcached
redis
...
2個基礎(chǔ)設(shè)施
1.馮諾依曼體系的硬件
2.以太網(wǎng),tcp/ip的網(wǎng)絡(luò)

DB-Engines 數(shù)據(jù)庫引擎

1、Redis

NoSQL(NoSQL = Not Only SQL),意即"不僅僅是SQL",泛指非關(guān)系型的數(shù)據(jù)庫。隨著互聯(lián)網(wǎng)web2.0網(wǎng)站的興起,傳統(tǒng)的關(guān)系數(shù)據(jù)庫在應(yīng)付web2.0網(wǎng)站,特別是超大規(guī)模和高并發(fā)的SNS類型的web2.0純動態(tài)網(wǎng)站已經(jīng)顯得力不從心,暴漏了很多難以克服的問題,而非關(guān)系型的數(shù)據(jù)庫則由于其本身的特點得到了非常迅速的發(fā)展。NoSQL數(shù)據(jù)庫的產(chǎn)生就是為了解決大規(guī)模數(shù)據(jù)集合多重數(shù)據(jù)種類帶來的挑戰(zhàn),尤其是大數(shù)據(jù)應(yīng)用難題,包括超大規(guī)模數(shù)據(jù)的存儲。(例如谷歌每天為它們的用戶收集萬億比特的數(shù)據(jù))。這些類型的數(shù)據(jù)存儲不需要固定的模式,無需多余操作就可以橫向擴展。

Redis官網(wǎng)

Redis中文網(wǎng)站

Redis ( Remote DIctionary Server ) 通常被稱為數(shù)據(jù)結(jié)構(gòu)服務(wù)器,因為值 ( value ) 可以是字符串 ( String ) , 哈希 ( Map ) , 列表 ( list ) , 集合 ( Sets ) 或有序集合 ( Sorted Sets ) 等類型。

redis 是二進制安全的

?2、Redis的安裝

Docker中安裝redis? Spring Boot入門+深入(六)-Docker

Redis默認(rèn)有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庫
#因為我環(huán)境在docker中前面添加了docker exec -it 16d9d3a548a1:其他環(huán)境不用添加
[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 exit

To 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 @set

  SADD key member [member ...]
  summary: Add one or more members to a set
  since: 1.0.0

  SCARD key
  summary: Get the number of members in a set
  since: 1.0.0

  SDIFF key [key ...]
  summary: Subtract multiple sets
  since: 1.0.0

  SDIFFSTORE destination key [key ...]
  summary: Subtract multiple sets and store the resulting set in a key
  since: 1.0.0

  SINTER key [key ...]
  summary: Intersect multiple sets
  since: 1.0.0

  SINTERCARD numkeys key [key ...] [LIMIT limit]
  summary: Intersect multiple sets and return the cardinality of the result
  since: 7.0.0

  SINTERSTORE destination key [key ...]
  summary: Intersect multiple sets and store the resulting set in a key
  since: 1.0.0

  SISMEMBER key member
  summary: Determine if a given value is a member of a set
  since: 1.0.0

  SMEMBERS key
  summary: Get all the members in a set
  since: 1.0.0

  SMISMEMBER key member [member ...]
  summary: Returns the membership associated with the given elements for a set
  since: 6.2.0

  SMOVE source destination member
  summary: Move a member from one set to another
  since: 1.0.0

  SPOP key [count]
  summary: Remove and return one or multiple random members from a set
  since: 1.0.0

  SRANDMEMBER key [count]
  summary: Get one or multiple random members from a set
  since: 1.0.0

  SREM key member [member ...]
  summary: Remove one or more members from a set
  since: 1.0.0

  SSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate Set elements
  since: 2.8.0

  SUNION key [key ...]
  summary: Add multiple sets
  since: 1.0.0

  SUNIONSTORE destination key [key ...]
  summary: Add multiple sets and store the resulting set in a key
  since: 1.0.0

127.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環(huán)境設(shè)置

1、redis 注冊為服務(wù)
redis-server.exe --service-install redis.windows.conf --service-name redis --port 6379

2、redis 服務(wù)卸載
redis-server.exe --service-uninstall --service-name redis

3、redis 設(shè)置密碼
redis.windows.conf 文件中的requirepass password 
requirepass后為密碼設(shè)置

3、String: 字符串

3.1、字符串

正反向索引:正向:從0開始依次遞增。反向:最后一位為-1,前一位為-2,一直到最前方。

127.0.0.1:6379> help set

  SET 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 key
  since: 1.0.0
  group: string

127.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,才能設(shè)置成功, 使用場景:1.分布式鎖2.只能新建
(nil)
127.0.0.1:6379> get k1
"hello"
127.0.0.1:6379> set k2 hello xx   #xx:存在key,才能設(shè)置成功,只能更新
(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 set

  SET 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 key
  since: 1.0.0
  group: string

127.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 OBJECT

  OBJECT
  summary: A container for object introspection commands
  since: 2.2.3
  group: generic

  OBJECT ENCODING key
  summary: Inspect the internal encoding of a Redis object
  since: 2.2.3
  group: generic

  OBJECT FREQ key
  summary: Get the logarithmic access frequency counter of a Redis object
  since: 4.0.0
  group: generic

  OBJECT HELP
  summary: Show helpful text about the different subcommands
  since: 6.2.0
  group: generic

  OBJECT IDLETIME key
  summary: Get the time since a Redis object was last accessed
  since: 2.2.3
  group: generic

  OBJECT REFCOUNT key
  summary: Get the number of references to the value of the key
  since: 2.2.3
  group: generic

127.0.0.1:6379> OBJECT help
 1) OBJECT <subcommand> [<arg> [value] [opt] ...]. Subcommands are:
 2) ENCODING <key>
 3)     Return the kind of internal representation used in order to store the value
 4)     associated with a <key>.
 5) FREQ <key>
 6)     Return the access frequency index of the <key>. The returned integer is
 7)     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
b

127.0.0.1:6379>

3.2、數(shù)值

字符串中有數(shù)值

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 incr

  INCR key
  summary: Increment the integer value of a key by one
  since: 1.0.0
  group: string

127.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>

使用場景:搶購,秒殺,詳情頁,點贊,評論,規(guī)避并發(fā)下,對數(shù)據(jù)庫的事物操作,完全由redis內(nèi)存操作代替。

3.3、bitmap

127.0.0.1:6379> help setbit

  SETBIT key offset value    #offset:二進制位的偏移量,二進制位也是有索引的從0開始依次遞增
  summary: Sets or clears the bit at offset in the string value stored at key
  since: 2.2.0
  group: bitmap

127.0.0.1:6379> setbit k1 1 1  #把二進位索引為1的位置的值設(shè)置為1,只能設(shè)置為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 bitpos

  BITPOS key bit [start [end [BYTE|BIT]]]  #start,end 二進制 字節(jié)索引
  summary: Find first bit set or clear in a string
  since: 2.8.7
  group: bitmap

127.0.0.1:6379> bitpos k1 1 0 0  #二進制位1第一次出現(xiàn)的位置在0-0字節(jié)索引中
1
127.0.0.1:6379> bitpos k1 1 1 1 #二進制位1第一次出現(xiàn)的位置在1-1字節(jié)索引中
9
127.0.0.1:6379> bitpos k1 1 0 1 #二進制位1第一次出現(xiàn)的位置在0-1字節(jié)索引中
1
127.0.0.1:6379> help bitcount

  BITCOUNT key [start end [BYTE|BIT]] #start,end 二進制 字節(jié)索引
  summary: Count set bits in a string
  since: 2.6.0
  group: bitmap

127.0.0.1:6379> bitcount k1 0 1  #k1的ascii碼0100 0001 0100 0000 索引0-1字節(jié)有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 bitop

  BITOP operation destkey key [key ...]
  summary: Perform bitwise operations between strings
  since: 2.6.0
  group: bitmap

127.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)

NAME
       ascii - ASCII character set encoded in octal, decimal, and hexadecimal

DESCRIPTION
       ASCII  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 as
       their 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    A
       002   2     02    STX (start of text)         102   66    42    B
       003   3     03    ETX (end of text)           103   67    43    C
       004   4     04    EOT (end of transmission)   104   68    44    D
       005   5     05    ENQ (enquiry)               105   69    45    E
       006   6     06    ACK (acknowledge)           106   70    46    F
       007   7     07    BEL '\a' (bell)             107   71    47    G
       010   8     08    BS  '\b' (backspace)        110   72    48    H
       011   9     09    HT  '\t' (horizontal tab)   111   73    49    I
       012   10    0A    LF  '\n' (new line)         112   74    4A    J
       013   11    0B    VT  '\v' (vertical tab)     113   75    4B    K
       014   12    0C    FF  '\f' (form feed)        114   76    4C    L
       015   13    0D    CR  '\r' (carriage ret)     115   77    4D    M
       016   14    0E    SO  (shift out)             116   78    4E    N
       017   15    0F    SI  (shift in)              117   79    4F    O
       020   16    10    DLE (data link escape)      120   80    50    P
       021   17    11    DC1 (device control 1)      121   81    51    Q
       022   18    12    DC2 (device control 2)      122   82    52    R
       023   19    13    DC3 (device control 3)      123   83    53    S
       024   20    14    DC4 (device control 4)      124   84    54    T
       025   21    15    NAK (negative ack.)         125   85    55    U
       026   22    16    SYN (synchronous idle)      126   86    56    V
       027   23    17    ETB (end of trans. blk)     127   87    57    W
       030   24    18    CAN (cancel)                130   88    58    X
       031   25    19    EM  (end of medium)         131   89    59    Y
       032   26    1A    SUB (substitute)            132   90    5A    Z
       033   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    a
 Manual page ascii(7) line 1 (press h for help or q to quit)

bitmap使用場景:1.統(tǒng)計用戶登錄天數(shù),且窗口隨機

#對user1用戶進行登錄統(tǒng)計  按照bitmap進行存儲,46字節(jié)存儲了一個用戶一年的是否登錄
#         1  2....7..........364  #代表天數(shù)
# user1   0  1 ...1..........364
# user2   ......................
#....用戶數(shù)...
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:統(tǒng)計系統(tǒng)中的活躍用戶,1-3號之間的活躍用戶總數(shù)

#統(tǒng)計20600101-20600103之間的用戶活躍數(shù),每一位代表一個用戶
#            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進行數(shù)值計算

使用場景:點贊,收藏,詳情頁

127.0.0.1:6379> help @hash

  HDEL key field [field ...]
  summary: Delete one or more hash fields
  since: 2.0.0

  HEXISTS key field
  summary: Determine if a hash field exists
  since: 2.0.0

  HGET key field
  summary: Get the value of a hash field
  since: 2.0.0

  HGETALL key
  summary: Get all the fields and values in a hash
  since: 2.0.0

  HINCRBY key field increment
  summary: Increment the integer value of a hash field by the given number
  since: 2.0.0

  HINCRBYFLOAT key field increment
  summary: Increment the float value of a hash field by the given amount
  since: 2.6.0

  HKEYS key
  summary: Get all the fields in a hash
  since: 2.0.0

  HLEN key
  summary: Get the number of fields in a hash
  since: 2.0.0

  HMGET key field [field ...]
  summary: Get the values of all the given hash fields
  since: 2.0.0

  HMSET key field value [field value ...]
  summary: Set multiple hash fields to multiple values
  since: 2.0.0

  HRANDFIELD key [count [WITHVALUES]]
  summary: Get one or multiple random fields from a hash
  since: 6.2.0

  HSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate hash fields and associated values
  since: 2.8.0

  HSET key field value [field value ...]
  summary: Set the string value of a hash field
  since: 2.0.0

  HSETNX key field value
  summary: Set the value of a hash field, only if the field does not exist
  since: 2.0.0

  HSTRLEN key field
  summary: Get the length of the value of a hash field
  since: 3.2.0

  HVALS key
  summary: Get all the values in a hash
  since: 2.0.0

127.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:可以描述數(shù)組

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 lindex

  LINDEX key index   #根據(jù)索引取,類似數(shù)組
  summary: Get an element from a list by its index
  since: 1.0.0
  group: list

127.0.0.1:6379> help lset

  LSET key index element  #根據(jù)索引更新
  summary: Set the value of an element in a list by its index
  since: 1.0.0
  group: list

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> 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 @list

  BLMOVE source destination LEFT|RIGHT LEFT|RIGHT timeout
  summary: Pop an element from a list, push it to another list and return it; or                                                                                                              block until one is available
  since: 6.2.0

  BLMPOP timeout numkeys key [key ...] LEFT|RIGHT [COUNT count]
  summary: Pop elements from a list, or block until one is available
  since: 7.0.0

  BLPOP key [key ...] timeout
  summary: Remove and get the first element in a list, or block until one is ava                                                                                                             ilable
  since: 2.0.0

  BRPOP key [key ...] timeout
  summary: Remove and get the last element in a list, or block until one is avai                                                                                                             lable
  since: 2.0.0

  BRPOPLPUSH source destination timeout
  summary: Pop an element from a list, push it to another list and return it; or                                                                                                              block until one is available
  since: 2.2.0

  LINDEX key index
  summary: Get an element from a list by its index
  since: 1.0.0

  LINSERT key BEFORE|AFTER pivot element   # 插入元素在前/后
  summary: Insert an element before or after another element in a list
  since: 2.2.0

  LLEN key
  summary: Get the length of a list
  since: 1.0.0

  LMOVE source destination LEFT|RIGHT LEFT|RIGHT
  summary: Pop an element from a list, push it to another list and return it
  since: 6.2.0

  LMPOP numkeys key [key ...] LEFT|RIGHT [COUNT count]
  summary: Pop elements from a list
  since: 7.0.0

  LPOP key [count]
  summary: Remove and get the first elements in a list
  since: 1.0.0

  LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
  summary: Return the index of matching elements on a list
  since: 6.0.6

  LPUSH key element [element ...]
  summary: Prepend one or multiple elements to a list
  since: 1.0.0

  LPUSHX key element [element ...]
  summary: Prepend an element to a list, only if the list exists
  since: 2.2.0

  LRANGE key start stop
  summary: Get a range of elements from a list
  since: 1.0.0

  LREM key count element  #移除元素  count為正,從頭開始移除,為負(fù),從后開始移除
  summary: Remove elements from a list
  since: 1.0.0

  LSET key index element
  summary: Set the value of an element in a list by its index
  since: 1.0.0

  LTRIM key start stop
  summary: Trim a list to the specified range
  since: 1.0.0

  RPOP key [count]
  summary: Remove and get the last elements in a list
  since: 1.0.0

  RPOPLPUSH source destination
  summary: Remove the last element in a list, prepend it to another list and ret                                                                                                             urn it
  since: 1.2.0

  RPUSH key element [element ...]
  summary: Append one or multiple elements to a list
  since: 1.0.0

  RPUSHX key element [element ...]
  summary: Append an element to a list, only if the list exists
  since: 2.2.0

127.0.0.1:6379>

6、Set: 集合

set:去重

127.0.0.1:6379> help @set

  SADD key member [member ...]
  summary: Add one or more members to a set
  since: 1.0.0

  SCARD key
  summary: Get the number of members in a set
  since: 1.0.0

  SDIFF key [key ...]
  summary: Subtract multiple sets
  since: 1.0.0

  SDIFFSTORE destination key [key ...]
  summary: Subtract multiple sets and store the resulting set in a key
  since: 1.0.0

  SINTER key [key ...]
  summary: Intersect multiple sets
  since: 1.0.0

  SINTERCARD numkeys key [key ...] [LIMIT limit]
  summary: Intersect multiple sets and return the cardinality of the result
  since: 7.0.0

  SINTERSTORE destination key [key ...]
  summary: Intersect multiple sets and store the resulting set in a key
  since: 1.0.0

  SISMEMBER key member
  summary: Determine if a given value is a member of a set
  since: 1.0.0

  SMEMBERS key
  summary: Get all the members in a set
  since: 1.0.0

  SMISMEMBER key member [member ...]
  summary: Returns the membership associated with the given elements for a set
  since: 6.2.0

  SMOVE source destination member
  summary: Move a member from one set to another
  since: 1.0.0

  SPOP key [count]
  summary: Remove and return one or multiple random members from a set
  since: 1.0.0

  SRANDMEMBER key [count]
  summary: Get one or multiple random members from a set
  since: 1.0.0

  SREM key member [member ...]
  summary: Remove one or more members from a set
  since: 1.0.0

  SSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate Set elements
  since: 2.8.0

  SUNION key [key ...]
  summary: Add multiple sets
  since: 1.0.0

  SUNIONSTORE destination key [key ...]
  summary: Add multiple sets and store the resulting set in a key
  since: 1.0.0

127.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 SRANDMEMBER

  SRANDMEMBER key [count]  #取出隨機數(shù) 
                           #正數(shù):取出一個去重的結(jié)果集(不能超過已有集)
                           #負(fù)數(shù):取出一個帶有重復(fù)的結(jié)果集,一定滿足你要的數(shù)量
                           #0:不返回
                           #使用場景:抽獎
  summary: Get one or multiple random members from a set
  since: 1.0.0
  group: set

127.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 SPOP

  SPOP key [count]  #取出1個,符合抽獎過程
  summary: Remove and return one or multiple random members from a set
  since: 1.0.0
  group: set

127.0.0.1:6379>

使用場景:抽獎

7、Sorted Set: 有序集合

Sorted Set:物理內(nèi)存左小右大,不隨命令發(fā)生變化

127.0.0.1:6379> help @sorted-set

  BZMPOP timeout numkeys key [key ...] MIN|MAX [COUNT count]
  summary: Remove and return members with scores in a sorted set or block until one is available
  since: 7.0.0

  BZPOPMAX key [key ...] timeout
  summary: Remove and return the member with the highest score from one or more sorted sets, or block until one is available
  since: 5.0.0

  BZPOPMIN key [key ...] timeout
  summary: Remove and return the member with the lowest score from one or more sorted sets, or block until one is available
  since: 5.0.0

  ZADD 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 exists
  since: 1.2.0

  ZCARD key
  summary: Get the number of members in a sorted set
  since: 1.2.0

  ZCOUNT key min max
  summary: Count the members in a sorted set with scores within the given values
  since: 2.0.0

  ZDIFF numkeys key [key ...] [WITHSCORES]
  summary: Subtract multiple sorted sets
  since: 6.2.0

  ZDIFFSTORE destination numkeys key [key ...]
  summary: Subtract multiple sorted sets and store the resulting sorted set in a new key
  since: 6.2.0

  ZINCRBY key increment member
  summary: Increment the score of a member in a sorted set
  since: 1.2.0

  ZINTER numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
  summary: Intersect multiple sorted sets
  since: 6.2.0

  ZINTERCARD numkeys key [key ...] [LIMIT limit]
  summary: Intersect multiple sorted sets and return the cardinality of the result
  since: 7.0.0

  ZINTERSTORE 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 key
  since: 2.0.0

  ZLEXCOUNT key min max
  summary: Count the number of members in a sorted set between a given lexicographical range
  since: 2.8.9

  ZMPOP numkeys key [key ...] MIN|MAX [COUNT count]
  summary: Remove and return members with scores in a sorted set
  since: 7.0.0

  ZMSCORE key member [member ...]
  summary: Get the score associated with the given members in a sorted set
  since: 6.2.0

  ZPOPMAX key [count]
  summary: Remove and return members with the highest scores in a sorted set
  since: 5.0.0

  ZPOPMIN key [count]
  summary: Remove and return members with the lowest scores in a sorted set
  since: 5.0.0

  ZRANDMEMBER key [count [WITHSCORES]]
  summary: Get one or multiple random elements from a sorted set
  since: 6.2.0

  ZRANGE key start stop [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
  summary: Return a range of members in a sorted set
  since: 1.2.0

  ZRANGEBYLEX key min max [LIMIT offset count]
  summary: Return a range of members in a sorted set, by lexicographical range
  since: 2.8.9

  ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
  summary: Return a range of members in a sorted set, by score
  since: 1.0.5

  ZRANGESTORE dst src min max [BYSCORE|BYLEX] [REV] [LIMIT offset count]
  summary: Store a range of members from sorted set into another key
  since: 6.2.0

  ZRANK key member
  summary: Determine the index of a member in a sorted set
  since: 2.0.0

  ZREM key member [member ...]
  summary: Remove one or more members from a sorted set
  since: 1.2.0

  ZREMRANGEBYLEX key min max
  summary: Remove all members in a sorted set between the given lexicographical range
  since: 2.8.9

  ZREMRANGEBYRANK key start stop
  summary: Remove all members in a sorted set within the given indexes
  since: 2.0.0

  ZREMRANGEBYSCORE key min max
  summary: Remove all members in a sorted set within the given scores
  since: 1.2.0

  ZREVRANGE key start stop [WITHSCORES]
  summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
  since: 1.2.0

  ZREVRANGEBYLEX 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.9

  ZREVRANGEBYSCORE 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 low
  since: 2.2.0

  ZREVRANK key member
  summary: Determine the index of a member in a sorted set, with scores ordered from high to low
  since: 2.0.0

  ZSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate sorted sets elements and associated scores
  since: 2.8.0

  ZSCORE key member
  summary: Get the score associated with the given member in a sorted set
  since: 1.2.0

  ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
  summary: Add multiple sorted sets
  since: 6.2.0

  ZUNIONSTORE 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 key
  since: 2.0.0

127.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 withscores
 1) "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  # 并集 權(quán)重
(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 withscores
 1) "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:排序是怎么實現(xiàn)的增刪改查的速度?-->skip list:跳躍表

Redis緩存數(shù)據(jù)庫(二)

干我們這行,啥時候懈怠,就意味著長進的停止,長進的停止就意味著被淘汰,只能往前沖,直到鳳凰涅槃的一天!文章來源地址http://www.zghlxwxcb.cn/news/detail-440336.html

到了這里,關(guān)于Redis緩存數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Redis如何保證緩存和數(shù)據(jù)庫一致性?

    現(xiàn)在我們在面向增刪改查開發(fā)時,數(shù)據(jù)庫數(shù)據(jù)量大時或者對響應(yīng)要求較快,我們就需要用到Redis來拿取數(shù)據(jù)。 Redis:是一種高性能的內(nèi)存數(shù)據(jù)庫,它將數(shù)據(jù)以鍵值對的形式存儲在內(nèi)存中,具有讀寫速度快、支持多種數(shù)據(jù)類型、原子性操作、豐富的特性等優(yōu)勢。 優(yōu)勢: 性能極高

    2024年01月16日
    瀏覽(40)
  • Redis如何保障緩存與數(shù)據(jù)庫的數(shù)據(jù)一致性問題?

    Redis如何保障緩存與數(shù)據(jù)庫的數(shù)據(jù)一致性問題?

    目錄 一.最經(jīng)典的數(shù)據(jù)庫加緩存的雙寫雙刪模式 二. 高并發(fā)場景下的緩存+數(shù)據(jù)庫雙寫不一致問題分析與解決方案設(shè)計 三、上面高并發(fā)的場景下,該解決方案要注意的問題 1.1 Cache Aside Pattern概念以及讀寫邏輯 (1)讀的時候,先讀緩存,緩存沒有的話,那么就讀數(shù)據(jù)庫,然后取

    2023年04月21日
    瀏覽(29)
  • redis的緩存更新策略以及如何保證redis與數(shù)據(jù)庫的數(shù)據(jù)一致性

    redis的緩存更新策略有這么幾種: 1、由應(yīng)用直接和redis以及數(shù)據(jù)庫相連接: ?? ??? ?查詢數(shù)據(jù)時,應(yīng)用去redis中查詢,查不到的話再由應(yīng)用去數(shù)據(jù)庫中查詢,并將查詢結(jié)果放在redis; ?? ??? ?更新數(shù)據(jù)時,由應(yīng)用去觸發(fā)redis數(shù)據(jù)的刪除以及數(shù)據(jù)庫的update。 2、應(yīng)用只跟redi

    2024年02月13日
    瀏覽(25)
  • 數(shù)據(jù)庫緩存服務(wù)——NoSQL之Redis配置與優(yōu)化

    數(shù)據(jù)庫緩存服務(wù)——NoSQL之Redis配置與優(yōu)化

    目錄 一、緩存概念 1.1 系統(tǒng)緩存 1.2 緩存保存位置及分層結(jié)構(gòu) 1.2.1 DNS緩存 1.2.2 應(yīng)用層緩存 1.2.3 數(shù)據(jù)層緩存 1.2.4 硬件緩存 二、關(guān)系型數(shù)據(jù)庫與非關(guān)系型數(shù)據(jù)庫 2.1 關(guān)系型數(shù)據(jù)庫 2.2 非關(guān)系型數(shù)據(jù)庫 2.3 關(guān)系型數(shù)據(jù)庫和非關(guān)系型數(shù)據(jù)庫區(qū)別: 2.4 非關(guān)系型數(shù)據(jù)庫產(chǎn)生背景 2.5 總結(jié)

    2024年02月15日
    瀏覽(24)
  • Springboot+Redis:實現(xiàn)緩存 減少對數(shù)據(jù)庫的壓力

    Springboot+Redis:實現(xiàn)緩存 減少對數(shù)據(jù)庫的壓力

    ????歡迎光臨,終于等到你啦???? ??我是蘇澤,一位對技術(shù)充滿熱情的探索者和分享者。???? ??持續(xù)更新的專欄 Redis實戰(zhàn)與進階 本專欄講解Redis從原理到實踐 這是蘇澤的個人主頁可以看到我其他的內(nèi)容哦???? 努力的蘇澤 http://suzee.blog.csdn.net/ ? 目錄 緩存如何實現(xiàn)?

    2024年03月24日
    瀏覽(19)
  • Redis數(shù)據(jù)庫 | 發(fā)布訂閱、主從復(fù)制、哨兵模式、緩存雪崩

    Redis數(shù)據(jù)庫 | 發(fā)布訂閱、主從復(fù)制、哨兵模式、緩存雪崩

    ??wei_shuo的個人主頁 ??wei_shuo的學(xué)習(xí)社區(qū) ??Hello World ! Redis 發(fā)布訂閱 (pub/sub) 是一種消息通信模式:發(fā)送者 (pub) 發(fā)送消息,訂閱者 (sub) 接收消息 Redis 客戶端可以訂閱任意數(shù)量的頻道 Redis主從復(fù)制是指在Redis中設(shè)置一個主節(jié)點(Master)和一個或多個從節(jié)點(Slave),

    2024年02月15日
    瀏覽(24)
  • REDIS21_緩存雙寫一致方案、先更新數(shù)據(jù)庫再刪除緩存

    REDIS21_緩存雙寫一致方案、先更新數(shù)據(jù)庫再刪除緩存

    ①. 緩存雙寫一致性,談?wù)勀愕睦斫?如果redis中有數(shù)據(jù),需要和數(shù)據(jù)庫中的值相同 如果redis中無數(shù)據(jù),數(shù)據(jù)庫中的值要是最新值 ②. 什么時候同步直寫? 小數(shù)據(jù),某條、某一小戳熱點數(shù)據(jù),要求立刻變更,可以前臺服務(wù)降價一下,后臺馬上同步直寫 ③. 什么時候異步緩寫? 正常業(yè)務(wù),馬

    2023年04月08日
    瀏覽(17)
  • Redis緩存MySQL數(shù)據(jù)庫存儲二者如何保證數(shù)據(jù)一致性

    在大型互聯(lián)網(wǎng)應(yīng)用中,由于數(shù)據(jù)庫讀寫頻繁、壓力大等原因,我們通常會使用緩存來減少數(shù)據(jù)庫的訪問次數(shù),提高系統(tǒng)的性能。而Redis作為一個高性能的內(nèi)存數(shù)據(jù)庫,成為了緩存的首選方案之一。但是,緩存和數(shù)據(jù)庫之間存在數(shù)據(jù)一致性的問題,如何解決這個問題呢?本文將

    2023年04月19日
    瀏覽(26)
  • redis面試題目-如何保證數(shù)據(jù)庫與緩存的數(shù)據(jù)一致性

    原視頻:https://www.bilibili.com/video/BV1Km4y1r75f?p=62vd_source=fa75329ae3880aa55609265a0e9f5d34 由于緩存和數(shù)據(jù)庫是分開的,無法做到原子性的同時進行數(shù)據(jù)修改,可能出現(xiàn)緩存更新失敗,或者數(shù)據(jù)庫更新失敗的情況,這時候會出現(xiàn)數(shù)據(jù)不一致,影響前端業(yè)務(wù) 先更新數(shù)據(jù)庫,再更新緩存。緩

    2024年02月05日
    瀏覽(27)
  • Redis 緩存與數(shù)據(jù)庫雙寫不一致如何解決

    Redis緩存與數(shù)據(jù)庫雙寫不一致是一個常見的挑戰(zhàn),但可以通過一些方法來解決或減輕這種不一致性。以下是一些可能的解決方案: 事務(wù)處理: 在進行緩存和數(shù)據(jù)庫雙寫時,確保它們被包含在同一事務(wù)中。這可以通過使用支持事務(wù)的數(shù)據(jù)庫和Redis事務(wù)來實現(xiàn)。這樣,要么兩者同

    2024年01月21日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包