SlideShare a Scribd company logo
Redis資料型別與場景的連結
Jamis Liao
http://mvc.tw
About Me
2
Tag
• ASP.Net MVC
• C#
• DevOps
• 軟體開發流程
• 系統架構
• Cloud Architecture
• Jamis
• 微軟最有價值專家
• 天橋下的說書人
Blog
Redis是甚麼(可以吃嗎……老梗)
3
 Remote Dictionary Server
 In-memory data structure store
 Platform
 Windows
 Mac
 Linux
4
Redis到底有多快
5
太快了………拿個Mac Air 11給它就好
6
 以Pipelining執行,一次送16條命令
這樣還不夠………
7
 給你滿滿的Redis大平台Cluster
Cluster
8
 Data sharding
 16384 slot
 Redis Cluster master-slave model
9
Redis Cluster
A (0~5500)
B (5501~11000) C (11001~16384)
10
Redis Cluster
A (0~5500)
B (5501~11000) C (11001~16384)
11
Redis Cluster Master-Slave
(0~5500)
(5501~11000) (11001~16384)
A A’
B B’ C C’
Master-Slave
12
 一個Master允許多個Slave
 Non-blocking
 透過專門的Slave來做持久化
持久化
13
 RDB
指定的时間間隔對資料進行快照存儲
 save 900 1
 save 300 10
 save 60 10000
 AOF
紀錄每次對Redis Server寫入的操作
Redis HA策略
14
 Master-Slave
 Sentinel
 port
 sentinel monitor
 sentinel auth-pass
 sentinel down-after-milliseconds
 sentinel failover-timeout
有誰可以用呢?
15
ActionScript
Bash
C
C#
C++
Clojure
Common Lisp
Crystal
D
Dart
Elixir
emacs lisp
Erlang
Fancy
Gawk
GNU Prolog
Go
Haskell
Haxe
Lo
Java
Javascript
Julia
Lua
Matlab
Nim
Node.js
Objectlive-C
Ocaml
Pascal
Perl
PHP
Pure Data
Python
R
Racket
Rebol
Ruby
Rust
Scala
Scheme
Smalltalk
Swift
Tcl
VB
VCL
C#可以用的套件
16
StackExchange.Redis
V.S
ServiceStack.Redis
為什麼選擇 StackExchange.Redis
17
免錢
V.S
要錢
18
Redis Setting Demo
開始來說正事了…………Redis Data Type
19
 Strings
 Redis最基本的型別,也是最基本
的使用方式
 最常見的兩種使用方式
 直接存字串
 物件序列成化成字串存到Redis
命令集
20
 https://redis.io/commands#string
 http://www.redis.cn/commands.html#string
21
Strings Demo
第一個場景
22
這是一個敗家的moment… https://goo.gl/UlmG7C
尋找敗家目標時最容易看到的………分頁
23
通常為了效能,我們會這樣做
24
 將每一頁的資料放到Cache
如果分頁顯示的數量可改變
25
把不同數量的顯示資料通通存起來
26
http://bbs.roheya.com/read.php?tid-6908.html
但是…………………………………這樣好嗎?
27
https://goo.gl/yt7iS3
試試看Redis Lists
28
 Lists是由字串組成的一個集合
 從頭尾取出一個元素
 從集合中取出任一範圍的元素集合
https://goo.gl/W9dOBz
 根據新增或插入的順序將集合中的元素排序
命令集
29
 https://redis.io/commands#list
 http://www.redis.cn/commands.html#generic
30
Lists Demo
第二個場景
31
現在很常見的文章+Tag
使用Tag的場景
32
使用Tag搜尋
33
那一次搜尋兩個Tag………
34
嗯………..沒有這個功能
其實另一個服務Pocket也沒有
35
Redis Sets該上場了
36
 Sets是由字串元素組成的一個集合
https://goo.gl/1ucVpJ
如何一次搜尋兩個Tag…………
37
 儲存的元素為無序並且唯一
 提供了交集與差集的功能
https://goo.gl/Eb62gf
Sets的另一個場景
38
樂觀鎖定…..這是什麼鬼
樂觀鎖定……………做啥用
39
http://sponsor.accupass.com/fb/_rush-ticket
除了應付網站流量
40
還要擔心不可以超賣
不超賣最簡單的方式……先準備好用發的
41
https://goo.gl/iTT9u0
但是……………事情真的有那麼簡單嗎?
42
如果Select到同一張票的資料怎麼辦?
SQL Server的樂觀鎖定
43
 SQL Server 樂觀鎖定的作法
 利用Timestamp (RowVersion)
https://goo.gl/5oLXle
Redis Sets的樂觀鎖定()
44
 Redis Sets 樂觀鎖定的作法
 拿走就沒了
 需搭配Redis Transaction
命令集
45
 https://redis.io/commands#set
 http://www.redis.cn/commands.html#set
46
Sets Demo
第四個場景
47
熱門排行榜(文章)
那些年曾經有過的……點部落熱門文章
48
曾經牛逼過的……博客園閱讀排行榜
49
現在滿紅的Segmentfault
50
好用的Sorted Sets輕鬆完成
51
 Sorted Sets是由字串元素組成的一個
集合,而每個元素都會關連到一個浮动
数值
https://goo.gl/QGTiWD
命令集
52
 https://redis.io/commands#sorted_set
 http://www.redis.cn/commands.html#sorted_set
53
Sorted Sets Demo
第五個場景
54
這個場景有點不好說
來做一個提供天氣資訊的服務
55
 資訊可能會包含
• 溫度
• 最高溫度
• 最低溫度
• 降雨機率
• 風力
• 風向
一般來說會把天氣資訊放到快取
56
 天氣資訊進去快取的步驟通常是這樣
1. 把物件序列化成字串
2. 把字串存進快取
3. 從快取把字串讀出來
4. 把字串反序列化成字串
在一次提供所有資訊的服務是很正常
57
 但是…………………如果是下面這些服務
1. 取得最高 / 最低溫度
2. 取得降雨機率
3. 取得風力 / 風向
複習一下快取的步驟
58
 把物件序列化成字串
 把字串存進快取
 從快取把字串讀出來
 把字串反序列化成字串
好像有哪裡怪怪的………………
59
我只需要部份資訊阿
來看看Hashes
60
 Hash是由field和關聯的value组成的map
https://goo.gl/rgRHtp
命令集
61
 https://redis.io/commands#hash
 http://www.redis.cn/commands.html#hash
還有兩種沒提到的資料型別……今天不會講
62
 HyperLogLogs
 Bitmaps
還有一件要特別提的事
63
 Pipelining
 這是一個將Client命令打包一次送到
Redis Server的概念
 Transactions
 這是一個將命令打包,做一次執行的概念
Transactions的五個指令
64
 MULTI
 EXEC
 DISCARD
 WATCH
 UNWATCH
http://mvc.tw
Blog 是記錄知識的最佳平台
65
http://mvc.tw
感謝 Jetbrains 贊助贈品
66
https://www.jetbrains.com/resharper/
http://mvc.tw
感謝 OzCode 贊助贈品
67
http://www.oz-code.com/
http://mvc.tw
業界師資、實戰教學
68
http://skilltree.my
http://mvc.tw
業界師資、實戰教學
69
https://mvc.tw
謝謝各位
• 本投影片所包含的商標與文字皆屬原著作者所有。
• 本投影片使用的圖片皆從網路搜尋。
• 本著作係採用姓名標示-非商業性-相同方式分享 3.0 台灣授權。閱讀本授權條款,請到
http://creativecommons.org/licenses/by-nc-sa/3.0/tw/,或寫信至Creative Commons, 444 Castro
Street, Suite 900, Mountain View, California, 94041, USA.
h t t p s : / / m v c . t w

More Related Content

PDF
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
PDF
twMVC#19 | opserver監控服務的解決
PPTX
twMVC#31沒有 hdd 的網站重構 webform to mvc
PDF
twMVC#30 | 你應該瞭解的 container-on-azure-二三事
PDF
twMVC#29 | 當.Net Core 遇到AWS Lambda
PDF
twMVC#23 | 快速上手 Azure Functions
PDF
twMVC#26 | 淺談 ASP.NET Caching 技術與實踐
PPTX
twMVC#31網站上線了然後呢
twMVC#21 | 以實例說明ASP.NET Web API 服務的開發與測試過程
twMVC#19 | opserver監控服務的解決
twMVC#31沒有 hdd 的網站重構 webform to mvc
twMVC#30 | 你應該瞭解的 container-on-azure-二三事
twMVC#29 | 當.Net Core 遇到AWS Lambda
twMVC#23 | 快速上手 Azure Functions
twMVC#26 | 淺談 ASP.NET Caching 技術與實踐
twMVC#31網站上線了然後呢

What's hot (20)

PDF
twMVC#33玩轉 Azure 彈性部署
PDF
twMVC#32應用 ASP.NET WebAPI2 Odata 建置高互動性 APIS
PDF
twMVC#38 How we migrate tfs to git(using azure dev ops)
PDF
twMVC#36讓 Exceptionless 存管你的 Log
PDF
twMVC#44 讓我們用 k6 來進行壓測吧
PDF
twMVC#42 Windows容器導入由0到1
PPTX
Asp.net 5 新功能與變革
PDF
twMVC#42 Azure DevOps Service Pipeline設計與非正常應用
PDF
twMVC#42 讓我們用一種方式來開發吧
PDF
twMVC#33聊聊如何自建 Facebook {廣告} 服務 with API
PPTX
twMVC#41 The journey of source generator
PDF
twMVC#21 | 你所不知道的 Visual Studio
PPTX
Asp.net mvc 6 新功能初探
PDF
twMVC#43 Visual Studio 2022 新功能拆解
PPTX
Frontend Devops at Cloudinsight
PDF
twMVC#43 YARP
PDF
twMVC#42 Azure IoT Hub for Smart Factory
PPTX
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
PDF
容器與 Gitlab CI 應用
PPTX
ASP.NET Core 6.0 全新功能探索
twMVC#33玩轉 Azure 彈性部署
twMVC#32應用 ASP.NET WebAPI2 Odata 建置高互動性 APIS
twMVC#38 How we migrate tfs to git(using azure dev ops)
twMVC#36讓 Exceptionless 存管你的 Log
twMVC#44 讓我們用 k6 來進行壓測吧
twMVC#42 Windows容器導入由0到1
Asp.net 5 新功能與變革
twMVC#42 Azure DevOps Service Pipeline設計與非正常應用
twMVC#42 讓我們用一種方式來開發吧
twMVC#33聊聊如何自建 Facebook {廣告} 服務 with API
twMVC#41 The journey of source generator
twMVC#21 | 你所不知道的 Visual Studio
Asp.net mvc 6 新功能初探
twMVC#43 Visual Studio 2022 新功能拆解
Frontend Devops at Cloudinsight
twMVC#43 YARP
twMVC#42 Azure IoT Hub for Smart Factory
實現 Angular, Docker 與 Kubernetes 持續部署 (NG+2020)
容器與 Gitlab CI 應用
ASP.NET Core 6.0 全新功能探索
Ad

More from twMVC (14)

PDF
twMVC#51 以平台工程重新思考系統設計 - 以 Batch System 為例封面
PDF
twMVC#51-GitHub Copilot 徹底改變開發模式,探索 AI 驅動的智慧程式碼協作
PDF
twMVC#50 微服務上線後的救贖
PDF
twMVC 47_Elastic APM 的兩三事
PDF
twMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
PDF
.NET 7 家族新成員: Microsoft Orleans v7
PDF
twMVC#46 一探 C# 11 與 .NET 7 的神奇
PDF
twMVC#44 如何測試與保護你的 web application with playwright
PDF
twMVC#43 C#10 新功能介紹
PDF
twMVC#41 hololens2 MR
PDF
twMVC#36C#的美麗與哀愁
PDF
twMVC#36.NetCore 3快速看一波
PDF
twMVC#30 | Bootstrap 搶先玩
PDF
twMvc#30 | 技術人員與業務團隊的無礙的溝通法則
twMVC#51 以平台工程重新思考系統設計 - 以 Batch System 為例封面
twMVC#51-GitHub Copilot 徹底改變開發模式,探索 AI 驅動的智慧程式碼協作
twMVC#50 微服務上線後的救贖
twMVC 47_Elastic APM 的兩三事
twMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
.NET 7 家族新成員: Microsoft Orleans v7
twMVC#46 一探 C# 11 與 .NET 7 的神奇
twMVC#44 如何測試與保護你的 web application with playwright
twMVC#43 C#10 新功能介紹
twMVC#41 hololens2 MR
twMVC#36C#的美麗與哀愁
twMVC#36.NetCore 3快速看一波
twMVC#30 | Bootstrap 搶先玩
twMvc#30 | 技術人員與業務團隊的無礙的溝通法則
Ad

twMVC#26 | Redis資料型別與場景的連結