成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Redis-py官方文檔翻譯

Alfred / 1984人閱讀

摘要:采取兩種實現(xiàn)命令其一類盡量堅持官方語法,但是以下除外沒有實現(xiàn),應該是線程安全的原因。線程安全性是線程安全的。由于線程安全原因,不提供實現(xiàn),因為它會導致數(shù)據(jù)庫的切換。

官網(wǎng):https://github.com/andymccurd...
當前版本:2.10.5
注:這不是完整翻譯,只提取了關(guān)鍵信息。省略了部分內(nèi)容,如lua腳本支持。

pip install redis
pip install hiredis(解析器,可選。windows下好像不行。)

>>> import redis
>>> r = redis.StrictRedis(host="localhost", port=6379, db=0)
>>> r.set("foo", "bar")
True
>>> r.get("foo")
"bar"

redis-py采取兩種client class實現(xiàn)redis命令:
其一、StrictRedis類盡量堅持官方語法,但是以下除外:

SELECT: 沒有實現(xiàn),應該是線程安全的原因。

DEL: 由于del是python語法關(guān)鍵字,所用delete來代替。

CONFIG GET|SET: 分開用 config_get or config_set來代替

MULTI/EXEC: 事務作為Pipeline類的其中一部分的實現(xiàn)。Pipeline默認保證了MULTI,EXEC聲明。但是你可以指定transaction=False來禁用這一行為。

SUBSCRIBE/LISTEN:PubSub作為一個獨立的類來實現(xiàn)發(fā)布訂閱機制。

SCAN/SSCAN/HSCAN/ZSCAN:每個命令都對應一個等價的迭代器方法scan_iter/sscan_iter/hscan_iter/zscan_iter methods for this behavior.

其二、Redis類是StrictRedis的子類,提供redis-py版本向后的兼容性。

關(guān)于StrictRedis與Redis的區(qū)別:(官方推薦使用StrictRedis.)
以下幾個方法在StrictRedis和Redis類中的參數(shù)順序不同。
LREM: Order of "num" and "value" arguments reversed such that "num" can provide a default value of zero.
在Redis類中是這樣的:
lrem(self, name, value, num=0)
在StrictRedis類中是這樣的:
lrem(self, name, count, value)

ZADD: Redis specifies the "score" argument before "value". These were swapped accidentally when being implemented and not discovered until after people were already using it. The Redis class expects *args in the form of: name1, score1, name2, score2, ...
在Redis類中是這樣的:
redis.zadd("my-key", "name1", 1.1, "name2", 2.2, name3=3.3, name4=4.4)
在StrictRedis中是這樣的:
redis.zadd("my-key", 1.1, "name1", 2.2, "name2", name3=3.3, name4=4.4)

SETEX: Order of "time" and "value" arguments reversed.
在Redis類中是這樣的:
setex(self, name, value, time)
而在StrictRedis中是這樣的:
setex(self, name, time, value)

連接池
>>> pool = redis.ConnectionPool(host="localhost", port=6379, db=0)
>>> r = redis.Redis(connection_pool=pool)

Connections:redis-py提供兩種類型的連接:基于TCP端口的,基于Unix socket文件的(需要redis服務器開啟配置)。

>>> r = redis.Redis(unix_socket_path="/tmp/redis.sock")

如果你需要,自定義連接類,需要告知連接池。

>>> pool = redis.ConnectionPool(connection_class=YourConnectionClass,
                                your_arg="...", ...)

釋放連接回到連接池:可以使用Redis類的reset()方法,或者使用with上下文管理語法。

解析器:
解析器控制如何解析Redis-server的響應內(nèi)容,redis-py提供兩種方式的解析器類支持:PythonParser和HiredisParser(需要多帶帶安裝)。它優(yōu)先選用HiredisParser,如果不存在,則選用PythonParser. Hiredis是redis核心團隊開發(fā)的一個高性能c庫,能夠提高10x的解析速度。

響應回調(diào):
The client class使用一系列的callbacks來完成響應到對應python類型的映射。這些響應回調(diào),定義在 Redis client class中的RESPONSE_CALLBACKS字典中。你可以使用set_response_callback 方法來添加自定義回調(diào)類。這個方法接受兩個參數(shù):一個命令名字,一個回調(diào)類?;卣{(diào)類接受至少一個參數(shù):響應內(nèi)容,關(guān)鍵字參數(shù)作為命令調(diào)用時的參數(shù)。

線程安全性:

Redis client instances是線程安全的。由于線程安全原因,不提供select實現(xiàn),因為它會導致數(shù)據(jù)庫的切換。
在不同線程間傳遞PubSub or Pipeline對象也是不安全的。

Pipelines

Pipelines是Redis類的一個子類,支持緩存多個命令,然后作為單個請求發(fā)送。通過減少TCP請求次數(shù)來達到提供性能的目的。

>>> r = redis.Redis(...)
>>> r.set("bing", "baz")
>>> # Use the pipeline() method to create a pipeline instance
>>> pipe = r.pipeline()
>>> # The following SET commands are buffered
>>> pipe.set("foo", "bar")
>>> pipe.get("bing")
>>> # the EXECUTE call sends all buffered commands to the server, returning
>>> # a list of responses, one for each command.
>>> pipe.execute()
[True, "baz"]

Pipelines的實現(xiàn)采用流式API,故而你可以采用以下鏈式調(diào)用的方式:

>>> pipe.set("foo", "bar").sadd("faz", "baz").incr("auto_number").execute()
[True, True, 6]

Pipelines默認以原子性(事務)的形式執(zhí)行所有緩存的命令,你也可以禁用這一行為:

>>> pipe = r.pipeline(transaction=False)

WATCH命令提供了在事務之前檢測一個或多個key值的變化。一旦在事務執(zhí)行之前,某個值發(fā)生了變化,那么事務將被取消然后拋出WatchError 異常。
利用watch我們可以實現(xiàn)client-side incr命令:

>>> with r.pipeline() as pipe:
...     while 1:
...         try:
...             # put a WATCH on the key that holds our sequence value
...             pipe.watch("OUR-SEQUENCE-KEY")
...             # after WATCHing, the pipeline is put into immediate execution
...             # mode until we tell it to start buffering commands again.
...             # this allows us to get the current value of our sequence
...             current_value = pipe.get("OUR-SEQUENCE-KEY")
...             next_value = int(current_value) + 1
...             # now we can put the pipeline back into buffered mode with MULTI
...             pipe.multi()
...             pipe.set("OUR-SEQUENCE-KEY", next_value)
...             # and finally, execute the pipeline (the set command)
...             pipe.execute()
...             # if a WatchError wasn"t raised during execution, everything
...             # we just did happened atomically.
...             break
...        except WatchError:
...             # another client must have changed "OUR-SEQUENCE-KEY" between
...             # the time we started WATCHing it and the pipeline"s execution.
...             # our best bet is to just retry.
...             continue

不過你可以使用transaction方法來簡化這一操作:它包含handling and retrying watch errors的樣板代碼。第一參數(shù)為callable(這個callable只能接受一個Pipeline參數(shù)),及多個需要被WATCH的keys

>>> def client_side_incr(pipe):
...     current_value = pipe.get("OUR-SEQUENCE-KEY")
...     next_value = int(current_value) + 1
...     pipe.multi()
...     pipe.set("OUR-SEQUENCE-KEY", next_value)
>>>
>>> r.transaction(client_side_incr, "OUR-SEQUENCE-KEY")
[True]
Publish / Subscribe

PubSub對象subscribes to channels and listens for new messages。

>>> r = redis.StrictRedis(...)
>>> p = r.pubsub()

>>> p.subscribe("my-first-channel", "my-second-channel", ...)
>>> p.psubscribe("my-*", ...)

>>> p.get_message()
{"pattern": None, "type": "subscribe", "channel": "my-second-channel", "data": 1L}
>>> p.get_message()
{"pattern": None, "type": "subscribe", "channel": "my-first-channel", "data": 2L}
>>> p.get_message()
{"pattern": None, "type": "psubscribe", "channel": "my-*", "data": 3L}

通過PubSub獲取消息時返回的是一個字典,字典key有如下幾個:
type:其中一個, "subscribe", "unsubscribe", "psubscribe", "punsubscribe", "message", "pmessage"
channel: The channel [un]subscribed to or the channel a message was published to
pattern: The pattern that matched a published message"s channel. Will be None in all cases except for "pmessage" types.
data: The message data. With [un]subscribe messages, this value will be the number of channels and patterns the connection is currently subscribed to. With [p]message messages, this value will be the actual published message.
現(xiàn)在來發(fā)布消息:

# the publish method returns the number matching channel and pattern
# subscriptions. "my-first-channel" matches both the "my-first-channel"
# subscription and the "my-*" pattern subscription, so this message will
# be delivered to 2 channels/patterns
>>> r.publish("my-first-channel", "some data")
2
>>> p.get_message()
{"channel": "my-first-channel", "data": "some data", "pattern": None, "type": "message"}
>>> p.get_message()
{"channel": "my-first-channel", "data": "some data", "pattern": "my-*", "type": "pmessage"}

取消訂閱:如果沒有傳遞任何參數(shù),那么這個PubSub訂閱的所有的channels or patterns都會被取消。

>>> p.unsubscribe()
>>> p.punsubscribe("my-*")
>>> p.get_message()
{"channel": "my-second-channel", "data": 2L, "pattern": None, "type": "unsubscribe"}
>>> p.get_message()
{"channel": "my-first-channel", "data": 1L, "pattern": None, "type": "unsubscribe"}
>>> p.get_message()
{"channel": "my-*", "data": 0L, "pattern": None, "type": "punsubscribe"}
回調(diào)的方式處理發(fā)布的消息

redis-py還允許你通過回調(diào)的方式處理發(fā)布的消息。
Message handlers接受一個參數(shù),the message,是一個字典對象。just like the examples above.
以回調(diào)形式訂閱:subscribe接受關(guān)鍵字參數(shù),鍵為channels or patterns,值為回調(diào)函數(shù)。

>>> def my_handler(message):
...     print "MY HANDLER: ", message["data"]
>>> p.subscribe(**{"my-channel": my_handler})

在你注冊了回調(diào)處理的情況下, get_message()會返回None。

默認情況下除了發(fā)布消息之外,還會傳遞 subscribe/unsubscribe成功的確認消息,如果你不想接收它們:ignore_subscribe_messages=True

>>> p = r.pubsub(ignore_subscribe_messages=True)
>>> p.subscribe("my-channel")
>>> p.get_message()  # hides the subscribe message and returns None
>>> r.publish("my-channel")
1
>>> p.get_message()
{"channel": "my-channel", "data": "my data", "pattern": None, "type": "message"}
三種讀取消息的方式

第一種:無限循環(huán)通過PubSub對象的get_message()讀取消息

>>> while True:
>>>     message = p.get_message()
>>>     if message:
>>>         # do something with the message
>>>     time.sleep(0.001)  # be nice to the system :)

第二種,通過阻塞方法listen()來讀取:p.listen()返回一個generator,阻塞直到有消息可獲取。

>>> for message in p.listen():
...     # do something with the message

第三種,開啟一個事件循環(huán)線程pubsub.run_in_thread()方法 creates a new thread and starts the event loop. 并返回線程對象。
但是需要注意的是:如果你沒有注冊消息處理函數(shù),那么調(diào)用run_in_thread()將會拋出異常redis.exceptions.PubSubError

>>> p.subscribe(**{"my-channel": my_handler})
>>> thread = p.run_in_thread(sleep_time=0.001)
# the event loop is now running in the background processing messages
# when it"s time to shut it down...
>>> thread.stop()
關(guān)于字符編碼:

默認情況下,publish的消息會被編碼,當你獲取消息時得到的是編碼后的字節(jié),如果你需要它自動解碼,創(chuàng)建Redis client實例時需要指定decode_responses=True,(譯者注:不建議使用該選項,因為當存在pickle序列化的值時,client.get(key)時會出現(xiàn)解碼失敗的錯誤UnicodeDecodeError)

關(guān)閉釋放資源:

PubSub.close() method to shutdown the connection.

>>> p = r.pubsub()
>>> ...
>>> p.close()
LUA Scripting支持:

略。

Sentinel support與節(jié)點發(fā)現(xiàn):

Redis Sentinel用于發(fā)現(xiàn)Redis節(jié)點。請確保至少一個Sentinel daemon 進程在運行。
你可以使用Sentinel connection to discover the master and slaves network addresses:

>>> from redis.sentinel import Sentinel
>>> sentinel = Sentinel([("localhost", 26379)], socket_timeout=0.1)
>>> sentinel.discover_master("mymaster")
("127.0.0.1", 6379)
>>> sentinel.discover_slaves("mymaster")
[("127.0.0.1", 6380)]

>>> master = sentinel.master_for("mymaster", socket_timeout=0.1)
>>> slave = sentinel.slave_for("mymaster", socket_timeout=0.1)
>>> master.set("foo", "bar")
>>> slave.get("foo")
"bar"

上面的master and slave對象就是一個普通的StrictRedis對象實例。如果Sentinel配置了連接池的話,它們還會使用這個連接池。
可能拋出的異常:MasterNotFoundError ,SlaveNotFoundError 它們都是ConnectionError的子類

Scan Iterators

Redis 2.8之后有了*SCAN命令。redis-py also exposes the following methods that return Python iterators for convenience: scan_iter, hscan_iter, sscan_iter and zscan_iter.

>>> for key, value in (("A", "1"), ("B", "2"), ("C", "3")):
...     r.set(key, value)
>>> for key in r.scan_iter():
...     print key, r.get(key)
A 1
B 2
C 3

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/38234.html

相關(guān)文章

  • 區(qū)分Python的redis客戶端:hiredis、hiredis-py、redis-py

    摘要:由于學習實戰(zhàn)用的是,需要按的客戶端庫。提供兩個類,和。還寫了一個版本的即。使用可以使得客戶端解析服務端響應內(nèi)容的速度提升倍。而且當執(zhí)行檢索多條數(shù)據(jù)時性能更顯著,如等。 由于學習《Redis實戰(zhàn)》用的是Python,需要按Python的redis客戶端庫。被幾個庫搞得有點暈,在此區(qū)分一下。 區(qū)分hiredis、hiredis-py、redis-py redis官網(wǎng)Github:https...

    Hanks10100 評論0 收藏0
  • Django-緩存

    摘要:自帶了一個健壯的緩存系統(tǒng)來保存動態(tài)頁面,避免每次請求都重新計算。緩存中的和方法是很常見的。盡量放在第一個繼承的類設置過期時間根據(jù)自己需求加緩存。目前這個緩存使用的是內(nèi)存。 概述:對于中等流量的網(wǎng)站來說,盡可能的減少開銷是非常必要的。緩存數(shù)據(jù)就是為了保存那些需要很多計算資源的結(jié)果,這樣的話就不必在下次重復消耗計算資源。獲取數(shù)據(jù)的數(shù)據(jù)的時候就是去緩存中拿,拿到了直接返回,沒拿到就去數(shù)據(jù)庫中...

    aervon 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<