• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Django缓存系统实现过程解析

python 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论

在动态网站中,用户每次请求一个页面,服务器都会执行以下操作:查询数据库,渲染模板,执行业务逻辑,最后生成用户可查看的页面。

这会消耗大量的资源,当访问用户量非常大时,就要考虑这个问题了。

缓存就是为了防止重复计算,把那些消耗了大量资源的结果保存起来,下次访问时就不用再次计算了。缓存的逻辑:

given a URL, try finding that page in the cache
if the page is in the cache:
  return the cached page
else:
  generate the page
  save the generated page in the cache (for next time)
  return the generated page

Django提供了不同粒度的缓存:你可以缓存某个页面,也可以只缓存很难计算、很消耗资源的某个部分,或者直接缓存整个网站。

Django也可以和一些”下游”缓存一起协作,例如Squid和基于浏览器的缓存,这些类型的缓存你不直接控制,但是你可以提供给他们站点哪部分应该被缓存和怎样被缓存(通过HTTP headers)。

设置缓存

在settings中的CACHES中设置缓存,下面是几个可用的缓存选项:

Memcached

Django目前原生支持的最快最有效的缓存系统。要使用Memcached,需要下载Memcached支持库,一般是python-memcached或者pylibmc。

然后设置BACKEND为django.core.cache.backends.memcached.MemcachedCache(使用python-memcached时)或者django.core.cache.backends.memcached.PyLibMCCache(使用pylibmc时)。

设置LOCATION为ip:port或者unix:本文来源[email protected]搞@^&代*@码2网path。例如:

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',
  }
}

当使用pylibmc时,去掉unix:/前缀:

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

还可以在多台机器上运行Memcached进程,程序将会把这组机器当作一个单独的缓存,而不需要在每台机器上复制缓存值:

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',
    ]
  }
}

由于Memcached是基于内存的缓存,数据只存储在内存中,如果服务器死机的话数据会丢失,所以不要把内存缓存作为唯一的数据存储方法。

Database caching

Django也可以把缓存数据存储在数据库中。

CACHES = {
  'default': {
    'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
    'LOCATION': 'my_cache_table',
  }
}

LOCATION为数据库中table的名字,任意起,在数据库中未被使用过即可以。

创建cache table:

python manage.py createcachetable

使用多数据库时,也需要为cache table写Router:

class CacheRouter(object):
  """A router to control all database cache operations"""
 
  def db_for_read(self, model, **hints):
    "All cache read operations go to the replica"
    if model._meta.app_label == 'django_cache':
      return 'cache_replica'
    return None
 
  def db_for_write(self, model, **hints):
    "All cache write operations go to primary"
    if model._meta.app_label == 'django_cache':
      return 'cache_primary'
    return None
 
  def allow_migrate(self, db, app_label, model_name=None, **hints):
    "Only install the cache model on primary"
    if app_label == 'django_cache':
      return db == 'cache_primary'
    return None

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Django缓存系统实现过程解析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址