Easy Kohana Cache For Variable Dependent Pages


/ Published in: PHP
Save to your folder(s)

Useful for controllers. Put this in your base class for easy caching on variable (GET, POST) dependent pages


Copy this code and paste it in your HTML
  1. protected function checkCacheData($prefix = FALSE, $vars = FALSE) {
  2. if($vars == FALSE) {
  3. $vars = input::instance()->get();
  4. }
  5.  
  6. if($prefix === FALSE) {
  7. $prefix = Router::$method;
  8. }
  9.  
  10. if(empty($vars)) {
  11. $cacheKey = 'empty';
  12. } else {
  13. asort($vars);
  14. $queryString = http_build_query($vars);
  15. $cacheKey = md5($queryString);
  16. }
  17.  
  18. if($cachedData = $this->cache->get(Router::$controller'.'.$prefix.$cacheKey)) {
  19. return $cachedData;
  20. } else {
  21. return FALSE;
  22. }
  23. }
  24.  
  25. protected function setCacheData($data, $prefix = FALSE, $vars = FALSE) {
  26. if($vars == FALSE) {
  27. $vars = input::instance()->get();
  28. }
  29.  
  30. if($prefix === FALSE) {
  31. $prefix = Router::$method;
  32. }
  33.  
  34. if(empty($vars)) {
  35. $cacheKey = 'empty';
  36. } else {
  37. asort($vars);
  38. $cacheKey = md5(http_build_query($vars));
  39. }
  40.  
  41. $this->cache->set(Router::$controller.'.'.$prefix.$cacheKey, $data);
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.