redis序列化_scrapy_redis中序列化源码及其在程序设计中的应用
序列化 (Serialization)是將對象的狀態信息轉換為可以存儲或傳輸的形式的過程。在序列化期間,對象將其當前狀態寫入到臨時或持久性存儲區。以后,可以通過從存儲區中讀取或反序列化對象的狀態,重新創建該對象。
在scrapy_redis中,一個Request對象先經過DupeFilter去重,然后遞交給scheduler調度儲存在Redis中,這就面臨一個問題,Request是一個對象,Redis不能存儲該對象,這時就需要將request序列化儲存。
scrapy中序列化模塊如下:
from scrapy_redis import picklecompat
"""A pickle wrapper module with protocol=-1 by default."""try:import cPickle as pickle # PY2 except ImportError:import pickledef loads(s):return pickle.loads(s)def dumps(obj):return pickle.dumps(obj, protocol=-1)當然python3直接使用pickle模塊, 已經沒有cPickle,該模塊最為重要的兩個方法,序列化與反序列化如上,通過序列化后的對象我們可以存儲在數據庫、文本等文件中,并快速恢復。
同時模式設計中的備忘錄模式通過這種方式達到最佳效果《python設計模式(十九):備忘錄模式》;可序列化的對象和數據類型如下:
- None, True,False
- 整數,長整數,浮點數,復數
- 普通字符串和Unicode字符串
- 元組、列表、集合和字典,只包含可選擇的對象。
- 在模塊頂層定義的函數
- 在模塊頂層定義的內置函數
- 在模塊的頂層定義的類。
- 這些類的實例
嘗試對不可序列化對象進行操作,將引發PicklingError異常;發生這種情況時,可能已經將未指定的字節數寫入基礎文件。嘗試選擇高度遞歸的數據結構可能會超過最大遞歸深度,RuntimeError在這種情況下會被提起。
模塊API
pickle.dump(obj, file[, protocol])
- Write a pickled representation of obj to the open file object file. This is equivalent to Pickler(file, protocol).dump(obj).
If the protocol parameter is omitted, protocol 0 is used. If protocol is specified as a negative value or HIGHEST_PROTOCOL, the highest protocol version will be used.Changed in version 2.3: Introduced the protocol parameter.file must have a write() method that accepts a single string argument. It can thus be a file object opened for writing, a StringIO object, or any other custom object that meets this interface. - pickle.load(file)
- Read a string from the open file object file and interpret it as a pickle data stream, reconstructing and returning the original object hierarchy. This is equivalent to Unpickler(file).load().file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return a string. Thus file can be a file object opened for reading, a StringIO object, or any other custom object that meets this interface.
This function automatically determines whether the data stream was written in binary mode or not. - pickle.dumps(obj[, protocol])
- Return the pickled representation of the object as a string, instead of writing it to a file.
If the protocol parameter is omitted, protocol 0 is used. If protocol is specified as a negative value or HIGHEST_PROTOCOL, the highest protocol version will be used.Changed in version 2.3: The protocol parameter was added. - pickle.loads(string)
- Read a pickled object hierarchy from a string. Characters in the string past the pickled object’s representation are ignored.
- Write a pickled representation of obj to the open file object file. This is equivalent to Pickler(file, protocol).dump(obj).
至于應用場景,比較常見的有如下幾種:
程序重啟時恢復上次的狀態、會話存儲、對象的網絡傳輸。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的redis序列化_scrapy_redis中序列化源码及其在程序设计中的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器同时装mysql和sqlserve
- 下一篇: springboot 读取配置文件_使用