Redis與Memcache對比

  1. Memcache是內存對象緩存系統,設計目標為通過緩解數據庫的壓力來加快Web應用的響應速度。
  2. Redis應用場景更加豐富,Memcache僅適合作為緩存使用。

Redis工作模式

  • ----> 【單進程】
  • ----> 【阻塞式】
    Redis在同一時刻只能處理一個請求,後來的請求需要排隊等待!!!

Redis優缺點

  • 優點: 無需處理併發問題,降低系統複雜度
  • 缺點: 不適合緩存大尺寸對象(超過100KB\10萬)

Redis常用數據類型

  • 字符串類型
  • HASH哈希對象
  • List列表(有序、重複)
  • Set集合(無序、不重複)
  • ZSet有序集合(元素有分值、用於排序)

Redis應用場景

  • 頻率控制:接口防刷,密碼嘗試次數限制;
  • 數量統計: 請求量統計;
  • 數量控制: 商品搶購,獎勵額度控制;

Redis計數器

Redis命令行操作

127.0.0.1:6379> exists mycounter
(integer) 0
127.0.0.1:6379> set mycounter 99
OK
127.0.0.1:6379> get mycounter
"99"
127.0.0.1:6379> incr mycounter
(integer) 100
127.0.0.1:6379> get mycounter
"100"
127.0.0.1:6379> incrby mycounter 100
(integer) 200
127.0.0.1:6379> get mycounter
"200"
127.0.0.1:6379> decrby mycounter -100
(integer) 300
127.0.0.1:6379> incrby mycounter -200
(integer) 100
127.0.0.1:6379> get mycounter
"100"
127.0.0.1:6379> setx mycounter 99
(error) ERR unknown command `setx`, with args beginning with: `mycounter`, `99`,
127.0.0.1:6379> setnx mycounter 99
(integer) 0
127.0.0.1:6379> setnx test 99
(integer) 1
127.0.0.1:6379> get test
"99"
127.0.0.1:6379> get mycounter
"100"
127.0.0.1:6379> expire mycounter 30
(integer) 1
127.0.0.1:6379> ttl mycounter
(integer) 20
127.0.0.1:6379> ttl mycounter
(integer) -2

Redis數量控制器-V1、V2版本

應用場景

  • 商品搶購。沒有控制住?庫存不夠了,成本失控;
  • 抽獎限量。沒控制住?錢多花了;
  • 搶紅包。

數量控制器V1

es只能插入1萬條數據為什麼_Redis

數量控制器V2

es只能插入1萬條數據為什麼_es只能插入1萬條數據為什麼_02

V1和V2源碼實現

es只能插入1萬條數據為什麼_緩存_03