完成cache的学习

main
RogerWork 1 year ago
parent f9acb2a120
commit 2f64c4a7ab
  1. 5
      apps/goods/views2.py
  2. 2
      apps/goods/views_api.py
  3. 2
      apps/goods/views_apiview.py
  4. 1
      apps/goods/views_viewset.py
  5. 30
      myshop_back/settings.py
  6. BIN
      requirements.txt

@ -1,13 +1,18 @@
from django.shortcuts import render, redirect, reverse from django.shortcuts import render, redirect, reverse
from django.http.response import JsonResponse from django.http.response import JsonResponse
from django.views.generic.base import View 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.models import *
from apps.goods.forms import * from apps.goods.forms import *
@method_decorator(cache_page(60 * 1), name='get') # 通过对类的装饰,实现指定方法的缓存
# Create your views here. # Create your views here.
class GoodCateView(View): class GoodCateView(View):
@method_decorator(cache_page(60*1)) # 直接缓存指定方法
def get(self, request): def get(self, request):
cates = GoodsCategory.objects.all() cates = GoodsCategory.objects.all()
print(cates) print(cates)

@ -3,12 +3,14 @@ from rest_framework.response import Response
from rest_framework import status from rest_framework import status
from django.http import Http404 from django.http import Http404
from django.views.decorators.cache import cache_page
from apps.goods.models import * from apps.goods.models import *
from apps.goods.serializers import * from apps.goods.serializers import *
@api_view(['GET', 'POST', 'PUT', 'DELETE']) @api_view(['GET', 'POST', 'PUT', 'DELETE'])
@cache_page(60*1) # 缓存1分钟
def goods_list(request, *args, **kwargs): def goods_list(request, *args, **kwargs):
if request.method == 'GET': if request.method == 'GET':
# 获取数据 # 获取数据

@ -1,12 +1,14 @@
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status from rest_framework import status
from rest_framework_extensions.cache.decorators import cache_response
from apps.goods.models import * from apps.goods.models import *
from apps.goods.serializers import * from apps.goods.serializers import *
class GoodsAPIView(APIView): class GoodsAPIView(APIView):
@cache_response(timeout=60*1, cache='default')
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
query_data = Goods.objects.all()[:10] query_data = Goods.objects.all()[:10]
goods_json = GoodsSerializers(query_data, many=True) goods_json = GoodsSerializers(query_data, many=True)

@ -3,6 +3,7 @@ from rest_framework import mixins
from django_filters.rest_framework import DjangoFilterBackend from django_filters.rest_framework import DjangoFilterBackend
from django_filters import rest_framework from django_filters import rest_framework
from rest_framework import filters from rest_framework import filters
from rest_framework_extensions.cache.mixins import CacheResponseMixin
from apps.goods.models import Goods from apps.goods.models import Goods
from apps.goods.serializers import GoodsSerializers from apps.goods.serializers import GoodsSerializers

@ -13,7 +13,6 @@ import datetime
from pathlib import Path from pathlib import Path
import sys, os import sys, os
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent 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' AUTH_USER_MODEL = 'user.Myuser'
REST_FRAMEWORK = { REST_FRAMEWORK = {
# 设置全局渲染模板 # 设置全局渲染模板
'DEFAULT_RENDERER_CLASSES': ( 'DEFAULT_RENDERER_CLASSES': (
@ -166,7 +164,7 @@ REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'common.myexception.my_exception_handler', 'EXCEPTION_HANDLER': 'common.myexception.my_exception_handler',
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema', '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',), 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
} }
JWT_AUTH = { JWT_AUTH = {
@ -184,3 +182,29 @@ SIMPLE_JWT = {
# 跨域配置 # 跨域配置
CORS_ALLOW_CREDENTIALS = True # 跨域时是否携带cookie CORS_ALLOW_CREDENTIALS = True # 跨域时是否携带cookie
CORS_ORIGIN_ALLOW_ALL = True # 指定所有域名都可以访问后端接口 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中的设置
}

Binary file not shown.
Loading…
Cancel
Save