经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Django » 查看文章
全面了解django的缓存机制及使用方法
来源:jb51  时间:2019/7/22 13:24:13  对本文有异议

一、缓存目的

1、减小过载

2、避免重复计算

3、提高系统性能

二、如何进行缓存

三、缓存类型

四、缓存粒度分类

五、缓存的设置与使用

示例一:

  1. CACHES = {
  2.   'default': {
  3.       'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
  4.    'LOCATION': '127.0.0.1:11211',
  5.   }
  6. }

示例二:

  1. CACHES = {
  2. 'default': {
  3. 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
  4. 'LOCATION': 'unix:/tmp/memcached.sock',
  5. }
  6. }

示例三:

  1. 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>}

示例四:

  1. CACHES = {
  2.   'default': {
  3.     'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
  4.     'LOCATION': [
  5.       '172.19.26.240:11211',
  6.       '172.19.26.242:11212',
  7.       '172.19.26.244:11213',
  8.     ]
  9.   }
  10. }

访问缓存:

  1. >>>from django.core.cache import caches
  2. >>>cache1 = caches[‘myalias']
  3. >>>cache2 = caches[‘myalias']
  4. >>>cache1 is cache2
  5. True
  6.  
  7.  
  8.  
  9. >>>from django.core.cache import cache
  10. >>>cache.set(‘my_key', ‘hello, world', 30)
  11. >>>cache.get(‘my_key')
  12. ‘hello, world!'
  13. >>>cache.get(‘my_key')
  14. None
  15. >>>cache.get(‘my_key',‘has expired')
  16. ‘has expired'

六、缓存原理

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号