zoukankan      html  css  js  c++  java
  • Django----缓存



    # 全站缓存
    MIDDLEWARE_CLASSES = (
        ‘django.middleware.cache.UpdateCacheMiddleware’, #第一
        'django.middleware.common.CommonMiddleware',
        ‘django.middleware.cache.FetchFromCacheMiddleware’, #最后
    )
     
    # 视图缓存
    from django.views.decorators.cache import cache_page
    import time
      
    @cache_page(15)          #超时时间为15秒
    def index(request):
     t=time.time()      #获取当前时间
     return render(request,"index.html",locals())
     
    # 模板缓存
    {% load cache %}
     <h3 style="color: green">不缓存:-----{{ t }}</h3>
      
    {% cache 2 'name' %} # 存的key
     <h3>缓存:-----:{{ t }}</h3>
    {% endcache %}
    django的缓存能使用redis吗?如果可以的话,如何配置?
    #1.安装 pip install django-redis
    #2.在stting中配置CACHES,可以设置多个缓存,根据名字使用
            CACHES = {
                "default": {
                    "BACKEND": "django_redis.cache.RedisCache",
                    "LOCATION": "redis://127.0.0.1:6379",
                    "OPTIONS": {
                        "CLIENT_CLASS": "django_redis.client.DefaultClient",
                        "CONNECTION_POOL_KWARGS": {"max_connections": 100}
                        # "PASSWORD": "密码",
                    }
                }
            },
            #另添加缓存
            "JERD": { }
    #3.根据名字去连接池中获取连接
            from django_redis import get_redis_connection
            conn = get_redis_connection("default")







  • 相关阅读:
    hihocoder 1049 后序遍历
    hihocoder 1310 岛屿
    Leetcode 63. Unique Paths II
    Leetcode 62. Unique Paths
    Leetcode 70. Climbing Stairs
    poj 3544 Journey with Pigs
    Leetcode 338. Counting Bits
    Leetcode 136. Single Number
    Leetcode 342. Power of Four
    Leetcode 299. Bulls and Cows
  • 原文地址:https://www.cnblogs.com/hanbowen/p/9899620.html
Copyright © 2011-2022 走看看