From 7ffdeef7efea5f6dc69cb4ca53f5f4eb514bf97f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Fri, 18 Jan 2013 14:17:51 +0200 Subject: [PATCH 1/2] add Minify_Cache_XCache - xcache backend based on APC and Memcache files --- min/lib/Minify/Cache/XCache.php | 126 ++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 min/lib/Minify/Cache/XCache.php diff --git a/min/lib/Minify/Cache/XCache.php b/min/lib/Minify/Cache/XCache.php new file mode 100644 index 0000000..f6b9429 --- /dev/null +++ b/min/lib/Minify/Cache/XCache.php @@ -0,0 +1,126 @@ + + * Minify::setCache(new Minify_Cache_XCache()); + * + * + * @package Minify + * @author Elan Ruusamäe + **/ +class Minify_Cache_XCache { + + /** + * Create a Minify_Cache_XCache object, to be passed to + * Minify::setCache(). + * + * @param int $expire seconds until expiration (default = 0 + * meaning the item will not get an expiration date) + */ + public function __construct($expire = 0) + { + $this->_exp = $expire; + } + + /** + * Write data to cache. + * + * @param string $id cache id + * @param string $data + * @return bool success + */ + public function store($id, $data) + { + return xcache_set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", $this->_exp); + } + + /** + * Get the size of a cache entry + * + * @param string $id cache id + * @return int size in bytes + */ + public function getSize($id) + { + if (! $this->_fetch($id)) { + return false; + } + return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) + ? mb_strlen($this->_data, '8bit') + : strlen($this->_data); + } + + /** + * Does a valid cache entry exist? + * + * @param string $id cache id + * @param int $srcMtime mtime of the original source file(s) + * @return bool exists + */ + public function isValid($id, $srcMtime) + { + return ($this->_fetch($id) && ($this->_lm >= $srcMtime)); + } + + /** + * Send the cached content to output + * + * @param string $id cache id + */ + public function display($id) + { + echo $this->_fetch($id) + ? $this->_data + : ''; + } + + /** + * Fetch the cached content + * + * @param string $id cache id + * @return string + */ + public function fetch($id) + { + return $this->_fetch($id) + ? $this->_data + : ''; + } + + private $_exp = null; + + // cache of most recently fetched id + private $_lm = null; + private $_data = null; + private $_id = null; + + /** + * Fetch data and timestamp from xcache, store in instance + * + * @param string $id + * @return bool success + */ + private function _fetch($id) + { + if ($this->_id === $id) { + return true; + } + $ret = xcache_get($id); + if (false === $ret) { + $this->_id = null; + return false; + } + list($this->_lm, $this->_data) = explode('|', $ret, 2); + $this->_id = $id; + return true; + } +} From 04f5873e2849a45a0f3011476a625a26e7a07d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Fri, 18 Jan 2013 14:19:59 +0200 Subject: [PATCH 2/2] upstream uses spaces for formatting --- min/lib/Minify/Cache/XCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/min/lib/Minify/Cache/XCache.php b/min/lib/Minify/Cache/XCache.php index f6b9429..3039ded 100644 --- a/min/lib/Minify/Cache/XCache.php +++ b/min/lib/Minify/Cache/XCache.php @@ -26,7 +26,7 @@ class Minify_Cache_XCache { * @param int $expire seconds until expiration (default = 0 * meaning the item will not get an expiration date) */ - public function __construct($expire = 0) + public function __construct($expire = 0) { $this->_exp = $expire; }