组团学

高阶应用-缓存

阅读 (292328)

一、原因

对于一个中等流量网站,尽可能地减少开销是很有必要的。缓存数据就是为了保护那些需要很多计算资源的结果,这样就不必在下次重新消耗资源进行计算

二、缓存系统工作原理

对于给定的网址,尝试从缓存中找到网址,如果页面在缓存中,直接返回缓存的页面,如果缓存中没有,一系列操作(比如查数据库)后,保存生成的页面内容到缓存系统以供下一次使用,然后返回生成的页面内容

三、缓存的好处

  • 减轻服务器的压力
  • 提供良好的用户体验

四、缓存的方式

  • 缓存在数据库中

    • 配置settings.py

      # 添加缓存配置 CACHES = { 'default':{ 'BACKEND':'django.core.cache.backends.db.DatabaseCache', 'LOCATION':'my_cache_table', 'OPTIONS':{ 'MAX_ENTRIES':'10', # 缓存数据的最大条数 超出则自动清除 }, 'KEY_PREFIX':'cache', # 缓存前缀 } }
    • 生成缓存所需要的缓存表

      python manage.py createcachetable [table_name]

      python manage.py createcachetable my_cache_table

  • 缓存在内存中

    配置settings.py

    # 配置缓存在内存中 CACHES = { 'default':{ 'BACKEND':'django.core.cache.backends.locmem.LocMemCache', } }
  • 缓存在文件中

    CACHES = { 'default':{ 'BACKEND':'django.core.cache.backends.filebased.FileBasedCache', 'LOCATION':r'C:\Users\xlg\Desktop\cache', } }
  • 缓存在redis中

    • 安装第三方扩展库

      pip install django-redis

    • 配置

      # 缓存在redis中 CACHES = { 'default':{ 'BACKEND':'django_redis.cache.RedisCache', 'LOCATION':'redis://127.0.0.1:6379/1' } }

五、单个视图缓存

cache_page装饰器的参数:

  • time 秒 过期的时间
  • cache缓存配置 默认为default
  • key_prefix 缓存的前缀
from django.views.decorators.cache import cache_page # 缓存的使用 @cache_page(10) def test_filter(req): print('你能看到我几次') u = User.objects.all() return render(req,'show_data.html',{'u':u})

六、底层缓存API

  • 导入

    from django.core.cache import cache

  • 缓存操作

    • 设置

      cache.set(key,value,expires)

    • 获取

      cache.get(key)

    • 删除

      cache.delete(key)

    • 清空

      cache.clear()

  • 示例

    from django.core.cache import cache from django.template import loader def my_cache(req): res = cache.get('user') if not res: u = User.objects.all() tem = loader.get_template('show_user.html') res = tem.render({'u':u}) cache.set('user', res, 60) return HttpResponse(res)

七、缓存案例

settings.py配置

# 缓存在redis中 CACHES = { 'default':{ 'BACKEND': 'django_redis.cache.RedisCache', 'LOCATION': 'redis://127.0.0.1:6379/1', } }

视图函数

def register(req): if req.method == 'GET': return render(req, 'register.html') elif req.method == 'POST': username = req.POST.get('username') userpass = req.POST.get('userpass') email = req.POST.get('email') if cache.get(username): return HttpResponse('该账户已存在') else: # 设置None为永远不过期 cache.set(username,userpass, None) return HttpResponse('成功') def Login(req): if req.method == 'GET': return render(req, 'login.html') elif req.method == 'POST': username = req.POST.get('username') userpass = req.POST.get('userpass') email = req.POST.get('email') password = cache.get(username) if password and password == userpass: return HttpResponse('登录成功') else: return HttpResponse('请输入正确的用户名或密码')

登录/注册模板

<!DOCTYPE html> <html> <head> <title>登录页面</title> <style> .pg_header{ position: fixed; height: 48px; top: 0; left: 0; right: 0; background-color: #2459a2; line-height: 48px; } .pg_header .logo{ margin: 0 auto; float: left; width: 200px; text-align: center; line-height: 48px; font-size: 28px; color: white; } .pg_dl{ left: 400px; display: inline-block; padding: 0 40px; color: white; } .pg_header .pg_dl:hover{ background-color: #2459fb; cursor: pointer; } .left{ margin-top: 20px; width: 400px; display: inline-block; float: left; } .pg_body{ margin-top: 50px; font-size: 18px; display: inline-block; width: 200px; } .pg_body .menu{ width: 800px; padding: 15px; float: left; font-weight: bold; } input[type="text"]{ width: 200px; height: 25px; border-radius: 6px; } input[type="password"]{ width: 200px; height: 25px; border-radius: 6px; } input[type="button"]{ background-color: #555555; border: none; color: white; padding: 12px 29px; text-align: center; text-decoration: none; display: inline-block; font-size: 17px; margin: 4px 2px; cursor: pointer; border-radius: 4px; } input[type="submit"]{ background-color: #555555; border: none; color: white; padding: 12px 29px; text-align: center; text-decoration: none; display: inline-block; font-size: 17px; margin: 4px 2px; cursor: pointer; border-radius: 4px; } .kong{ margin-top: -54px; margin-left: 200px; float:left; padding: 15px; } .img{ width: 50px; height: 40px; } .can{ width: 1220px; height: 40px; line-height: 40px; margin: 0 auto; text-align: center; display: inline-block; } .tian{ color: red; float: right; font-size: 12px; margin-right: -120px; margin-top: -25px; } </style> </head> <body id="i88" style="margin: 0"> <div class = "pg_header"> <a class = "logo">LOGO</a> <a class="pg_dl" id="i77">登录</a> </div> <form name="tijiao" method="post" onsubmit="return check()" action="{% url 'App:login' %}"> {% csrf_token %} <div class="left"></div> <div class="pg_body"> <div class="menu">用户名:</div> <div class="kong"> <input id="text1" type="text" name="username" placeholder="请输入用户名"><span id="div1" class="tian" style="margin-top: 4px">*(为必填)</span> </div> <div class="menu">密码:</div> <div class="kong"> <input id="text2" type="password" name="userpass" onblur="check()"> <span id="div2" class="tian" style="margin-top: 5px">*(为必填)</span> </div> </div> <div class="can"> <input id="i111" type="submit" name="002" value="登 录"> <p style="width: 200px;display: inline-block;"></p> <input id="i222" type="button" name="004" value="取 消"> </div> </form> </body> </html>

路由地址

from django.urls import path from App.views import * urlpatterns = [ path('', main.index, name='index'), path(r'login/', main.Login, name='login'), path(r'register/', main.register, name='register'), ]
需要 登录 才可以提问哦