cache'owanie danych w php
TRANSCRIPT
Patryk Jar
Agenda
� Definition � Methodologies � Solutions � Tips � What else can I do on my page?
Definition
Cache – a collection of data duplicating or ig ina l va lues s tored e lsewhere on a computer
wikipedia.org
Methodologies
� Whole page (use proxy) � Partial page � Database queries (example) � Re-usable objects (example)
Caching static pages
Caching database queries 1. function cachedSingleRowQuery(!2. $query, array $bind, $expiry, PDO $db, Memcached $cache!3. ) {!4. $key = 'query' . md5($query . implode(',', $bind));!5. $obj = $cache->get($key);!6. if ( ! $obj ) {!7. $result = $db->prepare($query)->execute($bind);!8. $obj = $result->fetchObject();!9. $cache->set($key, $obj, $expiry);!10. }!11. !12. return $obj;!13. }!
Cache Invalidation Strategy
� Time expiration � Update invalidation (DVSA Project ;) ) � Pre-generation (e.g. Facebook)
Application – no cache
…
…
HTTP requests
Application – cached data
… …
cache (e.g. session)
DataStore Interface
REST API (HTTP)
Tester Application - example 1. function get($uuid)!2. {!3. $application = getApplicationFromSession();!4.!5. if (null === $application) {!6. $data = api->getTesterApplication($uuid);!7. $application = new TesterApplication($data);!8. storeInSession($application);!9. }!10. !11. return $application ;!12. }!
Solutions
� APC � http://uk1.php.net/apc/
� Memcached � http://memcached.org/
� HTML5 API (in web browser): � localStorage � sessionStorage
Tips
What else can I do on my page?
� Externalize re-usable JS and CSS � Merge and compress CSS files � Merge and compress JavaScript files � Use CSS Sprites � Use Content Delivery Network
� https://developers.google.com/speed/libraries/
Thank you
� Qs?