extended sfWebBrowser with native symfony filecaching

Using sfWebBrowser plugin and native symfony caching with sfFileCache. My purpose for this class is retrieve "longterm" informations via REST.

stáhnout snippet
zobrazení: SyntaxHighlighter | GeSHi | Holý text
<?php
class myBrowser extends sfWebBrowser {

	protected $cache_expiration = 2592000; // 30 days 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 = $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())
  	{
   		$c = new sfFileCache(sfConfig::get('sf_cache_dir'));

   		$encoded_uri = urlencode($uri);

		if($c->has($encoded_uri, 'myBrowser') && (time() < ($this->cache_expiration + $c->lastModified($encoded_uri, 'myBrowser')))) {

			// debug stuff
			if(sfConfig::get('sf_logging_enabled'))
				sfContext::getInstance()->getLogger()->info('myBrowser: (cached!) GET '.$uri);

			$this->setResponseCode(200);
		    $this->setResponseHeaders();
		    $this->setResponseText($c->get($encoded_uri, 'myBrowser'));

		    return $this;
		} else {
  			$ret = parent::get($uri, $parameters, $headers);

  			// debug stuff
  			if(sfConfig::get('sf_logging_enabled'))
				sfContext::getInstance()->getLogger()->info('myBrowser: GET '.$uri);

  			$c->set($encoded_uri, 'myBrowser', $this->getResponseText());

  			return $ret;
		}
  	}
}
?>


Tagy:
php 65 řádků | 2008-05-27 02:08:07 | air.kadlec@seznam.cz