Authored by hf

code review by fei.hong: do modify add the second grade cache check when call ap…

…i error increment five times set data to first grade cache
@@ -71,6 +71,20 @@ class Cache @@ -71,6 +71,20 @@ class Cache
71 else { 71 else {
72 $result = HoodCache::Memcached($node)->get(self::makeKey($key, $node)); 72 $result = HoodCache::Memcached($node)->get(self::makeKey($key, $node));
73 } 73 }
  74 +
  75 + // 当接口异常,一级缓存没取到数据的情况
  76 + if ($node === 'slave') {
  77 + $incrementKey = self::makeKey('_increment_' . $key, 'slave');
  78 + $incrementValue = HoodCache::Memcached('slave')->get($incrementKey, 'slave');
  79 + // 接口调用失败累计5次之后,回填二级缓存数据到一级缓存
  80 + if (is_int($incrementValue) && $incrementValue > 5) {
  81 + HoodCache::Memcached('master')->set(self::makeKey($key, 'master'), $result, 300);
  82 + }
  83 + // 接口调用失败次数累加
  84 + else {
  85 + HoodCache::Memcached('slave')->increment($incrementKey, 1, 0, 3600);
  86 + }
  87 + }
74 } catch (Exception $e) { 88 } catch (Exception $e) {
75 $result = array(); 89 $result = array();
76 } 90 }
@@ -97,6 +111,32 @@ class Cache @@ -97,6 +111,32 @@ class Cache
97 HoodCache::Memcached('slave')->delete(self::makeKey($key, 'slave')); 111 HoodCache::Memcached('slave')->delete(self::makeKey($key, 'slave'));
98 } 112 }
99 } 113 }
  114 +
  115 + /**
  116 + * 累加
  117 + *
  118 + * @param string $key
  119 + * @param int $offset
  120 + * @param int $initialValue
  121 + * @param int $expiry
  122 + * @return boolean
  123 + */
  124 + public static function increment($key, $offset = 1, $initialValue = 0, $expiry = 0)
  125 + {
  126 + return HoodCache::Memcached('master')->increment(self::makeKey($key, 'master'), $offset, $initialValue, $expiry);
  127 + }
  128 +
  129 + /**
  130 + * 递减
  131 + *
  132 + * @param string $key
  133 + * @param int $offset
  134 + * @return boolean
  135 + */
  136 + public static function decrement($key, $offset = 1)
  137 + {
  138 + return HoodCache::Memcached('master')->decrement(self::makeKey($key, 'master'), $offset);
  139 + }
100 140
101 /** 141 /**
102 * 生成键名 142 * 生成键名