31. 31
4
OBJECT CACHING
def get_item_by_id(key):
# Check in cache
val = mc.get(key)
# If exists, return it
if val:
return val
# If not, get the val, store it in the cache
val = return session.query(User)
.filter_by(id=key)
.first()
mc.set(key, val)
return val
32. 32
4
OBJECT CACHING
@decorator
def cache(expensive_func, key):
# Check in cache
val = mc.get(key)
# If exists, return it
if val:
return val
# If not, get the val, store it in the cache
val = expensive_func(key)
mc.set(key, val)
return val
47. 47
4
FRAGMENTATION
“In the beginning there was NCSA Mosaic, and Mosaic
called itself NCSA_Mosaic/2.0 (Windows 3.1), and
Mosaic displayed pictures along with text, and there was
much rejoicing.”
History of the browser user-agent string
48. 48
• On a cache miss extra work is done
• The result is stored in the cache
• What if multiple simultaneous
misses?
• Every node tries to do the same
work at the same time and your
4
STAMPEDES
49. 49
• What caching scheme?
• How many extra servers?
• What happens if they fail?
• What will you do to debug it?
4
COMPLEXITY
50. 50
• The ‘how’ of caching:
• What are you caching?
• Where are you caching it?
• How bad is a cache miss?
• How and when are you
invalidating?
4
TAKEAWAYS
51. 51
• The ‘why’ of caching:
• Did it actually get faster?
• Is speed worth extra complexity?
• Don’t guess – measure!
• Always use real-world conditions.
4
TAKEAWAYS
53. 53
• Django documentation on caching:
https://docs.djangoproject.com/en/dev/topics/cache/
• Varnish caching, via Disqus:
http://blog.disqus.com/post/62187806135/scaling-django-to-8-
billion-page-views
• Django cache option comparisons:
http://codysoyland.com/2010/jan/17/evaluating-django-caching-
options/
• More Django-specific tips: http://www.slideshare.net/csky/where-
django-caching-bust-at-the-seams
• Guide to cache-related HTTP headers:
http://www.mobify.com/blog/beginners-guide-to-http-cache-
headers/
4
RESOURCES
54. 54
TraceView
Stop by later tonight to hear more
about AppNeta or TraceView
(make sure to get a sticker!)
April 1: Come drink on our tab
after the Boston Python Meetup
April 11: PyCon! Swing by our
booth (214) for an awesome shirt
Or, just try us out:
THANK YOU!
http://www.appneta.com/products/traceview/
Notes de l'éditeur
Caches that exist at the client or CDN level
Caches that exist within a running application’s code
Caches that exist at the programming language or infrastructure level