安装 Dependency
$ pip install django-redis
作为 cache backend 使用配置
为了使用 django-redis , 在你项目下的 setting.py 中添加如下代码:
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 作为连接字符串.
在 Django 中访问 Redis
通过 django-redis 访问 Redis
以搜索 keys 过期为例:
>>> from django.core.cache import cache
>>> cache.set("foo", "value", timeout=25)
>>> cache.ttl("foo")
25
>>> cache.ttl("not-existent")
0
Basic usage
The basic interface is:
>>> cache.set('my_key', 'hello, world!', 30)
>>> cache.get('my_key')
>>> 'hello, world!'
key
should be a str
, and value
can be any picklable Python object.
The timeout
argument is optional and defaults to the timeout
argument of the appropriate backend in the CACHES
setting (explained above). It’s the number of seconds the value should be stored in the cache. Passing in None
for timeout
will cache the value forever. A timeout
of 0
won’t cache the value.
If the object doesn’t exist in the cache, cache.get()
returns None
:
>>> # Wait 30 seconds for 'my_key' to expire...
>>> cache.get('my_key')
None
We advise against storing the literal value None
in the cache, because you won’t be able to distinguish between your stored None
value and a cache miss signified by a return value of None
.
cache.get()
can take a default
argument. This specifies which value to return if the object doesn’t exist in the cache:
>>> cache.get('my_key', 'has expired')
'has expired'
To add a key only if it doesn’t already exist, use the add()
method. It takes the same parameters as set()
, but it will not attempt to update the cache if the key specified is already present:
>>> cache.set('add_key', 'Initial value')
>>> cache.add('add_key', 'New value')
>>> cache.get('add_key')
'Initial value'
原生客户端使用
在某些情况下你的应用需要进入原生 Redis 客户端,以使用一些 django cache 接口没有暴露出来的进阶特性。
为了避免储存新的原生连接所产生的另一份设置,django-redis 提供了方法 get_redis_connection(alias)
使你获得可重用的连接字符串:
$ python manager.py shell
>>> from django_redis import get_redis_connection
>>> con = get_redis_connection("default")
>>> con
<redis.client.StrictRedis object at 0x2dc4510>
>>> con.hset("hash1","key1","value1")
接下来,你就可以像在 redis-cli
中那样,在 Python 中进行 Redis 读写操作。
以下列举了对于在 Redis 中几种不同的数据结构常用的操作命令。
基本操作
命令 | 含义 |
---|---|
del key | 删除 key |
keys * | 所有键 |
TTL key | 查看还有多久过期 |
del name | 删除 name |
select 2 | 表示切换到 2 库,注:Django 的 cache 是存在 1 库的 (上面设置的),进入 redis 的时候默认是 0 库的 |
expire key 100 | 设置过期时间 100 秒 |
EXPIRE name 300 | 设置 name 的 300 秒后自动过期 |
type name | 查看 name 的属性(总共:string, list, hash, set,zset) |
String
操作 | 字符 |
---|---|
set name “wuanger” | 创建 “wuanger”,键为 name |
get key | 获取 key 这个键的值 |
append key aa | 给一个值后面加 aa,类似字符串相加 |
DERC key | 给 key 的值 - 1,仅限数字 |
DERCBY key 4 | 给 key 的值 - 4,仅限数字 |
List
操作 | 含义 |
---|---|
lpush list_name a b c d e f | f e d c b a (依次从左边插入,如果该 list_name 不存在,就创建) |
rpush list_name g | 从右边插入 |
lrange list_name 0 -1 | 显示所有内容(根据下表来的) |
lpop list_name | 从左边删除第一个数,返回值是删除的那个数 |
rpop list_name | 从右边删除一个数,返回值是删除的那个数 |
操作 | 含义 |
---|---|
hset hash_name key1 val1 | hash_name = {key1: val1} |
hmset hash_name key1 val1 key2 val2 | hash_name = {key1: val1, key2: val2} |
hget hash_name key1 | 获取 key1 |
hmget hash_name key1 key2 | 获取多个 key |
hgetall hash_name | 获取所有 key |
hkeys hash_name | 获取所有的 key |
hvals hash_name | 获取所有的 val |
hdel hash_name key | 删除 hash_name 中的 key |
|
Set
操作 | 含义 |
---|---|
sadd set1 val1 val2 | 添加一个 set1 集合里的值 val1,val2 |
srem set1 val1 | 从 set1 中移除 val1 |
smembers set1 | set1 的成员 |
sismember set1 val1 | 查看 val1 是否在 set1 里,是返回 1,否返回 0 |
sinter set1 set2 | 求交集 |
sunion set1 set2 | 求并集 |
sdiff set1 set2 | 求差集 |
zset(有序集合)
操作 | 含义 |
---|---|
zadd zset1 score member | 创建、添加一个 zset1,member 的分数是 score (int) |
zrangebyscore zset1 0 100 withscores | 得出 zset1 中分数 score 在 0-100 的 member,score 从小到大,后面的 withscores 是带 score 输出 |
zrevrangebyscore zset1 100 0 | 从大到小输出 |
zrange zset1 2 4 | 获取 zset1 中序号为 3,4 的 member |
zrank zset1 a | zset1 中成员 a 的序号 |
zrem zset1 a | 移除 zset1 中的 a |
zcount zset1 1 6 | 获取 score 为 1-6 之间的 member 数量 |
Reference
- https://docs.djangoproject.com/en/dev/topics/cache/#the-low-level-cache-api
- https://django-redis-chs.readthedocs.io/zh_CN/latest/
- https://docs.djangoproject.com/en/dev/topics/cache/