code review by fei.hong: do modify plugin Cache.php
Showing
1 changed file
with
41 additions
and
0 deletions
@@ -71,6 +71,21 @@ class Cache | @@ -71,6 +71,21 @@ 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次之后,回填二级缓存数据到一级缓存, 重置计数值为0 | ||
80 | + if (is_int($incrementValue) && $incrementValue > 5) { | ||
81 | + HoodCache::Memcached('master')->set(self::makeKey($key, 'master'), $result, 300); | ||
82 | + HoodCache::Memcached('slave')->set($incrementKey, 0, 3600); | ||
83 | + } | ||
84 | + // 接口调用失败次数累加 | ||
85 | + else { | ||
86 | + HoodCache::Memcached('slave')->increment($incrementKey, 1, 0, 3600); | ||
87 | + } | ||
88 | + } | ||
74 | } catch (Exception $e) { | 89 | } catch (Exception $e) { |
75 | $result = array(); | 90 | $result = array(); |
76 | } | 91 | } |
@@ -99,6 +114,32 @@ class Cache | @@ -99,6 +114,32 @@ class Cache | ||
99 | } | 114 | } |
100 | 115 | ||
101 | /** | 116 | /** |
117 | + * 累加 | ||
118 | + * | ||
119 | + * @param string $key | ||
120 | + * @param int $offset | ||
121 | + * @param int $initialValue | ||
122 | + * @param int $expiry | ||
123 | + * @return boolean | ||
124 | + */ | ||
125 | + public static function increment($key, $offset = 1, $initialValue = 0, $expiry = 0) | ||
126 | + { | ||
127 | + return HoodCache::Memcached('master')->increment(self::makeKey($key, 'master'), $offset, $initialValue, $expiry); | ||
128 | + } | ||
129 | + | ||
130 | + /** | ||
131 | + * 递减 | ||
132 | + * | ||
133 | + * @param string $key | ||
134 | + * @param int $offset | ||
135 | + * @return boolean | ||
136 | + */ | ||
137 | + public static function decrement($key, $offset = 1) | ||
138 | + { | ||
139 | + return HoodCache::Memcached('master')->decrement(self::makeKey($key, 'master'), $offset); | ||
140 | + } | ||
141 | + | ||
142 | + /** | ||
102 | * 生成键名 | 143 | * 生成键名 |
103 | * | 144 | * |
104 | * @param string $key 键名 | 145 | * @param string $key 键名 |
-
Please register or login to post a comment