of 35 /35
CACHING FOR CASH - CACHING Scott MacVicar php|works 2008

Caching for Cash: Caching

Embed Size (px)

Text of Caching for Cash: Caching

  • 1.CACHING FOR CASH - CACHINGScott MacVicarphp|works 2008

2. WHY CACHE? Reduce memory usageReduce processing timeReduce network trafcReduce disk access 3. CACHING OPTIONSFlat leRAM diskDatabaseMemcacheAPCScript level 4. WHAT TO CACHE? External resourcesLarge result setsStatic dataSessions 5. FLAT FILE CACHE Write data out to a local leCong les from Database or XMLRemote resources store locallyOutput buffering to local le 6. CACHING FILESPEAR::Cache_LiteZend_Cache in Zend Frameworkvar_export in PHP 7. RAM DISKMount some memory as disk spaceUsed when IO actions cant be avoidedNon resizable on the y 8. LIGHTTPD + PHPLua is a scripting language in lighttpdUse PHP to write out le to diskHave Lua look at the modied time of the le, if within the limit serve else execute PHP 9. MEMCACHE Origins within LivejournalUsed by Facebook, Youtube, Wikipedia and diggCaching daemon, no persistenceStores key / value pairs 10. FACEBOOK EXAMPLE200 dedicated memcache serversEach 16GB quad-core amd643TB memcache data 11. MEMCACHE Slab AllocatorLibevent based (non-blocking)Simple ProtocolServer is just a hash tableNo authentication or self awareness 12. MEMCACHE CLIENTClients provide key and valueResponsible for serialising any valueCompress data 13. SERVER PROTOCOL set/replace/addgetappend/prependincrement/decrementcompare and swap 14. HASHING Key is hashed into a valueModulous then applied which is the number of the serversServer is then picked and storedIf one server drops out you only lose a fraction of the keys 15. MEMCACHE LIMITS Key Size is 250 bytesData Size is 1 megabyte32bit/64bit memory limitNo Replication across cluster 16. MEMCACHE CLEANING UPUses Least Recently Used AlgorithmLooks for the oldest item and removesTTL also applies to key / value pairs 17. SIMPLE USAGEFetch from memcacheIf there returnElse calculate, store in cache and return 18. WHERE TO RUN?Any server worksNo need for matching sizeLow CPU usage 19. MEMCACHE + PHP PECL extension using custom connectionpecl install memcacheProvides MemcachePool and MemcacheINI settings to change hash strategy, function and protocol. 20. MEMCACHE SAMPLE 21. MEMCACHE ACTIONSAdd - only if the key doesnt existSet - will add or update keyReplace - only if the key already exists 22. MEMCACHE TASK Open Terminal in VMWare Image and run sudo service memcached startTry using memcache to set, get and add valuesTry setting a value using the Time To Live parameterAsk questions :-) 23. MEMCACHEDBPersistent key/value storage systemBDB used for persitenceCompatible with Memcache protocol 24. MEMCACHEDB CAVEATSNo expirationIsnt a replacement for memcache 25. APC Provides OPCode caching in PHPShared memory for storageAPC is only on the local serverNo network overhead from TCP/IP 26. APC Input TokenizerParser APC Compiler Store OpcodesExecutor 27. APC SAMPLE 28. CACHE LAYERING Disk /APCMemcache Database Fetch from APC locallyFetch from Memcache, add to APCFinally fetch from the original source and store back in Memcache 29. APC USE CASESShould always use it to cache opcode of lesSmall but frequently accessed thingsYou only have one server 30. APC TASKTry storing classes or arrays in APCUse the Time To LiveAPC supports an array for keys in apc_get, fetch multiple values 31. SQLITELocal node storage of lesystemCache result sets from remote databaseIn memory database 32. DATABASE QUERY CACHINGRely on MySQL to do the query cachingDoesnt quite work though :-(Invalidation of cache happens easily 33. ALTERNATIVE DATABASE CACHING Store the result set from the database using another caching softwareHash query as a key valueExtend your DB layer to add this transparently? 34. CACHING TIPS Pre-heat the cacheUse multiple levels of cacheCache even dynamic data for short timesserialize() is slow 35. QUESTIONS?