From 2f64c4a7ab0a842d35b444d8c0ee9a6ffb0dc6d3 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Mon, 4 Sep 2023 13:56:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90cache=E7=9A=84=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/goods/views2.py | 5 +++++ apps/goods/views_api.py | 2 ++ apps/goods/views_apiview.py | 2 ++ apps/goods/views_viewset.py | 1 + myshop_back/settings.py | 30 +++++++++++++++++++++++++++--- requirements.txt | Bin 406 -> 1460 bytes 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/apps/goods/views2.py b/apps/goods/views2.py index cd5f5a6..f3b9d69 100644 --- a/apps/goods/views2.py +++ b/apps/goods/views2.py @@ -1,13 +1,18 @@ from django.shortcuts import render, redirect, reverse from django.http.response import JsonResponse from django.views.generic.base import View +from django.utils.decorators import method_decorator +from django.views.decorators.cache import cache_page from apps.goods.models import * from apps.goods.forms import * +@method_decorator(cache_page(60 * 1), name='get') # 通过对类的装饰,实现指定方法的缓存 # Create your views here. class GoodCateView(View): + + @method_decorator(cache_page(60*1)) # 直接缓存指定方法 def get(self, request): cates = GoodsCategory.objects.all() print(cates) diff --git a/apps/goods/views_api.py b/apps/goods/views_api.py index 9ee9639..8d6e4f7 100644 --- a/apps/goods/views_api.py +++ b/apps/goods/views_api.py @@ -3,12 +3,14 @@ from rest_framework.response import Response from rest_framework import status from django.http import Http404 +from django.views.decorators.cache import cache_page from apps.goods.models import * from apps.goods.serializers import * @api_view(['GET', 'POST', 'PUT', 'DELETE']) +@cache_page(60*1) # 缓存1分钟 def goods_list(request, *args, **kwargs): if request.method == 'GET': # 获取数据 diff --git a/apps/goods/views_apiview.py b/apps/goods/views_apiview.py index 3e16030..06a2917 100644 --- a/apps/goods/views_apiview.py +++ b/apps/goods/views_apiview.py @@ -1,12 +1,14 @@ from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status +from rest_framework_extensions.cache.decorators import cache_response from apps.goods.models import * from apps.goods.serializers import * class GoodsAPIView(APIView): + @cache_response(timeout=60*1, cache='default') def get(self, request, *args, **kwargs): query_data = Goods.objects.all()[:10] goods_json = GoodsSerializers(query_data, many=True) diff --git a/apps/goods/views_viewset.py b/apps/goods/views_viewset.py index 743a715..c973fb1 100644 --- a/apps/goods/views_viewset.py +++ b/apps/goods/views_viewset.py @@ -3,6 +3,7 @@ from rest_framework import mixins from django_filters.rest_framework import DjangoFilterBackend from django_filters import rest_framework from rest_framework import filters +from rest_framework_extensions.cache.mixins import CacheResponseMixin from apps.goods.models import Goods from apps.goods.serializers import GoodsSerializers diff --git a/myshop_back/settings.py b/myshop_back/settings.py index e1ece81..e3d3e0f 100644 --- a/myshop_back/settings.py +++ b/myshop_back/settings.py @@ -13,7 +13,6 @@ import datetime from pathlib import Path import sys, os - # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -150,7 +149,6 @@ auth.User.groups: (fields.E304) Reverse accessor 'Group.user_set' for 'auth.User """ AUTH_USER_MODEL = 'user.Myuser' - REST_FRAMEWORK = { # 设置全局渲染模板 'DEFAULT_RENDERER_CLASSES': ( @@ -166,7 +164,7 @@ REST_FRAMEWORK = { 'EXCEPTION_HANDLER': 'common.myexception.my_exception_handler', 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema', # 验证设置 - 'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication', ), + 'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication',), 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',), } JWT_AUTH = { @@ -184,3 +182,29 @@ SIMPLE_JWT = { # 跨域配置 CORS_ALLOW_CREDENTIALS = True # 跨域时是否携带cookie CORS_ORIGIN_ALLOW_ALL = True # 指定所有域名都可以访问后端接口 + +# CACHES = { +# 'default': { +# 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', +# 'LOCATION': 'my_cache_table', +# } +# } + +CACHES = { + 'default': { + 'BACKEND': 'django_redis.cache.RedisCache', + 'LOCATION': 'redis://home.rogersun.cn:6379', + 'OPTIONS': { + 'CLIENT_CLASS': 'django_redis.client.DefaultClient', + 'PASSWORD': 'Sxzgx1209', + } + } +} + +SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db' +SESSION_CACHE_ALIAS = 'default' + +REST_FRAMEWORK_EXTENSIONS = { + 'DEFAULT_CACHE_RESPONSE_TIMEOUT': 60, # 缓存时间60秒 + 'DEFAULT_USE_CACHE': 'default', # 默认缓存方式, 对应CACHE中的设置 +} diff --git a/requirements.txt b/requirements.txt index 02c791b30912c15208781d73b2884dbc31702933..1a75b106ad71f428a65b875b856841cde4b9abf9 100644 GIT binary patch literal 1460 zcmaKs!A{#?5QO(!iAOOa4iq?WNsg2v4ia>(1&k3-)p@xY9QI4fayF3%z59F+^T-m}xW>kdmUR`_%0 zg9UxOm#*>NUd2$zbLk3kK#!<7&ro%$6i``ig#QTt{ zNS#U6ww3R>p3Y0;`)|+o)J(1?zE@jM`OPyx=V-TIOuwJQ>63}(xAmR6au#wKo$&lW z7QC;tXG{4AMv^|xuhNZ4g!u5;k<@WwY|6+tWg!QJe8Pjb*f%!oQ+?Q4D(LMt{y}_& zZ)CbZ7YGu06kDYY-RS(Ye$T|x--o!9EAiL%E+?7au3U_9cj?phDqRH%xn1mj>HC}Q zb#*)WB*aorsjNeFd&gqRPwM$#-|S_t=SIDvwpOjt9S^1L*CDhrk;82bUffc9msgT% zc4?u0?3o4k;nZs1Ui!o?-3lGEh& g