Django Redis配置
Django Redis配置
# Django默认不支持redis,需要第三方插件来支持
pipenv install django-redis
pipenv install hiredis # 不是必须
vim settings.py
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://192.168.2.128:6379/1',
'TIMEOUT': 300, # NONE 永不超时
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient', # redis-py 客户端
'PARSER_CLASS': 'redis.connection.HiredisParser', # hiredis是C客户端,性能更高
'PASSWORD': 'mysecret', # 密码,可不设置
'PICKLE_VERSION': -1, # 插件使用PICKLE进行序列化,-1表示最新版本
'SOCKET_CONNECT_TIMEOUT': 5, # 连接超时
'SOCKET_TIMEOUT': 5, # 读写超时
'CONNECTION_POOL_KWARGS': {"max_connections": 100} # 连接池最大连接数
},
'CONNECTION_POOL_CLASS': 'redis.connection.BlockingConnectionPool', # 自定义连接池
}
}
# 支持主从哨兵模式
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': [
'redis://192.168.2.128:6379/1', # master
'redis://192.168.2.128:6380/1', # slave
]
}
}
# cache是django提供的代理对象,根据配置的BACKEND进行操作
from django.core.cache import cache
cache.set('foo', 'value', timeout=25)
cache.set('foo', 'value', timeout=None)
cache.ttl('foo')
cache.persist('foo') # 永不过期
cache.expire("foo", timeout=5)
cache.keys("foo_*")
cache.iter_keys("foo_*")
cache.delete_pattern("foo_*")
cache.set("key", "value1", nx=True) # 实现 SETNX原子操作
# key必须存在,否则报错
cache.incr("key")
cache.incr("key")
# 获得所使用的客户端对象
from django_redis import get_redis_connection
con = get_redis_connection("default")
Django Redis配置的更多相关文章
- Django中配置用Redis做缓存和session
django-redis文档: http://django-redis-chs.readthedocs.io/zh_CN/latest/# 一.在Django中配置 # Django的缓存配置 CAC ...
- Linux中python3,django,redis以及mariab的安装
1. Linux中python3,django,redis以及mariab的安装 2. CentOS下编译安装python3 编译安装python3.6的步骤 1.下载python3源码包 wget ...
- Django - 常用配置
一.logging配置 Django项目常用的logging配置 settings.py LOGGING = { 'version': 1, 'disable_existing_loggers': F ...
- Django redis的使用
一 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...
- Python Virtualenv运行Django环境配置
系统: RHEL6.5 版本说明: Python-3.5.0 Django-1.10.4 virtualenv:为每个项目建立不同的/独立的Python环境,你将为每个项目安装所有需要的软件包到它们各 ...
- redis 配置
一 Redis 支持写的指令 Redis大概的命令如下:set setnx setex appendincr decr rpush lpush rpushx lpushx linsert lset r ...
- redis配置详解
##redis配置详解 # Redis configuration file example. # # Note that in order to read the configuration fil ...
- Redis配置集群一(window)
因为接下来的项目要使用到redis作为我们项目的缓存,所以就花了一天时间研究了一下redis的一些用法,因为没转linux虚拟机,所以就决定先研究一下windows版本的redis集群.主要是redi ...
- Django环境配置
Django安装 #安装最新版本的Django $ pip install django #或者指定安装版本 pip install -v django==1.7.1 项目创建 $ django-ad ...
随机推荐
- nginx 二级目录反向代理
location /faceapi/ { #default_type application/json; # return 200 '{"status":"success ...
- kubectl -n ingress-nginx exec nginx-ingress-controller-78bd49949c-t22bl -- cat /etc/nginx/nginx.conf
kubectl -n ingress-nginx exec nginx-ingress-controller-78bd49949c-t22bl -- cat /etc/nginx/nginx.conf
- postgresql中pg_walfile_name()
pg_walfile_name(lsn pg_lsn):将wal位置转换成文件名 pg_lsn数据类型可以用于存储LSN数据,LSN是指向WAL中某个位置的指针.pg_lsn用XLogRecPtr的形 ...
- centos7.6下的python3.6.9虚拟环境安装elastalert
centos7.6安装python3.6.9+elastalert .编译安装python3..9环境 # 安装依赖 yum -y install zlib-devel bzip2-devel ope ...
- jquery click 与原生 click 的区别
$.click() 触发的事件中没有 event.originalEvent , 不同触发 href="" 中的内容 $[0].click() 可以 <script type ...
- Python 初级 6 循环
一.一个简单的for循环 1 重复做相同的事 for looper in [1, 2, 3, 4, 5]: print("hello") 1 looper的值从1开始, 所以loo ...
- ResponseBody和文件上传
SpringMVC默认使用MappingJacksonHttpMessageConverter对json数据进行转换 <dependency> <groupId>com.fas ...
- [LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LeetCode] 273. Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- 【Linux开发】【CUDA开发】Ubuntu上安装NVIDIA显卡驱动
机型为戴尔Vostro3900 显卡型号为GTX 745 对于Nvidia显卡的驱动,如今很多Linux发行版会默认使用名为nouveau的驱动程序.Nouveau是由第三方为Nvidia开发的一 ...