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