# Redis Remote Dictionary Server,可做為資料庫、快取、訊息代理程式和佇列使用。所有 Redis 資料都位於記憶體內,而資料庫的資料則存放在磁碟或 SSD 上,所以相較起來 Redis 可避免搜尋時間延遲,還能在幾微秒的時間內存取資料,平均讀取和寫入操作時間低於一毫秒,並支援每秒百萬個操作。 ## Install ```shell $ brew install redis ``` ## Start ```shell $ redis-server 29316:C 12 Jun 2020 14:29:34.897 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 29316:C 12 Jun 2020 14:29:34.897 # Redis version=6.0.5, bits=64, commit=00000000, modified=0, pid=29316, just started 29316:C 12 Jun 2020 14:29:34.897 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 29316:M 12 Jun 2020 14:29:34.899 * Increased maximum number of open files to 10032 (it was originally set to 256). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 6.0.5 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 29316 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 29316:M 12 Jun 2020 14:29:34.909 # Server initialized 29316:M 12 Jun 2020 14:29:34.911 * Ready to accept connections ``` ## Usage ### Value ```python >>> import redis >>> r = redis.Redis(host='localhost', port=6379) >>> r.set('foo', 'bar') True >>> r.get('foo') 'bar' ``` ### Password 暫時性的方式,重啟後會失效: ```shell $ redis-cli 127.0.0.1:6379> config get requirepass 1) "requirepass" 2) "" 127.0.0.1:6379> config set requirepass password OK 127.0.0.1:6379> config get requirepass 1) "requirepass" 2) "password" ``` 永久的方式是修改設定檔,重啟 Redis 即生效: ```shell $ vim /usr/local/etc/redis.conf # requirepass foobared ↓ requirepass $(PASSWORD) ``` ## 參考資料 - [Python Redis](https://redislabs.com/lp/python-redis/) - [Redis quickstart](https://redis.io/topics/quickstart) - [Try Redis](http://try.redis.io/) - [Redis Document](https://redis-py.readthedocs.io/en/latest)