欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

全面了解django的缓存机制及使用方法

时间:2021-09-02 10:07:25|栏目:Python代码|点击:

一、缓存目的

1、减小过载

2、避免重复计算

3、提高系统性能

二、如何进行缓存

三、缓存类型

四、缓存粒度分类

五、缓存的设置与使用

示例一:

CACHES = {  
  'default': {
      'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 
      'LOCATION': '127.0.0.1:11211',  
  }
}

示例二:

CACHES = {  
  'default': {    
    'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 
     'LOCATION': 'unix:/tmp/memcached.sock',  
  }
}

示例三:

CACHES = {  <br>  'default': {    <br>    'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',    <br>    'LOCATION': [      <br>      '172.19.26.240:11211',      <br>      '172.19.26.242:11211',    <br>    ]  <br>  }<br>}

示例四:

CACHES = {  
  'default': {    
    'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',    
    'LOCATION': [      
      '172.19.26.240:11211',      
      '172.19.26.242:11212',      
      '172.19.26.244:11213',    
    ]  
  }
}

访问缓存:

>>>from django.core.cache import caches
>>>cache1 = caches[‘myalias']
>>>cache2 = caches[‘myalias']
>>>cache1 is cache2
True



>>>from django.core.cache import cache
>>>cache.set(‘my_key', ‘hello, world', 30)
>>>cache.get(‘my_key')
‘hello, world!'
>>>cache.get(‘my_key')
None
>>>cache.get(‘my_key',‘has expired')
‘has expired'

六、缓存原理

上一篇:python 快速把超大txt文件转存为csv的实例

栏    目:Python代码

下一篇:pytorch Dropout过拟合的操作

本文标题:全面了解django的缓存机制及使用方法

本文地址:http://www.codeinn.net/misctech/171850.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有