zoukankan      html  css  js  c++  java
  • django 缓存的使用

    django缓存

    # 前端混合开发
    	-缓存的位置,通过配置文件来操作(以文件为例)
        -缓存的粒度:
        	-全站缓存
            	中间件
                MIDDLEWARE = [
                    'django.middleware.cache.UpdateCacheMiddleware',
                    ...
                    'django.middleware.cache.FetchFromCacheMiddleware',
                ]
                CACHE_MIDDLEWARE_SECONDS=10  # 全站缓存时间
            -单页面缓存
            	在视图函数上加装饰器
                from django.views.decorators.cache import cache_page
                @cache_page(5)  # 缓存5s钟
                def test_cache(request):
                    import time
                    ctime=time.time()
                    return render(request,'index.html',context={'ctime':ctime})
            	
            -页面局部缓存
            	{% load cache %}
                {% cache 5 'name' %}  # 5表示5s钟,name是唯一key值
                 {{ ctime }}
                {% endcache %}
            	
        
    # 前后端分离缓存的使用
    	- 如何使用
            from django.core.cache import cache
            
            cache.set('key',value可以是任意数据类型)
            cache.get('key')
            cache.remove('key')
            
        -应用场景:
            将数据缓存,每次使用,先查找缓存,没有查数据库,再添加到缓存中。
    
  • 相关阅读:
    Serveral effective linux commands
    Amber learning note A8: Loop Dynamics of the HIV-1 Integrase Core Domain
    amber初学……
    anaconda使用
    python中的一些语法
    python中的OOP
    python中的模块
    将python程序打包成exe
    python-执行过程
    python基础
  • 原文地址:https://www.cnblogs.com/pythonwl/p/13368535.html
Copyright © 2011-2022 走看看