加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

django 使用redis进行页面数据缓存和更新缓存数据

发布时间:2020-12-20 10:46:42 所属栏目:Python 来源:网络整理
导读:转自:https://blog.csdn.net/xiaohuoche175/article/details/89304601 在开发过程中会遇到一些页面的数据是很长时间才进行更新的,不使用缓存的情况下,用户每次访问这些都需要先去数据库中获取这些数据,当访问量较大时,这样获取数据的方式就会降低页面的

转自: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 adminfrom django.core.cache import cachefrom goods.models import GoodsType,IndexTypeGoodsBanner# Register your models here. class BaseModelAdmin(admin.ModelAdmin): def save_model(self,request,obj,form,change): ‘‘‘新增或更新表中的数据时调用‘‘‘ super().save_model(request,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,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读