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 orHIGHEST_PROTOCOL
, the highest protocol version will be used.Changed in version 2.3: Introduced the protocol parameter.file must have awrite()
method that accepts a single string argument. It can thus be a file object opened for writing, aStringIO
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, aread()
method that takes an integer argument, and areadline()
method that requires no arguments. Both methods should return a string. Thus file can be a file object opened for reading, aStringIO
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 orHIGHEST_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.
至于应用场景,比较常见的有如下几种:
程序重启时恢复上次的状态、会话存储、对象的网络传输。
redis序列化_scrapy_redis中序列化源码及其在程序设计中的应用相关推荐
- 20220910最新版Redis7源码编译及windows中安装
20220910最新版Redis7源码编译及windows中安装 文章目录 20220910最新版Redis7源码编译及windows中安装 1.Cygwin安装 1 Cygwin介绍 ...is i ...
- 如何将spring源码作为导入eclipse中,变成一个普通的项目(git、github)
引子: 怎么查看spring-framework的源码?是不是用压缩软件解压jar包,然后用编辑软件看?高端一点的,是在eclipse上面,按住Ctrl键跳转着看?这里我给大家介绍更加高端一点的方法. ...
- JDK源码解析 —— IO流中的包装类使用到了装饰者模式
JDK源码解析 IO流中的包装类使用到了装饰者模式. BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter ...
- java 自定义arraylist_Java 中模仿源码自定义ArrayList
Java 中模仿源码自定义ArrayList 最近看了下ArrayList的源码,抽空根据ArrayList的底层结构写了一个功能简单无泛型的自定义ArrayLsit,帮助自己更好理解ArrayLis ...
- 从源码角度解析Android中APK安装过程
从源码角度解析Android中APK的安装过程 1. Android中APK简介 Android应用Apk的安装有如下四种方式: 1.1 系统应用安装 没有安装界面,在开机时自动完成 1.2 网络下载 ...
- linux ssh rpm包,RHEL6(CentOS6)中使用源码包编译生成RPM的基本方法:升级OpenSSH篇
RHEL6(CentOS6)中使用源码包编译生成RPM的基本方法:升级OpenSSH篇 具体过程请见代码~ # cp openssh-7.1p1.tar.gz /root/rpmbuild/SOURC ...
- c++调用mysql存储过程_C++中ADO调用MySQL存储过程失败,诡异的语法异常,求解中,附源码...
C++中ADO调用mysql存储过程失败,诡异的语法错误,求解中,附源码 不管怎么调整,死活都出现下面的错误 C++ADO代码我实在找不到什么错误了,难道是MySQL有什么问题,或者有其他需要注意的地 ...
- Openfire4源码部署到eclipse中并编译
Openfire4源码部署到eclipse中并编译 概述 Openfire是众所周知的基于xmpp协议的IM开源服务,所有操作,配置,监控,调试等以B/S方式进行展示,非常的方便管理员进行管理.它的强 ...
- 如何在eclipse中查看源码
在eclipse中查看源码 作为一个java开发的程序员,特别是初学者的java程序员.有时候我们很有必要查看java本身的一些工具类的源码,但是在这之前需要进行一项设置才能顺利的查看得到我们JKD自 ...
最新文章
- 中文扩增子分析视频教程推荐
- mysql slave 配置_【mysql5.6】 数据库主从(Master/Slave)配置记录
- centos安装Flash插件
- MATLAB实战系列(二十九)-头脑风暴优化(BSO)算法求解旅行商问题(TSP)-交叉算子
- Java中对象的深克隆和浅克隆
- 95. Unique Binary Search Trees II
- HDU - 4856 Tunnels(哈密顿路径+状压dp)
- python字符串的特点_python小白之路(特性语法三之字符串)
- Java Date Time 教程-时间测量
- python编辑编程器_用Python制作编辑器
- 在Mac下配置Macaca环境
- 蓝桥杯 ADV-166 算法提高 聪明的美食家 java版
- 40岁,将站到哪里(转)
- Linux基础-获取命令帮助与命令的查找(1)
- UnityShader[1]光照模型
- JAVA对接圆通API
- execute()方法
- peewee-async使用描述
- 大话西游之Office应用实例系列! 16
- bootStrap 教程 文档