SlideShare a Scribd company logo
1 of 28
Download to read offline
Django book
Chapter 15 - Caching
Alfred
13年9月3⽇日星期⼆二
Basic
13年9月3⽇日星期⼆二
Why
Where
Memory or FileSystem
13年9月3⽇日星期⼆二
Setting.py
Django Doc
Django Book
13年9月3⽇日星期⼆二
Types
13年9月3⽇日星期⼆二
Memory Cache
• libmemcache http://gijsbert.org/cmemcache/
• python memcache ftp://ftp.tummy.com/pub/python-memcached/
13年9月3⽇日星期⼆二
DB Cache
Create Cache Table
Link Cache Table
13年9月3⽇日星期⼆二
File System Cache
file:// + 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⽇日星期⼆二
[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⽇日星期⼆二
[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⽇日星期⼆二
[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⽇日星期⼆二
Backend Arguments
• Use URI to set arguments
• timeout
• max_entries (locmem, db, fs)
• cull_percentage ( n => 1/n)
13年9月3⽇日星期⼆二
Use Cache
13年9月3⽇日星期⼆二
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⽇日星期⼆二
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⽇日星期⼆二
Cache PerView
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⽇日星期⼆二
Delay the cache
declaration
• The code, my_view can be reused in
another place.
13年9月3⽇日星期⼆二
Cache in template
Basic
Per User
Var setting
13年9月3⽇日星期⼆二
API
13年9月3⽇日星期⼆二
Set Cache
Do not store None
13年9月3⽇日星期⼆二
Notice and Control
13年9月3⽇日星期⼆二
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⽇日星期⼆二
Vary Header
if 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⽇日星期⼆二
Vary Header (cont.)
13年9月3⽇日星期⼆二
Private
A 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⽇日星期⼆二
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⽇日星期⼆二
Disable Cache
13年9月3⽇日星期⼆二
Thanks
13年9月3⽇日星期⼆二

More Related Content

What's hot

Speeding Up The Snail
Speeding Up The SnailSpeeding Up The Snail
Speeding Up The SnailMarcus Deglos
 
2008 MySQL Conference Recap
2008 MySQL Conference Recap2008 MySQL Conference Recap
2008 MySQL Conference RecapChris Barber
 
T3DD12 Caching with Varnish
T3DD12 Caching with VarnishT3DD12 Caching with Varnish
T3DD12 Caching with VarnishAOE
 
Memcached Presentation
Memcached PresentationMemcached Presentation
Memcached PresentationAsif Ali
 
Scaling wordpress for high traffic
Scaling wordpress for high trafficScaling wordpress for high traffic
Scaling wordpress for high trafficRoshan Bhattarai
 
Roshan Bhattarai: Scaling WordPress for high traffic sites
Roshan Bhattarai: Scaling WordPress for high traffic sitesRoshan Bhattarai: Scaling WordPress for high traffic sites
Roshan Bhattarai: Scaling WordPress for high traffic siteswpnepal
 
Using memcache to improve php performance
Using memcache to improve php performanceUsing memcache to improve php performance
Using memcache to improve php performanceSudar Muthu
 
Caching basics in PHP
Caching basics in PHPCaching basics in PHP
Caching basics in PHPAnis Ahmad
 
Super performing websites with TYPO3 and Varnish
Super performing websites with TYPO3 and VarnishSuper performing websites with TYPO3 and Varnish
Super performing websites with TYPO3 and Varnishchristianjul
 
How to reduce database load using Memcache
How to reduce database load using MemcacheHow to reduce database load using Memcache
How to reduce database load using Memcachevaluebound
 
Wordpress hosting canada
Wordpress hosting canadaWordpress hosting canada
Wordpress hosting canadanewfasthost
 
Shared Hosting Hepsia.Co plan and feature
Shared Hosting Hepsia.Co plan and featureShared Hosting Hepsia.Co plan and feature
Shared Hosting Hepsia.Co plan and featureAgus R
 
WordPress Performance & Scalability
WordPress Performance & ScalabilityWordPress Performance & Scalability
WordPress Performance & ScalabilityJoseph Scott
 
Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slidesmkherlakian
 
Understanding Web Cache
Understanding Web CacheUnderstanding Web Cache
Understanding Web CacheProdigyView
 
Speed up your site with Varnish
Speed up your site with VarnishSpeed up your site with Varnish
Speed up your site with VarnishSimon Jones
 
Performance all teh things
Performance all teh thingsPerformance all teh things
Performance all teh thingsMarcus Deglos
 

What's hot (20)

Speeding Up The Snail
Speeding Up The SnailSpeeding Up The Snail
Speeding Up The Snail
 
2008 MySQL Conference Recap
2008 MySQL Conference Recap2008 MySQL Conference Recap
2008 MySQL Conference Recap
 
T3DD12 Caching with Varnish
T3DD12 Caching with VarnishT3DD12 Caching with Varnish
T3DD12 Caching with Varnish
 
Memcached Presentation
Memcached PresentationMemcached Presentation
Memcached Presentation
 
Scaling wordpress for high traffic
Scaling wordpress for high trafficScaling wordpress for high traffic
Scaling wordpress for high traffic
 
Roshan Bhattarai: Scaling WordPress for high traffic sites
Roshan Bhattarai: Scaling WordPress for high traffic sitesRoshan Bhattarai: Scaling WordPress for high traffic sites
Roshan Bhattarai: Scaling WordPress for high traffic sites
 
Using memcache to improve php performance
Using memcache to improve php performanceUsing memcache to improve php performance
Using memcache to improve php performance
 
Caching basics in PHP
Caching basics in PHPCaching basics in PHP
Caching basics in PHP
 
Varnish bof
Varnish bofVarnish bof
Varnish bof
 
Super performing websites with TYPO3 and Varnish
Super performing websites with TYPO3 and VarnishSuper performing websites with TYPO3 and Varnish
Super performing websites with TYPO3 and Varnish
 
Memcache
MemcacheMemcache
Memcache
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
How to reduce database load using Memcache
How to reduce database load using MemcacheHow to reduce database load using Memcache
How to reduce database load using Memcache
 
Wordpress hosting canada
Wordpress hosting canadaWordpress hosting canada
Wordpress hosting canada
 
Shared Hosting Hepsia.Co plan and feature
Shared Hosting Hepsia.Co plan and featureShared Hosting Hepsia.Co plan and feature
Shared Hosting Hepsia.Co plan and feature
 
WordPress Performance & Scalability
WordPress Performance & ScalabilityWordPress Performance & Scalability
WordPress Performance & Scalability
 
Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slides
 
Understanding Web Cache
Understanding Web CacheUnderstanding Web Cache
Understanding Web Cache
 
Speed up your site with Varnish
Speed up your site with VarnishSpeed up your site with Varnish
Speed up your site with Varnish
 
Performance all teh things
Performance all teh thingsPerformance all teh things
Performance all teh things
 

Similar to Django caching guide for performance optimization

Drupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance SitesDrupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance SitesExove
 
Drupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance SitesDrupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance Sitesdrupalcampest
 
Caching and Its Main Types
Caching and Its Main TypesCaching and Its Main Types
Caching and Its Main TypesHTS Hosting
 
cache concepts and varnish-cache
cache concepts and varnish-cachecache concepts and varnish-cache
cache concepts and varnish-cacheMarc Cortinas Val
 
WordPress at Peak Performance (Radio Edit)
WordPress at Peak Performance (Radio Edit)WordPress at Peak Performance (Radio Edit)
WordPress at Peak Performance (Radio Edit)jaredwsmith
 
Oracle UCM: Web Site Performance Tuning
Oracle UCM: Web Site Performance TuningOracle UCM: Web Site Performance Tuning
Oracle UCM: Web Site Performance TuningBrian Huff
 
Scalable Django Architecture
Scalable Django ArchitectureScalable Django Architecture
Scalable Django ArchitectureRami Sayar
 
Drupal Performance - SerBenfiquista.com Case Study
Drupal Performance - SerBenfiquista.com Case StudyDrupal Performance - SerBenfiquista.com Case Study
Drupal Performance - SerBenfiquista.com Case Studyhernanibf
 
Cloud computing 3702
Cloud computing 3702Cloud computing 3702
Cloud computing 3702Jess Coburn
 
lessons from managing a pulsar cluster
 lessons from managing a pulsar cluster lessons from managing a pulsar cluster
lessons from managing a pulsar clusterShivji Kumar Jha
 
MNPHP Scalable Architecture 101 - Feb 3 2011
MNPHP Scalable Architecture 101 - Feb 3 2011MNPHP Scalable Architecture 101 - Feb 3 2011
MNPHP Scalable Architecture 101 - Feb 3 2011Mike Willbanks
 
dh-slides-perf.ppt
dh-slides-perf.pptdh-slides-perf.ppt
dh-slides-perf.ppthackday08
 
dh-slides-perf.ppt
dh-slides-perf.pptdh-slides-perf.ppt
dh-slides-perf.ppthackday08
 
Tumbleweed intro
Tumbleweed introTumbleweed intro
Tumbleweed introRich Helton
 
Caching in Drupal 8
Caching in Drupal 8Caching in Drupal 8
Caching in Drupal 8valuebound
 
Jug Lugano - Scale over the limits
Jug Lugano - Scale over the limitsJug Lugano - Scale over the limits
Jug Lugano - Scale over the limitsDavide Carnevali
 

Similar to Django caching guide for performance optimization (20)

Caching in Kentico 11
Caching in Kentico 11Caching in Kentico 11
Caching in Kentico 11
 
Drupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance SitesDrupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance Sites
 
Drupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance SitesDrupalcamp Estonia - High Performance Sites
Drupalcamp Estonia - High Performance Sites
 
Scaling PHP apps
Scaling PHP appsScaling PHP apps
Scaling PHP apps
 
Caching and Its Main Types
Caching and Its Main TypesCaching and Its Main Types
Caching and Its Main Types
 
cache concepts and varnish-cache
cache concepts and varnish-cachecache concepts and varnish-cache
cache concepts and varnish-cache
 
WordPress at Peak Performance (Radio Edit)
WordPress at Peak Performance (Radio Edit)WordPress at Peak Performance (Radio Edit)
WordPress at Peak Performance (Radio Edit)
 
Caching in drupal
Caching in drupalCaching in drupal
Caching in drupal
 
Oracle UCM: Web Site Performance Tuning
Oracle UCM: Web Site Performance TuningOracle UCM: Web Site Performance Tuning
Oracle UCM: Web Site Performance Tuning
 
Scalable Django Architecture
Scalable Django ArchitectureScalable Django Architecture
Scalable Django Architecture
 
Drupal Performance - SerBenfiquista.com Case Study
Drupal Performance - SerBenfiquista.com Case StudyDrupal Performance - SerBenfiquista.com Case Study
Drupal Performance - SerBenfiquista.com Case Study
 
Cloud computing 3702
Cloud computing 3702Cloud computing 3702
Cloud computing 3702
 
Azure appfabric caching intro and tips
Azure appfabric caching intro and tipsAzure appfabric caching intro and tips
Azure appfabric caching intro and tips
 
lessons from managing a pulsar cluster
 lessons from managing a pulsar cluster lessons from managing a pulsar cluster
lessons from managing a pulsar cluster
 
MNPHP Scalable Architecture 101 - Feb 3 2011
MNPHP Scalable Architecture 101 - Feb 3 2011MNPHP Scalable Architecture 101 - Feb 3 2011
MNPHP Scalable Architecture 101 - Feb 3 2011
 
dh-slides-perf.ppt
dh-slides-perf.pptdh-slides-perf.ppt
dh-slides-perf.ppt
 
dh-slides-perf.ppt
dh-slides-perf.pptdh-slides-perf.ppt
dh-slides-perf.ppt
 
Tumbleweed intro
Tumbleweed introTumbleweed intro
Tumbleweed intro
 
Caching in Drupal 8
Caching in Drupal 8Caching in Drupal 8
Caching in Drupal 8
 
Jug Lugano - Scale over the limits
Jug Lugano - Scale over the limitsJug Lugano - Scale over the limits
Jug Lugano - Scale over the limits
 

Recently uploaded

Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 

Recently uploaded (20)

Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 

Django caching guide for performance optimization

  • 1. Django book Chapter 15 - Caching Alfred 13年9月3⽇日星期⼆二
  • 6. Memory Cache • libmemcache http://gijsbert.org/cmemcache/ • python memcache ftp://ftp.tummy.com/pub/python-memcached/ 13年9月3⽇日星期⼆二
  • 7. DB Cache Create Cache Table Link Cache Table 13年9月3⽇日星期⼆二
  • 8. File System Cache file:// + 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⽇日星期⼆二
  • 9. [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⽇日星期⼆二
  • 10. [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⽇日星期⼆二
  • 11. [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⽇日星期⼆二
  • 12. Backend Arguments • Use URI to set arguments • timeout • max_entries (locmem, db, fs) • cull_percentage ( n => 1/n) 13年9月3⽇日星期⼆二
  • 14. 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⽇日星期⼆二
  • 15. 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⽇日星期⼆二
  • 16. Cache PerView 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⽇日星期⼆二
  • 17. Delay the cache declaration • The code, my_view can be reused in another place. 13年9月3⽇日星期⼆二
  • 18. Cache in template Basic Per User Var setting 13年9月3⽇日星期⼆二
  • 20. Set Cache Do not store None 13年9月3⽇日星期⼆二
  • 22. 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⽇日星期⼆二
  • 23. Vary Header if 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⽇日星期⼆二
  • 25. Private A 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⽇日星期⼆二
  • 26. 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⽇日星期⼆二