| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfProcessCache |
|---|
| 22 |
{ |
|---|
| 23 |
|
|---|
| 24 |
* Gets the cache engine name or false if no PHP accelerator is enabled. |
|---|
| 25 |
* |
|---|
| 26 |
* @return string The cache engine name |
|---|
| 27 |
*/ |
|---|
| 28 |
public static function cacher() |
|---|
| 29 |
{ |
|---|
| 30 |
static $cacher = null; |
|---|
| 31 |
|
|---|
| 32 |
if (null === $cacher) |
|---|
| 33 |
{ |
|---|
| 34 |
if (!sfConfig::get('sf_use_process_cache')) |
|---|
| 35 |
{ |
|---|
| 36 |
$cacher = false; |
|---|
| 37 |
} |
|---|
| 38 |
elseif (function_exists('apc_store')) |
|---|
| 39 |
{ |
|---|
| 40 |
$cacher = 'apc'; |
|---|
| 41 |
} |
|---|
| 42 |
elseif (function_exists('xcache_set')) |
|---|
| 43 |
{ |
|---|
| 44 |
$cacher = 'xcache'; |
|---|
| 45 |
} |
|---|
| 46 |
elseif (function_exists('eaccelerator_put')) |
|---|
| 47 |
{ |
|---|
| 48 |
$cacher = 'eaccelerator'; |
|---|
| 49 |
} |
|---|
| 50 |
else |
|---|
| 51 |
{ |
|---|
| 52 |
$cacher = false; |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
return $cacher; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
* Gets the prefix to use for all key name. |
|---|
| 61 |
* |
|---|
| 62 |
* @return string The prefix string |
|---|
| 63 |
*/ |
|---|
| 64 |
public static function getPrefix() |
|---|
| 65 |
{ |
|---|
| 66 |
static $prefix = null; |
|---|
| 67 |
|
|---|
| 68 |
if (!$prefix) |
|---|
| 69 |
{ |
|---|
| 70 |
$prefix = md5(sfConfig::get('sf_app_dir')).'_'; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
return $prefix; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
* Sets a value in the cache for the specified key. |
|---|
| 78 |
* |
|---|
| 79 |
* @param string The key name |
|---|
| 80 |
* @param string The content to put in cache |
|---|
| 81 |
* @param int The life time to keep the content in the cache in seconds |
|---|
| 82 |
* |
|---|
| 83 |
* @return boolean true if ok |
|---|
| 84 |
*/ |
|---|
| 85 |
public static function set($key, $value, $lifeTime = 0) |
|---|
| 86 |
{ |
|---|
| 87 |
switch (self::cacher()) |
|---|
| 88 |
{ |
|---|
| 89 |
case 'apc': |
|---|
| 90 |
return apc_store(self::getPrefix().$key, $value, $lifeTime); |
|---|
| 91 |
case 'xcache': |
|---|
| 92 |
return xcache_set(self::getPrefix().$key, $value, $lifeTime); |
|---|
| 93 |
case 'eaccelerator': |
|---|
| 94 |
return eaccelerator_put(self::getPrefix().$key, serialize($value), $lifeTime); |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
return false; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
* Gets a value in the cache for the specified key. |
|---|
| 102 |
* |
|---|
| 103 |
* @param string The key name |
|---|
| 104 |
* |
|---|
| 105 |
* @return mixed The content associated with the key or null if the key does not exist |
|---|
| 106 |
*/ |
|---|
| 107 |
public static function get($key) |
|---|
| 108 |
{ |
|---|
| 109 |
switch (self::cacher()) |
|---|
| 110 |
{ |
|---|
| 111 |
case 'apc': |
|---|
| 112 |
$value = apc_fetch(self::getPrefix().$key); |
|---|
| 113 |
return false === $value ? null : $value; |
|---|
| 114 |
case 'xcache': |
|---|
| 115 |
return xcache_isset(self::getPrefix().$key) ? xcache_get(self::getPrefix().$key) : null; |
|---|
| 116 |
case 'eaccelerator': |
|---|
| 117 |
return unserialize(eaccelerator_get(self::getPrefix().$key)); |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
return null; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
* Returns true if a given key exists in the cache, false otherwise. |
|---|
| 125 |
* |
|---|
| 126 |
* @param string The key name |
|---|
| 127 |
* |
|---|
| 128 |
* @return boolean true if the key exists, false otherwise |
|---|
| 129 |
*/ |
|---|
| 130 |
public static function has($key) |
|---|
| 131 |
{ |
|---|
| 132 |
switch (self::cacher()) |
|---|
| 133 |
{ |
|---|
| 134 |
case 'apc': |
|---|
| 135 |
return false === apc_fetch(self::getPrefix().$key) ? false : true; |
|---|
| 136 |
case 'xcache': |
|---|
| 137 |
return xcache_isset(self::getPrefix().$key); |
|---|
| 138 |
case 'eaccelerator': |
|---|
| 139 |
return null === eaccelerator_get(self::getPrefix().$key) ? false : true; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
return false; |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
* Clears the cache. |
|---|
| 147 |
* |
|---|
| 148 |
* @return boolean true if ok, false otherwise |
|---|
| 149 |
*/ |
|---|
| 150 |
public static function clear() |
|---|
| 151 |
{ |
|---|
| 152 |
switch (self::cacher()) |
|---|
| 153 |
{ |
|---|
| 154 |
case 'apc': |
|---|
| 155 |
return apc_clear_cache('user'); |
|---|
| 156 |
case 'xcache': |
|---|
| 157 |
for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) |
|---|
| 158 |
{ |
|---|
| 159 |
if (!xcache_clear_cache(XC_TYPE_VAR, $i)) |
|---|
| 160 |
{ |
|---|
| 161 |
return false; |
|---|
| 162 |
} |
|---|
| 163 |
} |
|---|
| 164 |
return true; |
|---|
| 165 |
case 'eaccelerator': |
|---|
| 166 |
$infos = eaccelerator_list_keys(); |
|---|
| 167 |
if (is_array($infos)) |
|---|
| 168 |
{ |
|---|
| 169 |
foreach ($infos as $info) |
|---|
| 170 |
{ |
|---|
| 171 |
|
|---|
| 172 |
$key = 0 === strpos($info['name'], ':') ? substr($info['name'], 1) : $info['name']; |
|---|
| 173 |
if (!eaccelerator_rm($key)) |
|---|
| 174 |
{ |
|---|
| 175 |
return false; |
|---|
| 176 |
} |
|---|
| 177 |
} |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
return true; |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
return false; |
|---|
| 184 |
} |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|