zoukankan      html  css  js  c++  java
  • Django积木块11 —— 缓存

    缓存

    Django的缓存可以缓存视图中的函数,模版中的内容,和一些不长变化的数据。

    # setting
    CACHES = {
        'default':{
            'BACKEND':'django.core.cache.backends.locmem.LocMemCache',
            'TIMEOUT':60,
        }
    }
    
    # url
    url(r'^cache$',Cachehandler),
    
    # 视图的缓存
    # view
    from django.views.decorators.cache import cache_page
    @cache_page(60*3)
    def Cachehandler(request):
        return HttpResponse('hello2')
        # return HttpResponse('hello1')
    
    # 模版的缓存
    # html
    {% load cache %}
    <!--缓存  时间  名字-->
    {% cache 120 hello %}
    hello
    {% endcache %}
    # 在视图函数中渲染这个模版就可以了
    
    # 缓存数据
    # view
    from django.core.cache import cache
    def cache2(request):
        cache.set('key1','value',600)
        print cache.get('key1')
        cache.clear()
        return render(request,'cache.html')
    
  • 相关阅读:
    连接ESP32失败:等待包头 #226超时
    题王网
    高可用架构模式——CAP
    高性能负载均衡
    单服务器高性能模式
    虚拟支付
    高性能架构设计——数据库篇
    MySQL
    shell 命令: MD5
    HTTPS
  • 原文地址:https://www.cnblogs.com/NeedEnjoyLife/p/6991280.html
Copyright © 2011-2022 走看看