【云圖說】第235期 DDS讀寫兩步走 帶您領略只讀節點的風采
742
2022-05-28
broker緩存:
- mongodb 存硬盤
- redis 默認存內存,配置可存硬盤
- memcache 只能存內存
redis介紹
REmote DIctionary Server(Redis)
redis官網:https://redis.io/
redis數據類型:
- String 操作 set get
- Hash 操作 hset hget
- List 操作 lpush lrange
- Set 操作 sadd smembers
- Sort Set 操作
安裝
windows安裝下載:
https://github.com/MicrosoftArchive/redis/releases
啟動服務:
切換目錄到 C:\redis 運行 redis-server.exe redis.windows.conf
連接服務:
切換到redis目錄下運行 redis-cli.exe -h 127.0.0.1 -p 6379
設置鍵值對 set myKey abc
取出鍵值對 get myKey
安裝第三方庫
pip install redis
1
簡單連接
import redis r = redis.Redis(host="127.0.0.1", port=6379) r.set("foo", "xxx") print(r.get("foo")) # b'xxx'
1
2
3
4
5
6
7
url鏈接
redis://username[:password]@host:port/db # TCP連接
1
2
連接池
import redis pool = redis.ConnectionPool(host="127.0.0.1", port=6379) r = redis.Redis(connection_pool=pool) r.set("cat", "Tom") print(r.get("cat"))
1
2
3
4
5
6
7
管道
import redis pool = redis.ConnectionPool(host="127.0.0.1", port=6379) r = redis.Redis(connection_pool=pool) pipe=r.pipeline(transaction=True) pipe.set("key1", "value1") pipe.set("key2", "value2") pipe.execute() # 一起執行 print(r.get("key1"))
1
2
3
4
5
6
7
8
9
10
11
12
13
發布者和訂閱者
# 封裝的公共類 import redis class RedisHelper: def __init__(self): self.__conn = redis.Redis(host='127.0.0.1') self.chan_sub = 'fm104.5' self.chan_pub = 'fm104.5' def public(self, msg): self.__conn.publish(self.chan_pub, msg) return True def subscribe(self): pub = self.__conn.pubsub() pub.subscribe(self.chan_sub) pub.parse_response() return pub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 發布者 import redis_helper obj = redis_helper.RedisHelper() obj.public('hello') print("發布成功")
1
2
3
4
5
6
7
8
# 訂閱者 import redis_helper obj = redis_helper.RedisHelper() redis_sub = obj.subscribe() print("開始訂閱") while True: msg = redis_sub.parse_response() print(msg)
1
2
3
4
5
6
7
8
9
10
11
12
參考文章:
redis-py
https://github.com/andymccurdy/redis-py/
Redis 命令參考
http://doc.redisfans.com/
《Redis 教程-菜鳥教程》
http://www.runoob.com/redis/redis-tutorial.html
《Python之路【第九篇】:Python操作Redis》
http://www.cnblogs.com/wupeiqi/articles/5132791.html
《python 之路,Day12 - redis緩存數據庫》
http://www.cnblogs.com/alex3714/articles/6217453.html
Redis 數據庫
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。