🚀 https://django-redis-chs.readthedocs.io/zh_CN/latest/
Andrey Antukh, niwi@niwi.be 4.7.0
翻译: RaPoSpectre
django-redis 基于 BSD 许可, 是一个使 Django 支持 Redis cache/session 后端的全功能组件.
因为:
版本号像 3.6, 3.7 … 等的是主要发行版本, 会包含向后不兼容的内容. 跟多信息请在升级前阅读升级日志.
版本号像 3.7.0, 3.7.1… 等的是小更新或者 bug 修复版本, 一般只会包含 bug 修复, 没有功能更新.
所有版本的 django-redis 基于 redis-py >= 2.10.0.
安装 django-redis 最简单的方法就是用 pip :
pip install django-redis
为了使用 django-redis , 你应该将你的 django cache setting 改成这样:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
为了更好的互操作性并使连接字符串更加 “标准”, 从 3.8.0 开始 django-redis 使用 redis-py native url notation 作为连接字符串.
URL 格式举例
redis://[:password]@localhost:6379/0 rediss://[:password]@localhost:6379/0 unix://[:password]@/path/to/socket.sock?db=0
支持三种 URL scheme :
指定数据库数字的方法:
在某些环境下连接密码放在 url 是不安全的, 这时你可以忽略密码或者使用方便的 OPTIONS 设置:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"PASSWORD": "mysecret"
}
}
}
注意, 这样配置不会覆盖 uri 中的密码, 所以如果你已经在 uri 中设置了密码, 此设置将被忽略.
Django 默认可以使用任何 cache backend 作为 session backend, 将 django-redis 作为 session 储存后端不用安装任何额外的 backend
SESSION_ENGINE = "django.contrib.sessions.backends.cache" SESSION_CACHE_ALIAS = "default"
django-redis 支持定制基于 Redis 的客户端 ( 参考[可扩展 redis 客户端][] ) 可以用来测试, 例如: 替换默认的客户端为 fakerdis (https://github.com/jamesls/fakeredis) 或者 mockredis (https://github.com/locationlabs/mockredis). 这样做可以不用依赖真的 redis server 做集成测试.
使用 fakeredis 举例:
import fakeredis
CACHES = {
"default": {
"OPTIONS": {
"REDIS_CLIENT_CLASS": "fakeredis.FakeStrictRedis",
}
}
}
如果在测试完毕后想清理所有数据, 在你的 TestCase 中加入如下代码:
def tearDown(self):
from django_redis import get_redis_connection
get_redis_connection("default").flushall()
django-redis 使用 pickle 序列化几乎所有数据.
默认使用最新的 pickle. 如果你想设置其他版本, 使用 PICKLE_VERSION 参数:
CACHES = {
"default": {
# ...
"OPTIONS": {
"PICKLE_VERSION": -1 # Use the latest protocol version
}
}
}
套接字超时设置使用 SOCKET_TIMEOUT 和 SOCKET_CONNECT_TIMEOUT 参数:
CACHES = {
"default": {
# ...
"OPTIONS": {
"SOCKET_CONNECT_TIMEOUT": 5, # in seconds
"SOCKET_TIMEOUT": 5, # in seconds
}
}
}
SOCKET_CONNECT_TIMEOUT : socket 建立连接超时设置
SOCKET_TIMEOUT : 连接建立后的读写操作超时设置
django-redis 支持压缩, 但默认是关闭的. 你可以激活它:
CACHES = {
"default": {
# ...
"OPTIONS": {
"COMPRESSOR": "django_redis.compressors.zlib.ZlibCompressor",
}
}
}
使用 lzma 压缩的例子:
import lzma
CACHES = {
"default": {
# ...
"OPTIONS": {
"COMPRESSOR": "django_redis.compressors.lzma.LzmaCompressor",
}
}
}
在某些情况下, redis 只作为缓存使用, 当它关闭时如果你不希望触发异常. 这是 memcached backend 的默认行为, 你可以使用 django-redis 模拟这种情况.
为了设置这种类似memcached 的行为 ( 忽略连接异常 ), 使用 IGNORE_EXCEPTIONS 参数:
CACHES = {
"default": {
# ...
"OPTIONS": {
"IGNORE_EXCEPTIONS": True,
}
}
}
Also, you can apply the same settings to all configured caches, you can set the global flag in your settings:
当然,你也可以给所有缓存配置相同的忽略行为:
DJANGO_REDIS_IGNORE_EXCEPTIONS = True
当使用 IGNORE_EXCEPTIONS 或者 DJANGO_REDIS_IGNORE_EXCEPTIONS 参数忽略异常时, 你也许会用到 DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS 参数来配置日志异常:
DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS = True
如果你想设置指定的 logger 输出异常, 只需要设置全局变量 DJANGO_REDIS_LOGGER 为 logger 的名称或其路径即可. 如果没有 logger 被设置并且 DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS=True 时此参数将取 name :
DJANGO_REDIS_LOGGER = 'some.specified.logger'
django-redis comes with infinite timeouts support out of the box. And it behaves in same way as django backend contract specifies:
django-redis 支持永不超时设置. 其表现和 django backend 指定的相同:
cache.set("key", "value", timeout=None)
With redis, you can access to ttl of any stored key, for it, django-redis exposes ttl function.
It returns:
在 redis 中, 你可以获取任何 key 的 ttl, django-redis 也支持获取 ttl 的函数:
它返回:
以 keys 搜索过期:
>>> from django.core.cache import cache
>>> cache.set("foo", "value", timeout=25)
>>> cache.ttl("foo")
25
>>> cache.ttl("not-existent")
0
除了简单的 ttl 查询, 你可以使用 persist 或者 expire 方法让一个值永久存在或者指定一个新的过期时间:
使用 persist 的例子:
>>> cache.set("foo", "bar", timeout=22)
>>> cache.ttl("foo")
22
>>> cache.persist("foo")
>>> cache.ttl("foo")
None
使用 expire 的例子:
>>> cache.set("foo", "bar", timeout=22)
>>> cache.expire("foo", timeout=5)
>>> cache.ttl("foo")
5
django-redis 支持 redis 分布式锁. 锁的线程接口是相同的, 因此你可以使用它作为替代.
使用 python 上下文管理器分配锁的例子:
with cache.lock("somekey"):
do_some_thing()
django-redis 支持使用全局通配符的方式来检索或者删除键.
使用通配符搜索的例子
>>> from django.core.cache import cache
>>> cache.keys("foo_*")
["foo_1", "foo_2"]
这个简单的写法将返回所有匹配的值, 但在拥有很大数据量的数据库中这样做并不合适. 在 redis 的 server side cursors 2.8 版及以上, 你可以使用 iter_keys 取代 keys 方法, iter_keys 将返回匹配值的迭代器, 你可以使用迭代器高效的进行遍历.
使用 server side cursors 搜索
>>> from django.core.cache import cache
>>> cache.iter_keys("foo_*")
<generator object algo at 0x7ffa9c2713a8>
>>> next(cache.iter_keys("foo_*"))
"foo_1"
如果要删除键, 使用 delete_pattern 方法, 它和 keys 方法一样也支持全局通配符, 此函数将会返回删掉的键的数量
使用 delete_pattern 的例子
>>> from django.core.cache import cache
>>> cache.delete_pattern("foo_*")
django-redis 有限制的支持一些 Redis 原子操作, 例如 SETNX 和 INCR 命令.
你可以在 set() 方法中加上 nx 参数使用来使用 SETNX 命令
例子:
>>> from django.core.cache import cache
>>> cache.set("key", "value1", nx=True)
True
>>> cache.set("key", "value2", nx=True)
False
>>> cache.get("key")
"value1"
当值 (value) 有合适的键 (key) 时, incr 和 decr 也可以使用 Redis 原子操作
在某些情况下你的应用需要进入原生 Redis 客户端使用一些 django cache 接口没有暴露出来的进阶特性. 为了避免储存新的原生连接所产生的另一份设置, django-redis 提供了方法 get_redis_connection(alias) 使你获得可重用的连接字符串.
>>> from django_redis import get_redis_connection
>>> con = get_redis_connection("default")
>>> con
<redis.client.StrictRedis object at 0x2dc4510>
警告 不是所有的扩展客户端都支持这个特性.
django-redis 使用 redis-py 的连接池接口, 并提供了简单的配置方式. 除此之外, 你可以为 backend 定制化连接池的产生.
redis-py 默认不会关闭连接, 尽可能重用连接
配置默认连接池很简单, 你只需要在 CACHES 中使用 CONNECTION_POOL_KWARGS 设置连接池的最大连接数量即可:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
...
"OPTIONS": {
"CONNECTION_POOL_KWARGS": {"max_connections": 100}
}
}
}
你可以得知连接池已经打开多少连接:
from django.core.cache import get_cache
from django_redis import get_redis_connection
r = get_redis_connection("default") # Use the name you have defined for Redis in settings.CACHES
connection_pool = r.connection_pool
print("Created connections so far: %d" % connection_pool._created_connections)
有时你想使用自己的连接池子类. django-redis 提供了 CONNECTION_POOL_CLASS 来配置连接池子类
myproj/mypool.py
from redis.connection import ConnectionPool
class MyOwnPool(ConnectionPool):
# Just doing nothing, only for example purpose
pass
setting.py
# Omitting all backend declaration boilerplate code.
"OPTIONS": {
"CONNECTION_POOL_CLASS": "myproj.mypool.MyOwnPool",
}
如果之前的方法都不合适, 你可以定制 django-redis 的 connection factory 过程甚至完全重写.
django-redis 默认使用Django setting 中 DJANGO_REDIS_CONNECTION_FACTORY 参数指定的django_redis.pool.ConnectionFactory 类产生连接.
ConnectionFactory 类的部分接口
# Note: Using Python 3 notation for code documentation ;)
class ConnectionFactory(object):
def get_connection_pool(self, params:dict):
# Given connection parameters in the `params` argument,
# return new connection pool.
# It should be overwritten if you want do something
# before/after creating the connection pool, or return your
# own connection pool.
pass
def get_connection(self, params:dict):
# Given connection parameters in the `params` argument,
# return a new connection.
# It should be overwritten if you want to do something
# before/after creating a new connection.
# The default implementation uses `get_connection_pool`
# to obtain a pool and create a new connection in the
# newly obtained pool.
pass
def get_or_create_connection_pool(self, params:dict):
# This is a high layer on top of `get_connection_pool` for
# implementing a cache of created connection pools.
# It should be overwritten if you want change the default
# behavior.
pass
def make_connection_params(self, url:str) -> dict:
# The responsibility of this method is to convert basic connection
# parameters and other settings to fully connection pool ready
# connection parameters.
pass
def connect(self, url:str):
# This is really a public API and entry point for this
# factory class. This encapsulates the main logic of creating
# the previously mentioned `params` using `make_connection_params`
# and creating a new connection using the `get_connection` method.
pass
redis-py (django-redis 使用的 Redis 客户端) 支持的纯净 Python Redis 解析器可以满足大部分普通任务, 但如果你想要性能更好, 可以使用 hiredis
hiredis 是一个用 C 写的 Redis 客户端, 并且他的解析器可以用在 django-redis 中:
"OPTIONS": {
"PARSER_CLASS": "redis.connection.HiredisParser",
}
django_redis 设计的非常灵活和可配置。它提供了可扩展的后端,拥有易扩展的特性.
我们已经说明了默认客户端几乎所有的特点, 但有一个例外: 默认客户端支持主从配置.
如果需要主从设置, 你需要更改 LOCATION 参数:
"LOCATION": [
"redis://127.0.0.1:6379/1",
"redis://127.0.0.1:6378/1",
]
第一个字段代表 master 服务器, 第二个字段代表 slave 服务器.
警告 主从设置没有在生产环境中经过大量测试
此可扩展客户端实现了客户端分片, 它几乎继承了默认客户端的全部功能. 如果需要使用, 请将配置改成这样
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": [
"redis://127.0.0.1:6379/1",
"redis://127.0.0.1:6379/2",
],
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.ShardClient",
}
}
}
警告 分片客户端仍处于试验阶段, 请在生产环境中谨慎使用
我们同时也在尝试解决惊群问题, 更多信息请阅读Wikipedia
和上文讲的一样, 客户端基本继承了默认客户端所有功能, 增加额外的方法以获取/设置键 (keys)
设置举例
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.HerdClient",
}
}
}
一些其他的设置:
客户端在将数据发给服务器之前先会序列化数据. django-redis 默认使用 Python pickle 序列化数据.
如果需要使用 json 序列化数据, 使用 JSONSerializer
设置举例
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"SERIALIZER": "django_redis.serializers.json.JSONSerializer",
}
}
}
使用 MsgPack http://msgpack.org/ 进行序列化 (需要 msgpack-python 库支持)
设置举例
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"SERIALIZER": "django_redis.serializers.msgpack.MSGPackSerializer",
}
}
}
django-redis 默认使用 redis.client.StrictClient 作为 Redis 客户端, 你可以使用其他客户端替代, 比如之前在讲测试时我们用 fakeredis 代替真实客户端.
使用 REDIS_CLIENT_CLASS in the CACHES 来配置你的客户端, 使用 REDIS_CLIENT_KWARGS 提供配置客户端的参数 (可选).
设置举例
CACHES = {
"default": {
"OPTIONS": {
"REDIS_CLIENT_CLASS": "my.module.ClientClass",
"REDIS_CLIENT_KWARGS": {"some_setting": True},
}
}
}