轉自:https://blog.csdn.net/xiaohuoche175/article/details/89304601
在開發過程中會遇到一些頁面的數據是很長時間才進行更新的,不使用緩存的情況下,用戶每次訪問這些都需要先去數據庫中獲取這些數據,當訪問量較大時,這樣獲取數據的方式就會降低頁面的訪問速度,影響效率,這時就可以使用redis將這些數據保存起來,通過判斷是否生成過獲取以及是否更新過數據來生成新的緩存數據
具體操作如下:
在settings.py里添加緩存設置
# Django的緩存配置
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/9",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
在對應的app中的views.py文件中寫視圖邏輯和緩存操作
from django.core.cache import cache
from goods.models import GoodsType, GoodsSKU, IndexGoodsBanner,IndexPromotionBanner,IndexTypeGoodsBanner
# Create your views here.
class IndexView(View):
def get(self,request):
'''顯示首頁'''
# 嘗試從緩存中獲取數據
context = cache.get('index_page_data')
if context is None:
print('設置緩存')
# 緩存中沒有數據
# 獲取商品的種類信息
types = GoodsType.objects.all()
print(types)
# 獲取首頁輪播商品信息
goods_banners = IndexGoodsBanner.objects.all().order_by('index')
print(goods_banners)
# 獲取首頁促銷活動信息
promotion_banners = IndexPromotionBanner.objects.all().order_by('index')
print(promotion_banners)
# 獲取首頁分類商品展示信息
for type in types: # GoodsType
# 獲取type種類首頁分類商品的圖片展示信息
image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index')
print(image_banners)
# 獲取type種類首頁分類商品的文字展示信息
title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index')
print(title_banners)
# 動態給type增加屬性,分別保存首頁分類商品的圖片展示信息和文字展示信息
type.image_banners = image_banners
type.title_banners = title_banners
context = {'types': types,
'goods_banners': goods_banners,
'promotion_banners': promotion_banners}
# 設置redis緩存
# key value timeout
cache.set('index_page_data', context, 3600)
return render(request, 'index.html', context)
在對應的app中的admin.py文件中添加BaseModelAdmin,作用是當數據發生改變時,將之前的緩存刪除,然后再通過視圖邏輯生成新的緩存
from django.contrib import admin
from django.core.cache import cache
from goods.models import GoodsType,IndexPromotionBanner,IndexGoodsBanner,IndexTypeGoodsBanner
# Register your models here.
class BaseModelAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
'''新增或更新表中的數據時調用'''
super().save_model(request, obj, form, change)
# 發出任務,讓celery worker重新生成首頁靜態頁
from celery_tasks.tasks import generate_static_index_html
generate_static_index_html.delay()
# 清除首頁的緩存數據
cache.delete('index_page_data')
def delete_model(self, request, obj):
'''刪除表中的數據時調用'''
super().delete_model(request, obj)
# 發出任務,讓celery worker重新生成首頁靜態頁
from celery_tasks.tasks import generate_static_index_html
generate_static_index_html.delay()
# 清除首頁的緩存數據
cache.delete('index_page_data')
class GoodsTypeAdmin(BaseModelAdmin):
pass
class IndexGoodsBannerAdmin(BaseModelAdmin):
pass
class IndexTypeGoodsBannerAdmin(BaseModelAdmin):
pass
class IndexPromotionBannerAdmin(BaseModelAdmin):
pass
admin.site.register(GoodsType, GoodsTypeAdmin)
admin.site.register(IndexGoodsBanner, IndexGoodsBannerAdmin)
admin.site.register(IndexTypeGoodsBanner, IndexTypeGoodsBannerAdmin)
admin.site.register(IndexPromotionBanner, IndexPromotionBannerAdmin)
————————————————
版權聲明:本文為CSDN博主「小火skr車」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/xiaohuoche175/article/details/89304601