工程造价信息网站,wordpress免费媒体库管理,天津设计院排名,东莞住房和城乡建设网以下学习 朔宁夫 开发工程师 课程。
缓存可提高程序响应速度。数据库缓存(可过期)/ Redis缓存(Key:Value)/ Memcacheed缓存/ 程序层缓存。
一 缓存
1. 数据库缓存
创建缓存数据表 //
python manage.py createcachetable cache_table
setting //
# 缓存配置
CACHES {def…以下学习 朔宁夫 开发工程师 课程。
缓存可提高程序响应速度。数据库缓存(可过期)/ Redis缓存(Key:Value)/ Memcacheed缓存/ 程序层缓存。
一 缓存
1. 数据库缓存
创建缓存数据表 //
python manage.py createcachetable cache_table
setting //
# 缓存配置
CACHES {default: {BACKEND: django.core.cache.backends.db.DatabaseCache, # 数据库缓存 声明此时django项目使用数据库缓存方式进行缓存LOCATION: cache_table # 指定数据库缓存表的名称}
}
创建新的演示 app // python manage.py startapp cache_app
setting注册、项目链接总路由、app创建子路由。
path(cache/,include(cache_app.urls, namespacecache)),
views //
import timefrom django.core.cache import caches
from django.http import HttpResponse
from django.shortcuts import renderdef db_show(request):# 实例化缓存对象db_cache caches[default]# 判断缓存师傅存在cache_data db_cache.get(data_cache) # 缓存的数据if cache_data:print(命中缓存)return HttpResponse(cache_data)print(没有命中开始查找······)time.sleep(10)data [new algorithm, new application, combining models, data mining]response render(request, advance.html, {data: data})# 设置缓存db_cache.set(data_cache, response.content, timeout30)return response
sub route//
app_name cacheurlpatterns [path(db/, db_show),
]
advance.html //
ul{% for i in data %}li{{ i }}/li{% endfor %}
/ul
2. Redis缓存
# 安装依赖 # pip install django-redis # pip install django-redis-cache
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
or
Releases · tporadowski/redis · GitHub
Windows下安装Redis7.0.8_redis7.0.8解压版安装-CSDN博客
最终找资源安装了github上的7需编译版。安装完成后
1 “服务” 启动 get readyxxxredis 2项
2管理员cmd cd /d D:\mysql\redis redis-server.exe redis.conf
3管理员cmd cd /d D:\mysql\redis redis-cli.exe -h 127.0.0.1 -p 6379 set a 1 get a
4django terminal D:\mysql\redis .\redis-cli.exe ping select 15 keys *。可见我的15号库是空的。
setting //
CACHES {# 数据库缓存default: {BACKEND: django.core.cache.backends.db.DatabaseCache,# 数据库缓存 声明此时django项目使用数据库缓存方式进行缓存LOCATION: cache_table# 指定数据库缓存表的名称},# Redis 缓存 (Redis有[0,15]个库select k, 默认0对应6379)# 安装依赖# pip install django-redis# pip install django-redis-cacheredis_cache: {BACKEND: django_redis.cache.RedisCache,LOCATION: redis://127.0.0.1:6379/2, # 设置为本机 2号库OPTIONS: {CLIENT_CLASS: django_redis.client.DefaultClient}}
}
views //
def redis_show(request):# 实例化缓存对象redis_cache caches[redis_cache]# 判断缓存师傅存在cache_data redis_cache.get(data_cache) # 缓存的数据if cache_data:print(命中缓存)return HttpResponse(cache_data)print(没有命中开始查找······)time.sleep(10)data [new algorithm, new application, combining models, data mining]response render(request, advance.html, {data: data})# 设置缓存redis_cache.set(data_cache, response.content, timeout30)return response
访问页面未过期时终端可见2号库中数据 3. memcached缓存
# memcached
# 一个单独的服务系统有自己的端口号
# pip install python-memcached
mem_cached: {BACKEND: django.core.cache.backends.memcached.MemcachedCach, # 指定缓存使用的引擎LOCATION: 127.0.0.1:11211
}
4. 程序层缓存
django中使用最多的一种方式
① 用装饰器
views //
cache_page(30) # 先看有没有缓存再绝对是否调用函数
def show(request):print(无系统缓存执行对本函数的调用)data [new algorithm, new application, combining models, data mining]return render(request, advance.html, {data: data}) ②放到urls中使用
url //
path(pro_url/, cache_page(30)(url_show))
views //
def url_show(request):print(无系统缓存执行对本函数的调用)data [new algorithm, new application, combining models, data mining]return render(request, advance.html, {data: data})
二 中间件
本质上是一个类。AOP思想。aspect oriented programming, 面向切面编程。