--- tags: Redis --- # Ch1. Getting to know Redis ## 1.1. What is Redis? * redis is a database, sometimes we call it NoSQL or Non-relational * memcached: 也是一種儲存空間,但是好像只能存 string。which is a high performance key value server * redis 與 memcached 的比較 * redis 寫資料到硬碟的方式有 2 種 * memcached 只能儲存 string * table1.1: 資料庫種類比較  * in-memory database: 簡單的說就是單純儲存在記憶體中的資料庫,而非外部的儲存空間 * 如果 redis 背叛該怎麼辦? * 方法一 1. 當某寫條件觸發時,可以啟動 point-in-time dump 2. 當 1 個或 2 個 dump-to-disk commands 被呼叫時 * 方法二 1. 用 append-only 的檔案且直接用 command 來修改或寫入這些 redis 裡面的檔案 2. append-only 的檔案可以被設定成在指定的時間內不會去 sync,或者過了某些 operation 操作過後再 sync 3. 會在 chapter 4 討論更深 * 傳統的 database 要 updating row 是很慢的,因為這是 random read & random write * 在 redis 可以使用 INCR: 在 redis 的 random write 是快的 * 繼續讀 redis 會發現我們想要解決很多 real time 的問題 ## 1.2. What Redis data structures look like * 這小節介紹 redis 的資料結構,以及如何使用指令才操作這些資料 * redis 最常用的 5 個資料結構 * STRINGs * LISTs * SETs * HASHes: 很像 dictionary 的東西,其實也是 key-value 的結構 * ZSET * 以上每個都有共通的 command 指令 DEL, TYPE, RENAME, and others * ZSET 是在 redis 比較特別的資料結構:sorted set * table 1.2: redis 資料型態  * 可以用 appendix A 來安裝 redis ## 1.3 Hello Redis * 使用 redis 解決文章點擊排序的範例: [notebook link](https://github.com/yurei79601/PythonNotes/blob/master/redis/learn_redis.ipynb) ## 其他補充資訊 ### 1. local 安裝 Redis * 參考連結: [link](https://redis.io/docs/getting-started/installation/install-redis-on-mac-os/) * 以 mac 為例 1. 確認是否有 brew ``` brew --version ``` 2. 使用 mac 指令安裝 redis ```bash brew install redis ``` 3. 在欲儲存 redis 資料的地方執行。這個地方就會變成 redis database ``` redis-server ``` ### 2. 申請遠端連線的 Redis 空間 1. 到這個[網站](https://redis.com/try-free/?_ga=2.247645063.1261926520.1666015070-1729647689.1665501378),申請 account 或者使用第三方帳號認證。我個人使用 google 登入 2. 點選 Free-db 會進入到 DB 資訊欄,可以找到 endpoint 以及 password 3. 這兩個資訊可以到 python 環境取得 redis_conn,方法如下 ```python import redis def create_redis_cloud_conn(): endpoint = "" # 2. 第一張圖複製出來的 hostname = endpoint.split(':')[0] # 冒號前面是 hostname port = int(endpoint.split(':')[1]) # 冒號後面是 port password = "" # 2. 第二張圖複製出來的 redis_conn = redis.Redis( host=hostname, port=port, password=password ) return redis_conn ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up