-
-
Save pbowyer/1486179 to your computer and use it in GitHub Desktop.
sfWebBrowser cached version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Extended version of sfWebBrowser to provide caching functionality | |
*/ | |
class tlCachedBrowser extends sfWebBrowser | |
{ | |
protected $cache_expiration = 86400; // 1 day default cache | |
/** | |
* return current cache expiration in seconds | |
* | |
* @return integer | |
*/ | |
public function getCacheExpiration() | |
{ | |
return $this->cache_expiration; | |
} | |
/** | |
* set cache expiration | |
* | |
* @param integer $seconds | |
* @return integer | |
*/ | |
public function setCacheExpiration($seconds) | |
{ | |
$this->cache_expiration = $seconds; | |
return $this->cache_expiration; | |
} | |
/** | |
* cached get | |
* | |
* @param string The request uri | |
* @param array The request parameters (associative array) | |
* @param array The request headers (associative array) | |
* | |
* @return sfWebBrowser The current browser object | |
*/ | |
public function get($uri, $parameters = array(), $headers = array()) | |
{ | |
$dir = sfConfig::get('sf_cache_dir').'/tlCachedBrowser'; | |
$sfFileCache = new sfFileCache(array('cache_dir' => $dir)); | |
$hashed_uri = md5($uri.'?'.implode(',', $parameters)); | |
if($sfFileCache->has($hashed_uri, 'tlCachedBrowser') && (time() < ($this->cache_expiration + $sfFileCache->getLastModified($hashed_uri, 'tlCachedBrowser')))) | |
{ | |
sfContext::getInstance()->getLogger()->debug('{tlCachedBrowser} (CACHED): '.$uri.'?'.http_build_query($parameters, null, '&')); | |
$this->setResponseCode(200); | |
$this->setResponseHeaders(); | |
$this->setResponseText($sfFileCache->get($hashed_uri, 'tlCachedBrowser')); | |
return $this; | |
} | |
else // set the cache | |
{ | |
$ret = parent::get($uri, $parameters, $headers); | |
sfContext::getInstance()->getLogger()->debug('{tlCachedBrowser} (NOT CACHED): '.$uri.'?'.http_build_query($parameters, null, '&')); | |
$sfFileCache->set($hashed_uri, $this->getResponseText()); | |
return $ret; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment