好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

转自: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,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

查看更多关于django 使用redis进行页面数据缓存和更新缓存数据的详细内容...

  阅读:26次