Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Planet_Cache
- {
- /**
- * @var int
- */
- private static $now;
- /**
- * @var bool
- */
- private static $isClientCacheEnabled;
- /**
- * @var bool
- */
- private static $isServerCacheEnabled;
- /**
- * Delete a cache entry
- * @param string $uniqueId
- */
- public static function delete($uniqueId = null)
- {
- if(null === $uniqueId)
- {
- if(!($uniqueId = self::createUniqueId())) return;
- }
- $cache = Zend_Registry::getInstance()->get('cache');
- if(null !== $cache){
- if($cache->test($uniqueId))
- $cache->remove($uniqueId);
- }
- }
- /**
- * create a $uniqueId from the $request object
- * [moduleName]_[controllerName]_[actionName]_param1_value1_param2_value2_
- * eg. config
- * cache.module.default.docs.view.cached
- * @param Zend_Controller_Request_Abstract $request
- */
- public static function createUniqueId($request = null)
- {
- if(null === $request) $request = Zend_Controller_Front::getInstance()->getRequest();
- $cache = Zend_Registry::getInstance()->get('cache');
- if(null !== $cache){
- $params = array();
- if(isset(Zend_Registry::getInstance()->configuration->cache->module->{$request->getModuleName()}->{$request->getControllerName()}->{$request->getActionName()}->params))
- {
- $params = Zend_Registry::getInstance()->configuration->cache->module->{$request->getModuleName()}->{$request->getControllerName()}->{$request->getActionName()}->params->toArray();
- sort($params);
- }
- $uniqueId = $request->getModuleName().'_'.$request->getControllerName().'_'.$request->getActionName();
- if(!empty($params))
- {
- $uniqueId .= '_';
- foreach($params as $param)
- $uniqueId .= $param.'_'.strval($request->getParam($param)).'_';
- }
- return $uniqueId;
- }
- return false;
- }
- /**
- * Determines if the client cache is enabled
- * requires:
- * server cache enabled
- * directive: cache.client.enabled = "true"
- * @return bool
- */
- public static function isClientCacheEnabled()
- {
- if(null !== self::$isClientCacheEnabled) return self::$isClientCacheEnabled;
- if(!self::isServerCacheEnabled()) return self::$isClientCacheEnabled = false;
- return self::$isClientCacheEnabled = isset(Zend_Registry::getInstance()->configuration->cache->client->enabled) && Planet_Utils::Booleanize(Zend_Registry::getInstance()->configuration->cache->client->enabled);
- }
- /**
- * Check if the cache directive exists in config and it's true
- * Check if cache.module.[moduleName].[controllerName].[actionName].cached is TRUE
- *
- * @param Zend_Controller_Request_Abstract $request
- * @return bool
- */
- public static function isServerCacheEnabled($request = null)
- {
- if(null !== self::$isServerCacheEnabled) return self::$isServerCacheEnabled;
- if(null === $request) $request = Zend_Controller_Front::getInstance()->getRequest();
- if(null === $request) throw new Planet_Exception('Request can\'t be NULL');
- /**
- * cache directive must be present in config
- */
- if(!isset(Zend_Registry::getInstance()->configuration->cache))
- {
- return self::$isServerCacheEnabled = false;
- }
- $c = Zend_Registry::getInstance()->configuration->cache; //alias
- /**
- * cache.module.[moduleName].[controllerName].[actionName].cached = "true"
- * MUST be present ...
- */
- if(!isset($c->module->{$request->getModuleName()}->{$request->getControllerName()}->{$request->getActionName()}->cached))
- {
- return self::$isServerCacheEnabled = false;
- }
- /**
- * ... and TRUE
- */
- if(!Planet_Utils::Booleanize($c->module->{$request->getModuleName()}->{$request->getControllerName()}->{$request->getActionName()}->cached))
- {
- return self::$isServerCacheEnabled = false;
- }
- return self::$isServerCacheEnabled = true;
- }
- /**
- * get the cache lifetime
- * @return string
- */
- public static function getLifetime()
- {
- return isset(Zend_Registry::getInstance()->configuration->cache->lifetime) ? Zend_Registry::getInstance()->configuration->cache->lifetime : 0;
- }
- /**
- * Get time()
- */
- public static function getNow()
- {
- if(null !== self::$now) return self::$now;
- return self::$now = time();
- }
- /**
- * Set the standard headers for caching
- */
- public static function setClientHeaders()
- {
- $front = Zend_Controller_Front::getInstance();
- $front->getResponse()->setHeader("Date",gmdate('D M j G:i:s T Y', self::getNow()), true);
- $front->getResponse()->setHeader("Pragma","cache", true);
- $front->getResponse()->setHeader("Cache-Control","private", true);
- $front->getResponse()->setHeader('Expires', gmdate('D, d M Y H:i:s', self::getNow() + self::getLifetime()) . ' GMT', true);
- $front->getResponse()->setHeader("Last-Modified", gmdate('D, d M Y H:i:s', self::getNow()).' GMT', true);
- }
- }
Add Comment
Please, Sign In to add comment