我们在程序中集成了很多我们非常好用的工具方法,开发者可以直接使用这些方法来加快开发速度,以集灵活的使用方法可以让我们后期的维护更加简单

StaticCacheManager

静态管理

 

前置参数:

self.static_config = StaticSettings.get_solo()
manager = StaticCacheManager(self.static_config.high_safe,
                                     self.static_config.static_item)

生成静态:

manager.create(filename, content)

获取静态:

manager.get(filename)

通class视图当中可以直接这样用

这样就可以实现在阀值内可以直接将静态返回前端,如果静态则再进行查询。self.static_config.static_tag并不一定非常要使用static_tag的值,具体以在静态缓存中的设置为准。

class YourAppDetailView(DetailView):
    model = YourApp
    template_name = 'yourapp/yourapp_detail.html'
    context_object_name = 'yourapp'
    def get(self, request, *args, **kwargs):
        filename = request.path_info
        self.static_config = StaticSettings.get_solo()
        manager = StaticCacheManager(self.static_config.high_safe,
                                     self.static_config.static_tag)


        # 获取静态内容

        if self.static_config.static_key:
            static_content = manager.get(filename)
            if static_content:
                return HttpResponse(static_content)

            # 执行父类get方法完成常规处理
        response = super().get(request, *args, **kwargs)
        if self.static_config.static_key:
            manager.create(filename, response.render().content.decode('utf-8'))

        return response

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        ...
        return context