StaticCacheManager

静态管理

 

前置参数:

self.staticconfig = StaticSettings.getsolo()
manager = StaticCacheManager(self.staticconfig.highsafe,
                                     self.staticconfig.staticitem)

生成静态:

manager.create(filename, content)

获取静态:

manager.get(filename)

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

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

class YourAppDetailView(DetailView):
    model = YourApp
    templatename = 'yourapp/yourappdetail.html'
    contextobjectname = 'yourapp'
    def get(self, request, *args, **kwargs):
        filename = request.pathinfo
        self.staticconfig = StaticSettings.getsolo()
        manager = StaticCacheManager(self.staticconfig.highsafe,
                                     self.staticconfig.statictag)

    # 获取静态内容

    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</code></pre><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>