Showing
1 changed file
with
154 additions
and
0 deletions
library/Plugin/Cache.php
0 → 100755
1 | +<?php | ||
2 | + | ||
3 | +namespace Plugin; | ||
4 | + | ||
5 | +use Hood\Cache as HoodCache; | ||
6 | + | ||
7 | +/** | ||
8 | + * 数据缓存的操作类 | ||
9 | + * | ||
10 | + * @name Cache | ||
11 | + * @package Plugin | ||
12 | + * @copyright yoho.inc | ||
13 | + * @version 1.0 (2015-10-10 11:23:47) | ||
14 | + * @author fei.hong <fei.hong@yoho.cn> | ||
15 | + */ | ||
16 | +class Cache | ||
17 | +{ | ||
18 | + | ||
19 | + /** | ||
20 | + * 设置缓存 | ||
21 | + * | ||
22 | + * 备注: | ||
23 | + * 采用主从分布式缓存 | ||
24 | + * master 代表主缓存服务器 | ||
25 | + * slave 代表从缓存服务器 | ||
26 | + * | ||
27 | + * 作用: | ||
28 | + * 在后端的服务异常挂掉了并且主服务器缓存(master)的数据时间到期后, | ||
29 | + * 能获取从服务器缓存(slave)的数据,不影响前端页面的正常访问. | ||
30 | + * | ||
31 | + * @param string $key 键名 | ||
32 | + * @param mixed $value 需要缓存的数据 | ||
33 | + * @param int $expire 缓存有效期(单位秒, 0表示永久) | ||
34 | + * @return void | ||
35 | + */ | ||
36 | + public static function set($key, $value, $expire = 300) | ||
37 | + { | ||
38 | + try { | ||
39 | + // WINDOWS | ||
40 | + if (DIRECTORY_SEPARATOR === '\\') { | ||
41 | + HoodCache::Memcache('master')->set(self::makeKey($key, 'master'), $value, $expire); | ||
42 | + HoodCache::Memcache('slave')->set(self::makeKey($key, 'slave'), $value, 86400); // 二级缓存1天 | ||
43 | + } | ||
44 | + // LINUX | ||
45 | + else { | ||
46 | + HoodCache::Memcached('master')->set(self::makeKey($key, 'master'), $value, $expire); | ||
47 | + HoodCache::Memcached('slave')->set(self::makeKey($key, 'slave'), $value, 86400); // 二级缓存1天 | ||
48 | + } | ||
49 | + } catch (Exception $e) { | ||
50 | + // do nothing | ||
51 | + } | ||
52 | + } | ||
53 | + | ||
54 | + /** | ||
55 | + * 获取缓存 | ||
56 | + * | ||
57 | + * @param string $key 键名 | ||
58 | + * @param string $node master代表主服务器, slave代表从服务器 | ||
59 | + * @return mixed | ||
60 | + */ | ||
61 | + public static function get($key, $node = 'master') | ||
62 | + { | ||
63 | + $result = array(); | ||
64 | + | ||
65 | + try { | ||
66 | + // WINDOWS | ||
67 | + if (DIRECTORY_SEPARATOR === '\\') { | ||
68 | + $result = HoodCache::Memcache($node)->get(self::makeKey($key, $node)); | ||
69 | + } | ||
70 | + // LINUX | ||
71 | + else { | ||
72 | + $result = HoodCache::Memcached($node)->get(self::makeKey($key, $node)); | ||
73 | + } | ||
74 | + | ||
75 | + // 当接口异常,一级缓存没取到数据的情况 | ||
76 | + if ($node === 'slave') { | ||
77 | + $incrementKey = self::makeKey('_increment_' . $key, 'slave'); | ||
78 | + $incrementValue = HoodCache::Memcached('slave')->get($incrementKey); | ||
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 | + } | ||
89 | + } catch (Exception $e) { | ||
90 | + $result = array(); | ||
91 | + } | ||
92 | + | ||
93 | + return $result; | ||
94 | + } | ||
95 | + | ||
96 | + /** | ||
97 | + * 清除缓存 | ||
98 | + * | ||
99 | + * @param string $key 键名 | ||
100 | + * @return void | ||
101 | + */ | ||
102 | + public static function delete($key) | ||
103 | + { | ||
104 | + // WINDOWS | ||
105 | + if (DIRECTORY_SEPARATOR === '\\') { | ||
106 | + HoodCache::Memcache('master')->delete(self::makeKey($key, 'master')); | ||
107 | + HoodCache::Memcache('slave')->delete(self::makeKey($key, 'slave')); | ||
108 | + } | ||
109 | + // LINUX | ||
110 | + else { | ||
111 | + HoodCache::Memcached('master')->delete(self::makeKey($key, 'master')); | ||
112 | + HoodCache::Memcached('slave')->delete(self::makeKey($key, 'slave')); | ||
113 | + } | ||
114 | + } | ||
115 | + | ||
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 | + /** | ||
143 | + * 生成键名 | ||
144 | + * | ||
145 | + * @param string $key 键名 | ||
146 | + * @param string $prefix 前缀标识 | ||
147 | + * @return string | ||
148 | + */ | ||
149 | + public static function makeKey($key, $prefix) | ||
150 | + { | ||
151 | + return md5($prefix . '_' . $key); | ||
152 | + } | ||
153 | + | ||
154 | +} |
-
Please register or login to post a comment