Zend Framework reverse proxy cache
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 9596 97 98 99 | <?php /** * A Zend_Controller_Front plugin to give HTTP Gateway Cache capabilities to a Zend Framework application * * @author Christian Soronellas <theunic@gmail.com> */ class Zend_Controller_Plugin_HttpGatewayCache extends Zend_Controller_Plugin_Abstract { /** * The cache * @var Zend_Cache_Core */ protected $_cache; /** * A flag for whether the response comes already cached * * @var boolean */ protected $_responseCached = false; /** * Class constructor */ public function __construct(Zend_Cache_Core $cache) { $this->_cache = $cache; } /** * \\\"dispatchLoopStartup\\\" event. Fired when the dispatch loop starts. */ public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) { if (false !== ($data = $this->_cache->load($this->_getCacheKey($request)))) { $this->_process($data); $this->_responseCached = true; $request->isDispatched(true); } } /** * "dispatchLoopShutdown" event. Fired when the dispatch loop has finished. */ public function dispatchLoopShutdown() { // Here we have a full generated response, so replace ESI parts, // cache the response, and go on. if (!$this->_responseCached) { $content = $this->getResponse()->getBody(); // Get the "Cache-control" header and extract the value. This // value will be the cached content lifetime. If no "Cache-control" // header set, the content won't be cached. foreach ($this->getResponse()->getHeaders() as $header) { if ('cache-control' == strtolower($header['name'])) { // Extract the value, cache the content and go sleep! :) $lifetime = (int) sscanf(trim($header['value']), 'max-age=%d'); $this->_cache->save($content, $this->_getCacheKey($request), array(), $lifetime); } } $this->_process($content); } } /** * Generates a cache key using the Request's path info * * @param Zend_Controller_Request_Abstract $request * @return string */ protected function _getCacheKey(Zend_Controller_Request_Abstract $request) { return md5($this->getRequest()->getPathInfo()); } /** * Search for any esi include, and replace by its content */ protected function _process($content) { $matches = array(); if (preg_match_all('#<esi\:include\s+src="(.*?)"\s*/>#', $content, $matches) > 0) { $frontController = Zend_Controller_Front::getInstance(); $frontController->returnResponse(true); foreach ($matches as $match) { $request = new Zend_Controller_Request_Http($match[1] . '?standalone'); $response = $frontController->dispatch($request); $content = str_replace($match[0], $response->getBody(), $content); } $this->getResponse()->setBody($content); $frontController->returnResponse(false); } } } |
Comments
You must login before commenting on a snippet. If you do not have an account, please register.
Snippet description
A reverse proxy cache that undestands ESI standard, written on top of Zend Framework.
Snippet details
- Created:
-
theUniC
- Edited:
-
theUniC
- Revision Id:
- 207
- Edit Message:
- Fixed the \"dispatchLoopShutdown\"
- ZF Version
- 1.10.7
- Tags:
- plugin reverse proxy cache controller
- Comments:
- 0
- Views:
- 1242
- Points:
- 2 (2 votes)