资讯专栏INFORMATION COLUMN

Python-Django-memcached

Alliot / 1865人阅读

Documentation

Pseudocode

</>复制代码

  1. given a URL, try finding that page in the cache
  2. if the page is in the cache:
  3. return the cached page
  4. else:
  5. generate the page
  6. save the generated page in the cache (for next time)
  7. return the generated page
Install

After installing Memcached itself, you’ll need to install a Memcached binding. There are several Python Memcached bindings available; the two most common are python-memcached and pylibmc.

</>复制代码

  1. $ sudo apt-get install memcached
  2. $ sudo apt-get install python-memcached

To use Memcached with Django:

Set BACKEND to django.core.cache.backends.memcached.MemcachedCache or django.core.cache.backends.memcached.PyLibMCCache (depending on your chosen memcached binding)

Set LOCATION to ip:port values, where ip is the IP address of the Memcached daemon and port is the port on which Memcached is running, or to a unix:path value, where path is the path to a Memcached Unix socket file.

In this example, Memcached is running on localhost (127.0.0.1) port 11211, using the python-memcached binding:

</>复制代码

  1. # settings.py
  2. CACHES = {
  3. "default": {
  4. "BACKEND": "django.core.cache.backends.memcached.MemcachedCache",
  5. "LOCATION": [
  6. "127.0.0.1:11211",
  7. ]
  8. }
  9. }

In this example, Memcached is available through a local Unix socket file /tmp/memcached.sock using the python-memcached binding:

</>复制代码

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

This is the default cache if another is not specified in your settings file. If you want the speed advantages of in-memory caching but don’t have the capability of running Memcached, consider the local-memory cache backend. This cache is per-process (see below) and thread-safe. To use it, set BACKEND to "django.core.cache.backends.locmem.LocMemCache". For example:

</>复制代码

  1. CACHES = {
  2. "default": {
  3. "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
  4. "LOCATION": "unique-snowflake",
  5. }
  6. }

The cache LOCATION is used to identify individual memory stores. If you only have one locmem cache, you can omit the LOCATION; however, if you have more than one local memory cache, you will need to assign a name to at least one of them in order to keep them separate.

Note that each process will have its own private cache instance, which means no cross-process caching is possible. This obviously also means the local memory cache isn’t particularly memory-efficient, so it’s probably not a good choice for production environments. It’s nice for development.

Usage Decorator

A more granular way to use the caching framework is by caching the output of individual views. django.views.decorators.cache defines a cache_page decorator that will automatically cache the view’s response for you. It’s easy to use:

</>复制代码

  1. from django.views.decorators.cache import cache_page
  2. @cache_page(60 * 15)
  3. def my_view(request):
  4. ...

cache_page takes a single argument: the cache timeout, in seconds. In the above example, the result of the my_view() view will be cached for 15 minutes. (Note that we’ve written it as 60 15 for the purpose of readability. 60 15 will be evaluated to 900 – that is, 15 minutes multiplied by 60 seconds per minute.)

The per-view cache, like the per-site cache, is keyed off of the URL. If multiple URLs point at the same view, each URL will be cached separately. Continuing the my_view example, if your URLconf looks like this:

</>复制代码

  1. urlpatterns = [
  2. url(r"^foo/([0-9]{1,2})/$", my_view),
  3. ]

then requests to /foo/1/ and /foo/23/ will be cached separately, as you may expect. But once a particular URL (e.g., /foo/23/) has been requested, subsequent requests to that URL will use the cache.

URLconf

</>复制代码

  1. urlpatterns = [
  2. url(r"^foo/([0-9]{1,2})/$", my_view),
  3. ]

Here’s the same thing, with my_view wrapped in cache_page:

</>复制代码

  1. from django.views.decorators.cache import cache_page
  2. urlpatterns = [
  3. url(r"^foo/([0-9]{1,2})/$", cache_page(60 * 15)(my_view)),
  4. ]
More Choice

</>复制代码

  1. from django.core.cache import cache
  2. def heavy_view(request):
  3. cache_key = "my_heavy_view_cache_key"
  4. cache_time = 900 # time to live in seconds
  5. result = cache.get(cache_key)
  6. if not result:
  7. result = # some calculations here
  8. cache.set(cache_key, result, cache_time)
  9. return result

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/38382.html

相关文章

  • Python-Django-memcached

    Documentation Pseudocode 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 (f...

    firim 评论0 收藏0

发表评论

0条评论

Alliot

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<