+ All Categories
Home > Technology > Django book15 caching

Django book15 caching

Date post: 10-May-2015
Category:
Upload: shih-yi-wei
View: 546 times
Download: 0 times
Share this document with a friend
Popular Tags:
28
Django book Chapter 15 - Caching Alfred 1393星期
Transcript
Page 1: Django book15 caching

Django bookChapter 15 - Caching

Alfred

13年9月3⽇日星期⼆二

Page 2: Django book15 caching

Basic

13年9月3⽇日星期⼆二

Page 3: Django book15 caching

Why

WhereMemory or FileSystem

13年9月3⽇日星期⼆二

Page 4: Django book15 caching

Setting.pyDjango Doc

Django Book

13年9月3⽇日星期⼆二

Page 5: Django book15 caching

Types

13年9月3⽇日星期⼆二

Page 6: Django book15 caching

Memory Cache

• libmemcache http://gijsbert.org/cmemcache/

• python memcache ftp://ftp.tummy.com/pub/python-memcached/

13年9月3⽇日星期⼆二

Page 7: Django book15 caching

DB CacheCreate Cache Table

Link Cache Table

13年9月3⽇日星期⼆二

Page 8: Django book15 caching

File System Cachefile:// + path

The directory path should be absolute – that is, it should start at the root of your filesystem. It doesn’t matter whether you put a slash at the end of the setting.

if your server runs as the user apache, make sure the directory/var/tmp/django_cache exists and is readable and writable by the user apache.

13年9月3⽇日星期⼆二

Page 9: Django book15 caching

[Dev]Local Memory Cache

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.

13年9月3⽇日星期⼆二

Page 10: Django book15 caching

[Dev]Dummy Cache

Django comes with a “dummy” cache that doesn’t actually cache – it just implements the cache interface without doing anything. It s useful if you have a production site that uses heavy-duty caching in various places but a development/test environment where you don’t want to cache and don’t want to have to change your code to special-case the latter.

13年9月3⽇日星期⼆二

Page 11: Django book15 caching

[Dev]Implementation and others

• If you’re building your own backend, you can use the standard cache backends as reference implementations. You’ll find the code in the django/core/cache/backends/ directory of the Django source.

13年9月3⽇日星期⼆二

Page 12: Django book15 caching

Backend Arguments

• Use URI to set arguments

• timeout

• max_entries (locmem, db, fs)

• cull_percentage ( n => 1/n)

13年9月3⽇日星期⼆二

Page 13: Django book15 caching

Use Cache

13年9月3⽇日星期⼆二

Page 14: Django book15 caching

Per-Site Cache• Order of middleware is important.

• CACHE_MIDDLEWARE_SECONDS – The number of seconds each page should be cached.

• CACHE_MIDDLEWARE_KEY_PREFIX – If the cache is shared across multiple sites using the same Django installation, set this to the name of the site, or some other string that is unique to this Django instance, to prevent key collisions. Use an empty string if you don’t care.

• CACHE_MIDDLEWARE_ANONYMOUS_ONLY  -­‐  Cache the only anonymous page. Related with AuthenticationMiddleware.

13年9月3⽇日星期⼆二

Page 15: Django book15 caching

Per-Site Cache ( cont. )• Middleware will modify the header in each HttpResponse

• Sets the Last-­‐Modified header to the current date/time when a fresh (uncached) version of the page is requested.

• Sets the Expires header to the current date/time plus the defined CACHE_MIDDLEWARE_SECONDS.

• Sets the Cache-­‐Control header to give a max age for the page – again, from theCACHE_MIDDLEWARE_SECONDS setting.

django.views.decorators.cache  can  set  life-­‐time  per  view,and  the  priority  is  higher  than  site  if  the  time  is  longer.

13年9月3⽇日星期⼆二

Page 16: Django book15 caching

Cache Per View

By decorator

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.

foo/1/, foo/12/ is separate cache.

13年9月3⽇日星期⼆二

Page 17: Django book15 caching

Delay the cache declaration

• The code, my_view can be reused in another place.

13年9月3⽇日星期⼆二

Page 18: Django book15 caching

Cache in template

Basic

Per User

Var setting

13年9月3⽇日星期⼆二

Page 19: Django book15 caching

API

13年9月3⽇日星期⼆二

Page 20: Django book15 caching

Set Cache

Do not store None

13年9月3⽇日星期⼆二

Page 21: Django book15 caching

Notice and Control

13年9月3⽇日星期⼆二

Page 22: Django book15 caching

Types of upstream cache

• Your ISP may cache certain pages, so if you requested a page from http://example.com/, your ISP would send you the page without having to access example.com directly.

• Your Django Web site may sit behind a proxy cache, such as Squid Web Proxy Cache (http://www.squid-cache.org/), that caches pages for performance. In this case, each request first would be handled by the proxy, and it would be passed to your application only if needed.

• Your Web browser caches pages, too. If a Web page sends out the appropriate headers, your browser will use the local cached copy for subsequent requests to that page, without even contacting the Web page again to see whether it has changed.

13年9月3⽇日星期⼆二

Page 23: Django book15 caching

Vary Headerif this page produces different content based on some difference in request headers – such as a cookie, or a language, or a user-agent – you’ll need to use the Vary header to tell caching mechanisms that the page output depends on those things.

13年9月3⽇日星期⼆二

Page 24: Django book15 caching

Vary Header (cont.)

13年9月3⽇日星期⼆二

Page 25: Django book15 caching

PrivateA user usually faces two kinds of caches: his or her own browser cache (a private cache) and his or her provider’s cache (a public cache). Ex. Back account should be private.

13年9月3⽇日星期⼆二

Page 26: Django book15 caching

Cache Control

• public=True• private=True• no_cache=True• no_transform=True• must_revalidate=True• proxy_revalidate=True• max_age=num_seconds• s_maxage=num_seconds

13年9月3⽇日星期⼆二

Page 27: Django book15 caching

Disable Cache

13年9月3⽇日星期⼆二

Page 28: Django book15 caching

Thanks

13年9月3⽇日星期⼆二


Recommended