...
|
...
|
@@ -71,6 +71,21 @@ class Cache |
|
|
else {
|
|
|
$result = HoodCache::Memcached($node)->get(self::makeKey($key, $node));
|
|
|
}
|
|
|
|
|
|
// 当接口异常,一级缓存没取到数据的情况
|
|
|
if ($node === 'slave') {
|
|
|
$incrementKey = self::makeKey('_increment_' . $key, 'slave');
|
|
|
$incrementValue = HoodCache::Memcached('slave')->get($incrementKey, 'slave');
|
|
|
// 接口调用失败累计5次之后,回填二级缓存数据到一级缓存, 重置计数值为0
|
|
|
if (is_int($incrementValue) && $incrementValue > 5) {
|
|
|
HoodCache::Memcached('master')->set(self::makeKey($key, 'master'), $result, 300);
|
|
|
HoodCache::Memcached('slave')->set($incrementKey, 0, 3600);
|
|
|
}
|
|
|
// 接口调用失败次数累加
|
|
|
else {
|
|
|
HoodCache::Memcached('slave')->increment($incrementKey, 1, 0, 3600);
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception $e) {
|
|
|
$result = array();
|
|
|
}
|
...
|
...
|
@@ -99,6 +114,32 @@ class Cache |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 累加
|
|
|
*
|
|
|
* @param string $key
|
|
|
* @param int $offset
|
|
|
* @param int $initialValue
|
|
|
* @param int $expiry
|
|
|
* @return boolean
|
|
|
*/
|
|
|
public static function increment($key, $offset = 1, $initialValue = 0, $expiry = 0)
|
|
|
{
|
|
|
return HoodCache::Memcached('master')->increment(self::makeKey($key, 'master'), $offset, $initialValue, $expiry);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 递减
|
|
|
*
|
|
|
* @param string $key
|
|
|
* @param int $offset
|
|
|
* @return boolean
|
|
|
*/
|
|
|
public static function decrement($key, $offset = 1)
|
|
|
{
|
|
|
return HoodCache::Memcached('master')->decrement(self::makeKey($key, 'master'), $offset);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成键名
|
|
|
*
|
|
|
* @param string $key 键名
|
...
|
...
|
|