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