Guest User

Untitled

a guest
Jul 16th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. <?php
  2.  
  3. class Planet_Cache
  4. {
  5.  
  6. /**
  7. * @var int
  8. */
  9. private static $now;
  10.  
  11. /**
  12. * @var bool
  13. */
  14. private static $isClientCacheEnabled;
  15.  
  16. /**
  17. * @var bool
  18. */
  19. private static $isServerCacheEnabled;
  20.  
  21. /**
  22. * Delete a cache entry
  23. * @param string $uniqueId
  24. */
  25. public static function delete($uniqueId = null)
  26. {
  27. if(null === $uniqueId)
  28. {
  29. if(!($uniqueId = self::createUniqueId())) return;
  30. }
  31. $cache = Zend_Registry::getInstance()->get('cache');
  32. if(null !== $cache){
  33. if($cache->test($uniqueId))
  34. $cache->remove($uniqueId);
  35. }
  36. }
  37.  
  38. /**
  39. * create a $uniqueId from the $request object
  40. * [moduleName]_[controllerName]_[actionName]_param1_value1_param2_value2_
  41. * eg. config
  42. * cache.module.default.docs.view.cached
  43. * @param Zend_Controller_Request_Abstract $request
  44. */
  45. public static function createUniqueId($request = null)
  46. {
  47. if(null === $request) $request = Zend_Controller_Front::getInstance()->getRequest();
  48. $cache = Zend_Registry::getInstance()->get('cache');
  49. if(null !== $cache){
  50. $params = array();
  51. if(isset(Zend_Registry::getInstance()->configuration->cache->module->{$request->getModuleName()}->{$request->getControllerName()}->{$request->getActionName()}->params))
  52. {
  53. $params = Zend_Registry::getInstance()->configuration->cache->module->{$request->getModuleName()}->{$request->getControllerName()}->{$request->getActionName()}->params->toArray();
  54. sort($params);
  55. }
  56. $uniqueId = $request->getModuleName().'_'.$request->getControllerName().'_'.$request->getActionName();
  57. if(!empty($params))
  58. {
  59. $uniqueId .= '_';
  60. foreach($params as $param)
  61. $uniqueId .= $param.'_'.strval($request->getParam($param)).'_';
  62. }
  63. return $uniqueId;
  64. }
  65. return false;
  66. }
  67.  
  68. /**
  69. * Determines if the client cache is enabled
  70. * requires:
  71. * server cache enabled
  72. * directive: cache.client.enabled = "true"
  73. * @return bool
  74. */
  75. public static function isClientCacheEnabled()
  76. {
  77. if(null !== self::$isClientCacheEnabled) return self::$isClientCacheEnabled;
  78. if(!self::isServerCacheEnabled()) return self::$isClientCacheEnabled = false;
  79. return self::$isClientCacheEnabled = isset(Zend_Registry::getInstance()->configuration->cache->client->enabled) && Planet_Utils::Booleanize(Zend_Registry::getInstance()->configuration->cache->client->enabled);
  80. }
  81.  
  82. /**
  83. * Check if the cache directive exists in config and it's true
  84. * Check if cache.module.[moduleName].[controllerName].[actionName].cached is TRUE
  85. *
  86. * @param Zend_Controller_Request_Abstract $request
  87. * @return bool
  88. */
  89. public static function isServerCacheEnabled($request = null)
  90. {
  91. if(null !== self::$isServerCacheEnabled) return self::$isServerCacheEnabled;
  92. if(null === $request) $request = Zend_Controller_Front::getInstance()->getRequest();
  93. if(null === $request) throw new Planet_Exception('Request can\'t be NULL');
  94. /**
  95. * cache directive must be present in config
  96. */
  97. if(!isset(Zend_Registry::getInstance()->configuration->cache))
  98. {
  99. return self::$isServerCacheEnabled = false;
  100. }
  101.  
  102. $c = Zend_Registry::getInstance()->configuration->cache; //alias
  103. /**
  104. * cache.module.[moduleName].[controllerName].[actionName].cached = "true"
  105. * MUST be present ...
  106. */
  107. if(!isset($c->module->{$request->getModuleName()}->{$request->getControllerName()}->{$request->getActionName()}->cached))
  108. {
  109. return self::$isServerCacheEnabled = false;
  110. }
  111. /**
  112. * ... and TRUE
  113. */
  114. if(!Planet_Utils::Booleanize($c->module->{$request->getModuleName()}->{$request->getControllerName()}->{$request->getActionName()}->cached))
  115. {
  116. return self::$isServerCacheEnabled = false;
  117. }
  118.  
  119. return self::$isServerCacheEnabled = true;
  120. }
  121.  
  122. /**
  123. * get the cache lifetime
  124. * @return string
  125. */
  126. public static function getLifetime()
  127. {
  128. return isset(Zend_Registry::getInstance()->configuration->cache->lifetime) ? Zend_Registry::getInstance()->configuration->cache->lifetime : 0;
  129. }
  130.  
  131. /**
  132. * Get time()
  133. */
  134. public static function getNow()
  135. {
  136. if(null !== self::$now) return self::$now;
  137. return self::$now = time();
  138. }
  139.  
  140. /**
  141. * Set the standard headers for caching
  142. */
  143. public static function setClientHeaders()
  144. {
  145. $front = Zend_Controller_Front::getInstance();
  146. $front->getResponse()->setHeader("Date",gmdate('D M j G:i:s T Y', self::getNow()), true);
  147. $front->getResponse()->setHeader("Pragma","cache", true);
  148. $front->getResponse()->setHeader("Cache-Control","private", true);
  149. $front->getResponse()->setHeader('Expires', gmdate('D, d M Y H:i:s', self::getNow() + self::getLifetime()) . ' GMT', true);
  150. $front->getResponse()->setHeader("Last-Modified", gmdate('D, d M Y H:i:s', self::getNow()).' GMT', true);
  151. }
  152. }
Add Comment
Please, Sign In to add comment