码迷,mamicode.com
首页 > 系统相关 > 详细

Django cache

时间:2018-11-11 12:15:24      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:return   end   cat   rgs   ack   shortcut   cli   connect   index   

Django中使用redis

方式一:

utils文件夹下,建立redis_pool.py

import redis
POOL = redis.ConnectionPool(host=‘127.0.0.1‘, port=6379,password=‘1234‘,max_connections=1000)

视图函数中使用:

技术分享图片
import redis
from django.shortcuts import render,HttpResponse
from utils.redis_pool import POOL

def index(request):
    conn = redis.Redis(connection_pool=POOL)
    conn.hset(‘kkk‘,‘age‘,18)

    return HttpResponse(‘设置成功‘)
def order(request):
    conn = redis.Redis(connection_pool=POOL)
    conn.hget(‘kkk‘,‘age‘)

    return HttpResponse(‘获取成功‘)
技术分享图片

方式二:

安装django-redis模块

pip3 install django-redis

setting里配置:

技术分享图片
# redis配置
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": {"max_connections": 100}
            # "PASSWORD": "123",
        }
    }
}
技术分享图片

视图函数:

from django_redis import get_redis_connection
conn = get_redis_connection(‘default‘)
print(conn.hgetall(‘xxx‘))

Django cache

标签:return   end   cat   rgs   ack   shortcut   cli   connect   index   

原文地址:https://www.cnblogs.com/hanbowen/p/9941662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!