django-redis-cache缓存使用
1. redis安装配置
(1)到redis目录
[root@localhost redis-2.8.17]# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README runtest sentinel.conf tests
BUGS COPYING INSTALL MANIFESTO redis.conf runtest-sentinel src utils
(2) redis 源码包安装
make
(3)修改环境变量
vim /etc/profile
添加以下一行:
export PATH=/qqc_pack/redis-2.8.17/src:$PATH
(4)生效配置
source /etc/profile
(5)启动服务端:redis-server &
客户端:redis-cli
指定配置启动:
redis-server /qqc_pack/redis-2.8.17/redis.conf
(6)查看进程:
[root@localhost ~]# ps -aux|grep redis
root 21692 0.1 0.4 140812 7876 ? Sl 18:29 0:30 redis-server 0.0.0.0:6379
root 21869 0.0 0.2 20200 5192 pts/1 S+ 18:48 0:00 redis-cli
root 22139 0.0 0.0 112724 992 pts/0 R+ 23:34 0:00 grep --color=auto redis
(7) 修改密码,开放host
[root@localhost redis-2.8.17]# vi redis.conf
bind 0.0.0.0
# bind 127.0.0.1
# requirepass foobared
requirepass qqcqqc
(8) 登录
127.0.0.1:6379> auth qqcqqc
OK
2.django中配置,连接redis服务
1、setting中配置:
# redis配置
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://172.29.32.104:6379/0",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100},
"PASSWORD": "qqcqqc",
}
}
}
2、views中使用:
from django_redis import get_redis_connection
def resdis_test(request):
conn = get_redis_connection('default')
all=conn.get("age")
data={"age":all}
print(type(data))
return JsonResponse(data=data, safe=False)
3、cache命令操作:
到manage.py目录
[root@localhost test_pro]# python3 manage.py shell
Python 3.6.4 (default, Nov 25 2019, 21:07:27)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.cache import cache
>>> cache.get("name")
>>> cache.get("103")
{'password': '123456', 'mobile': '22222'}
4、在redis 中查看
127.0.0.1:6379> keys *
1) "name"
2) ":1:103"
3) "age"
127.0.0.1:6379> get ":1:103"
"\x80\x04\x95*\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\bpassword\x94\x8c\x06123456\x94\x8c\x06mobile\x94\x8c\x0522222\x94u."
5.业务场景中使用
def tset_user_cz(request):
user_id = request.GET["user_id"]
# User_info.objects.create(user_id=user_id, name="name", password="123456", remark="庐州", mobile="22222")
info = get_user_cache(user_id)
if not info:
values = User_info.objects.filter(user_id=user_id).values_list("password", "mobile")
data = {"password": values[0][0], "mobile": values[0][1]}
create_user_cache(user_id, data)
return JsonResponse(data=data, safe=False)
return JsonResponse(data=info, safe=False)
6.缓存方法
from django.core.cache import cache
def create_user_cache(user_id, value):
cache.set(user_id, value, timeout=300) # 默认过期时间5分钟
def get_user_cache(user_id):
data = cache.get(user_id)
if not data:
"""查数据库"""
pass
return data
def delete_user_cache(user_id):
cache.delete(user_id)
django-redis-cache缓存使用的更多相关文章
- tp5.0 结合 Redis Cache缓存风暴
方法介绍 1.sadd() 描述:为一个Key添加一个值.如果这个值已经在这个Key中,则返回FALSE. 参数:key value 返回值:成功返回true,失败false 2.delete() ...
- Django学习之十二:Cache 缓存组件
目录 Django Cache 缓存组件 缓存逻辑伪代码 配置缓存源 可配置参数说明 01. Django的默认缓存 02. 基于Redis的django-redis 03. 自定义cache 04. ...
- Django使用redis实现缓存
实现缓存的方式,有多种:本地内存缓存,数据库缓存,文件系统缓存.这里介绍使用Redis数据库进行缓存. 配置 CACHES = { "default": { "BACKE ...
- Django中的缓存(内存,文件,redis)
一.Django中的缓存的几种方法 1)单个视图缓存.时间测试 import time from django.views.decorators.cache import cache_page @ca ...
- Django + Redis实现页面缓存
目的:把从数据库读出的数据存入的redis 中既提高了效率,又减少了对数据库的读写,提高用户体验. 例如: 1,同一页面局部缓存,局部动态 from django.views import View ...
- Django分别使用Memcached和Redis作为缓存的配置(Linux环境)
1 使用memcached 1.1 安装memcached 安装(Linux) sudo apt install memcached 启动 #方式一: service memcached start ...
- django使用redis做缓存
Django 使用 Redis 做缓存 django中应用redis:pip3 install django-redis - 配置 CACHES = { "default": { ...
- Django Cache缓存系统介绍及Memcached使用
在动态网站中,用户每次请求一个页面,服务器都会执行以下操作:查询数据库,渲染模板,执行业务逻辑,最后生成用户可查看的页面. 这会消耗大量的资源,当访问用户量非常大时,就要考虑这个问题了. 缓存就是为了 ...
- Django使用Redis进行缓存详细最全流程
背景和意义服务器数据非经常更新.若每次都从硬盘读取一次,浪费服务器资源.拖慢响应速度.而且数据更新频率较高,服务器负担比较大.若保存到数据库,还需要额外建立一张对应的表存储数据.在Django中建立表 ...
- Django(39)使用redis配置缓存
前言 动态网站的基本权衡是,它们是动态的.每次用户请求页面时,Web服务器都会进行各种计算 - 从数据库查询到模板呈现再到业务逻辑 - 以创建站点访问者看到的页面.从处理开销的角度来看,这比标准的 ...
随机推荐
- 【报错】An error happened during template parsing (template: "class path resource [templates/adminManageCourse.html]")
页面显示: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing t ...
- eclipse以及myeclipse的xml配置文件没有提示的问题解决
对于在使用hibernate时,需要对配置文件进行配置,我们需要引入dtd约束文件.在有网的情况下,可以直接从网上下载,编写xml配置文件的时候,可以提示:在没网的情况下,那么就提示不出来. 下面的方 ...
- windows8.1安装python
python3.8安装后缺少runtime.dll文件,试验了各种方法都不可行,最后安装了Anaconda3,这是一个python配置环境,但是好像Anaconda3只能兼容3.7,python3.8 ...
- python的小介绍
Python简介 龟叔 优美.清晰.简单 主要应用领域: 云计算 WEB开发 科学技术.人工智能 系统运维 爬虫 金融量化分析 图形GUI 游戏 Python发展史 1989年,Guido开始写Pyt ...
- 微信支付公众号支付redirect_uri域名与后台配置不一致,错误码10003
最近弄微信支付,微信支付公众号支付redirect_uri域名与后台配置不一致,错误码10003,最容易出错两个地方 1,appid 对应不到 2,开发者网页授权 填写域名 文章来自http://ww ...
- 如何创建 Qt 插件?
如何创建 Qt 插件? 简单三部曲 定义接口类或接口基类并使用 Q_DECLARE_INTERFACE 宏进行声明 所有的插件都需要继承该基类并继承 QObject(不带界面插件) or QWidge ...
- 【转】sed命令的基本操作
原文链接 sed命令行格式为: sed [-option] ‘command’ 输入文本/文件 常用选项: -n∶取消默认的输出,使用安静(sile ...
- jquery的扩展,及编辑插件的书写格式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- C#windows向窗体传递泛型类
修改窗体代码文件*.cs public partial class FormName<T> : Form partial说明此类还有一半在另外的cs文件中,正是系统替你写好的*.desig ...
- Introduction to Sound Programming with ALSA
ALSA stands for the Advanced Linux Sound Architecture. It consists of a set of kernel drivers, an ap ...