Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Caching: A Guided Tour - 10/12/2010

751 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Caching: A Guided Tour - 10/12/2010

  1. 1. Caching: A Guided Tour Jason Ragsdale - DPUG 10/12/2010
  2. 2. Overview • When to cache? • What to cache? • Page, Fragment and Data • File system, Database and In-Memory • Managing your TTL’s • Clearing / Invalidation
  3. 3. When to cache? • When you are accessing: • Read only / mostly static data • High latency external services • Repeated page segments
  4. 4. When to cache? • Short answer : Always
  5. 5. What to cache? • Database data • Web service data • Dynamically generated content
  6. 6. Page caching • Caching the content of a whole page
  7. 7. Fragment caching • Caching a portion of a page
  8. 8. Data caching • Storing of return data from a service • Database • Web services
  9. 9. File system • PRO: • Fast (depending on hardware) • Persistent data • Can operate on data via cron jobs
  10. 10. File system • CON: • Slow (depending on hardware) • Open file limits of OS • Max inode limits of OS/FS • File lock issues on writes • Does not scale well
  11. 11. Database • PRO: • Fast - Can rely on DB caching • Flexable - Can scale • Persistent data • Can operate on data via cron jobs
  12. 12. Database • CON: • Relying on the DB being available • Caching DB data back into the DB • Flat file DB’s can have file lock issues
  13. 13. Database - Clarification • When speaking of DB caching we are talking about caching of content into a table not the caching mechanisms of the DB itself.
  14. 14. In-Memory • PRO: • Fast: Local memory access is cheap • Distributed access is cheap (network) • Flexible - can scale
  15. 15. In-Memory • CON: • Non-Persistent data (normally) • No security built in
  16. 16. Managing your TTL’s • Same content called from multiple API’s • API 1 want’s data that is no older than 1 hour • API 2 want’s data that is no older than 24 hours
  17. 17. Clearing your cache • Data is no longer valid • Clear to force a new call to the fresh data
  18. 18. Invalidating your cache • What if you do not know all possible keys? • Item ‘123456’ • ‘123456_methodname_param’ • You do not know all ‘methodnames’ or ‘params’
  19. 19. Invalidating your cache • Create a “key” that you can invalidate • Create a cache entry ‘ver_123456’ with the value of the current unixtime • ‘123456_unixtime_methodname_param’ • Now you can invalidate an item just by changing the value of the ‘ver’ record, TTL will take care old ver’s
  20. 20. <?php time • pecl/apc • pecl/memcache • pecl/memcached (huh? two of them?)
  21. 21. APC • Built in cache for php (may have to compile it) • Provides opcode caching • Provides ability to cache data
  22. 22. APC (the dark side) • Cache race/locking/overwriting issues
  23. 23. memcached • Fast • Easy to set up • Easy to configure for distributed caching
  24. 24. cache_object • __call - Where the magic happens
  25. 25. Q&A

×