| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Minify_Cache_XCache |
| 4 |
* |
| 5 |
* @link http://xcache.lighttpd.net/ |
| 6 |
* @package Minify |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* XCache-based cache class for Minify |
| 11 |
* {@see http://xcache.lighttpd.net/wiki/XcacheApi XCache API} |
| 12 |
* |
| 13 |
* <code> |
| 14 |
* Minify::setCache(new Minify_Cache_XCache()); |
| 15 |
* </code> |
| 16 |
* |
| 17 |
* @package Minify |
| 18 |
* @author Elan Ruusamäe <glen@delfi.ee> |
| 19 |
**/ |
| 20 |
class Minify_Cache_XCache implements Minify_CacheInterface |
| 21 |
{ |
| 22 |
|
| 23 |
/** |
| 24 |
* Create a Minify_Cache_XCache object, to be passed to |
| 25 |
* Minify::setCache(). |
| 26 |
* |
| 27 |
* @param int $expire seconds until expiration (default = 0 |
| 28 |
* meaning the item will not get an expiration date) |
| 29 |
*/ |
| 30 |
public function __construct($expire = 0) |
| 31 |
{ |
| 32 |
$this->_exp = $expire; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Write data to cache. |
| 37 |
* |
| 38 |
* @param string $id cache id |
| 39 |
* @param string $data |
| 40 |
* @return bool success |
| 41 |
*/ |
| 42 |
public function store($id, $data) |
| 43 |
{ |
| 44 |
return xcache_set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", $this->_exp); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get the size of a cache entry |
| 49 |
* |
| 50 |
* @param string $id cache id |
| 51 |
* @return int size in bytes |
| 52 |
*/ |
| 53 |
public function getSize($id) |
| 54 |
{ |
| 55 |
if (! $this->_fetch($id)) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { |
| 60 |
return mb_strlen($this->_data, '8bit'); |
| 61 |
} else { |
| 62 |
return strlen($this->_data); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Does a valid cache entry exist? |
| 68 |
* |
| 69 |
* @param string $id cache id |
| 70 |
* @param int $srcMtime mtime of the original source file(s) |
| 71 |
* @return bool exists |
| 72 |
*/ |
| 73 |
public function isValid($id, $srcMtime) |
| 74 |
{ |
| 75 |
return ($this->_fetch($id) && ($this->_lm >= $srcMtime)); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Send the cached content to output |
| 80 |
* |
| 81 |
* @param string $id cache id |
| 82 |
*/ |
| 83 |
public function display($id) |
| 84 |
{ |
| 85 |
echo $this->_fetch($id) ? $this->_data : ''; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Fetch the cached content |
| 90 |
* |
| 91 |
* @param string $id cache id |
| 92 |
* @return string |
| 93 |
*/ |
| 94 |
public function fetch($id) |
| 95 |
{ |
| 96 |
return $this->_fetch($id) ? $this->_data : ''; |
| 97 |
} |
| 98 |
|
| 99 |
private $_exp = null; |
| 100 |
|
| 101 |
// cache of most recently fetched id |
| 102 |
private $_lm = null; |
| 103 |
private $_data = null; |
| 104 |
private $_id = null; |
| 105 |
|
| 106 |
/** |
| 107 |
* Fetch data and timestamp from xcache, store in instance |
| 108 |
* |
| 109 |
* @param string $id |
| 110 |
* @return bool success |
| 111 |
*/ |
| 112 |
private function _fetch($id) |
| 113 |
{ |
| 114 |
if ($this->_id === $id) { |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
$ret = xcache_get($id); |
| 119 |
if (false === $ret) { |
| 120 |
$this->_id = null; |
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
list($this->_lm, $this->_data) = explode('|', $ret, 2); |
| 126 |
$this->_id = $id; |
| 127 |
|
| 128 |
return true; |
| 129 |
} |
| 130 |
} |
| 131 |
|