Showing
2 changed files
with
170 additions
and
147 deletions
1 | -<?php | ||
2 | - | ||
3 | -namespace WebPlugin; | ||
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 | - if (USE_CACHE) { | ||
39 | - try { | ||
40 | - // WINDOWS | ||
41 | - if (DIRECTORY_SEPARATOR === '\\') { | ||
42 | - HoodCache::Memcache('master')->set(self::makeKey($key, 'master'), $value, $expire); | ||
43 | - HoodCache::Memcache('slave')->set(self::makeKey($key, 'slave'), $value, 86400); // 二级缓存1天 | ||
44 | - } | ||
45 | - // LINUX | ||
46 | - else { | ||
47 | - HoodCache::Memcached('master')->set(self::makeKey($key, 'master'), $value, $expire); | ||
48 | - HoodCache::Memcached('slave')->set(self::makeKey($key, 'slave'), $value, 86400); // 二级缓存1天 | ||
49 | - } | ||
50 | - } catch (Exception $e) { | ||
51 | - // do nothing | ||
52 | - } | ||
53 | - } | ||
54 | - } | ||
55 | - | ||
56 | - /** | ||
57 | - * 获取缓存 | ||
58 | - * | ||
59 | - * @param string $key 键名 | ||
60 | - * @param string $node master代表主服务器, slave代表从服务器 | ||
61 | - * @return mixed | ||
62 | - */ | ||
63 | - public static function get($key, $node = 'master') | ||
64 | - { | ||
65 | - $result = array(); | ||
66 | - | ||
67 | - if (USE_CACHE) { | ||
68 | - try { | ||
69 | - // WINDOWS | ||
70 | - if (DIRECTORY_SEPARATOR === '\\') { | ||
71 | - $result = HoodCache::Memcache($node)->get(self::makeKey($key, $node)); | ||
72 | - } | ||
73 | - // LINUX | ||
74 | - else { | ||
75 | - $result = HoodCache::Memcached($node)->get(self::makeKey($key, $node)); | ||
76 | - } | ||
77 | - } catch (Exception $e) { | ||
78 | - $result = array(); | ||
79 | - } | ||
80 | - } | ||
81 | - | ||
82 | - return $result; | ||
83 | - } | ||
84 | - | ||
85 | - /** | ||
86 | - * 清除缓存 | ||
87 | - * | ||
88 | - * @param string $key 键名 | ||
89 | - * @return void | ||
90 | - */ | ||
91 | - public static function delete($key) | ||
92 | - { | ||
93 | - if (USE_CACHE) { | ||
94 | - // WINDOWS | ||
95 | - if (DIRECTORY_SEPARATOR === '\\') { | ||
96 | - HoodCache::Memcache('master')->delete(self::makeKey($key, 'master')); | ||
97 | - HoodCache::Memcache('slave')->delete(self::makeKey($key, 'slave')); | ||
98 | - } | ||
99 | - // LINUX | ||
100 | - else { | ||
101 | - HoodCache::Memcached('master')->delete(self::makeKey($key, 'master')); | ||
102 | - HoodCache::Memcached('slave')->delete(self::makeKey($key, 'slave')); | ||
103 | - } | ||
104 | - } | ||
105 | - } | ||
106 | - | ||
107 | - /** | ||
108 | - * 生成键名 | ||
109 | - * | ||
110 | - * @param string $key 键名 | ||
111 | - * @param string $prefix 前缀标识 | ||
112 | - * @return string | ||
113 | - */ | ||
114 | - public static function makeKey($key, $prefix) | ||
115 | - { | ||
116 | - return md5($prefix . '_' . $key); | ||
117 | - } | ||
118 | - | ||
119 | - /** | ||
120 | - * 累加 | ||
121 | - * | ||
122 | - * @param string $key | ||
123 | - * @param int $offset | ||
124 | - * @param int $initialValue | ||
125 | - * @param int $expiry | ||
126 | - * @return boolean | ||
127 | - */ | ||
128 | - public static function increment($key, $offset = 1, $initialValue = 0, $expiry = 0) | ||
129 | - { | ||
130 | - return HoodCache::Memcached('master')->increment(self::makeKey($key, 'master'), $offset, $initialValue, $expiry); | ||
131 | - } | ||
132 | - | ||
133 | - /** | ||
134 | - * 递减 | ||
135 | - * | ||
136 | - * @param string $key | ||
137 | - * @param int $offset | ||
138 | - * @return boolean | ||
139 | - */ | ||
140 | - public static function decrement($key, $offset = 1) | ||
141 | - { | ||
142 | - return HoodCache::Memcached('master')->decrement(self::makeKey($key, 'master'), $offset); | ||
143 | - } | ||
144 | - | ||
145 | -} | 1 | +<?php |
2 | + | ||
3 | +namespace WebPlugin; | ||
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 | + if (USE_CACHE) { | ||
39 | + try { | ||
40 | + // WINDOWS | ||
41 | + if (DIRECTORY_SEPARATOR === '\\') { | ||
42 | + HoodCache::Memcache('master')->set(self::makeKey($key, 'master'), $value, $expire); | ||
43 | + HoodCache::Memcache('slave')->set(self::makeKey($key, 'slave'), $value, 86400); // 二级缓存1天 | ||
44 | + } | ||
45 | + // LINUX | ||
46 | + else { | ||
47 | + HoodCache::Memcached('master')->set(self::makeKey($key, 'master'), $value, $expire); | ||
48 | + HoodCache::Memcached('slave')->set(self::makeKey($key, 'slave'), $value, 86400); // 二级缓存1天 | ||
49 | + } | ||
50 | + } catch (Exception $e) { | ||
51 | + // do nothing | ||
52 | + } | ||
53 | + } | ||
54 | + } | ||
55 | + | ||
56 | + /** | ||
57 | + * 获取缓存 | ||
58 | + * | ||
59 | + * @param string $key 键名 | ||
60 | + * @param string $node master代表主服务器, slave代表从服务器 | ||
61 | + * @return mixed | ||
62 | + */ | ||
63 | + public static function get($key, $node = 'master') | ||
64 | + { | ||
65 | + $result = array(); | ||
66 | + | ||
67 | + if (USE_CACHE) { | ||
68 | + try { | ||
69 | + // WINDOWS | ||
70 | + if (DIRECTORY_SEPARATOR === '\\') { | ||
71 | + $result = HoodCache::Memcache($node)->get(self::makeKey($key, $node)); | ||
72 | + } | ||
73 | + // LINUX | ||
74 | + else { | ||
75 | + $result = HoodCache::Memcached($node)->get(self::makeKey($key, $node)); | ||
76 | + } | ||
77 | + } catch (Exception $e) { | ||
78 | + $result = array(); | ||
79 | + } | ||
80 | + } | ||
81 | + | ||
82 | + return $result; | ||
83 | + } | ||
84 | + | ||
85 | + /** | ||
86 | + * 直接查询缓存,不对key做任何处理 | ||
87 | + */ | ||
88 | + public static function getBy($key, $node = 'master') | ||
89 | + { | ||
90 | + $result = array(); | ||
91 | + | ||
92 | + try { | ||
93 | + // WINDOWS | ||
94 | + if (DIRECTORY_SEPARATOR === '\\') { | ||
95 | + $result = HoodCache::Memcache($node)->getBy($key); | ||
96 | + } | ||
97 | + // LINUX | ||
98 | + else { | ||
99 | + $result = HoodCache::Memcached($node)->getBy($key); | ||
100 | + } | ||
101 | + } catch (Exception $e) { | ||
102 | + $result = array(); | ||
103 | + } | ||
104 | + | ||
105 | + return $result; | ||
106 | + } | ||
107 | + | ||
108 | + /** | ||
109 | + * 清除缓存 | ||
110 | + * | ||
111 | + * @param string $key 键名 | ||
112 | + * @return void | ||
113 | + */ | ||
114 | + public static function delete($key) | ||
115 | + { | ||
116 | + if (USE_CACHE) { | ||
117 | + // WINDOWS | ||
118 | + if (DIRECTORY_SEPARATOR === '\\') { | ||
119 | + HoodCache::Memcache('master')->delete(self::makeKey($key, 'master')); | ||
120 | + HoodCache::Memcache('slave')->delete(self::makeKey($key, 'slave')); | ||
121 | + } | ||
122 | + // LINUX | ||
123 | + else { | ||
124 | + HoodCache::Memcached('master')->delete(self::makeKey($key, 'master')); | ||
125 | + HoodCache::Memcached('slave')->delete(self::makeKey($key, 'slave')); | ||
126 | + } | ||
127 | + } | ||
128 | + } | ||
129 | + | ||
130 | + /** | ||
131 | + * 生成键名 | ||
132 | + * | ||
133 | + * @param string $key 键名 | ||
134 | + * @param string $prefix 前缀标识 | ||
135 | + * @return string | ||
136 | + */ | ||
137 | + public static function makeKey($key, $prefix) | ||
138 | + { | ||
139 | + return md5($prefix . '_' . $key); | ||
140 | + } | ||
141 | + | ||
142 | + /** | ||
143 | + * 累加 | ||
144 | + * | ||
145 | + * @param string $key | ||
146 | + * @param int $offset | ||
147 | + * @param int $initialValue | ||
148 | + * @param int $expiry | ||
149 | + * @return boolean | ||
150 | + */ | ||
151 | + public static function increment($key, $offset = 1, $initialValue = 0, $expiry = 0) | ||
152 | + { | ||
153 | + return HoodCache::Memcached('master')->increment(self::makeKey($key, 'master'), $offset, $initialValue, $expiry); | ||
154 | + } | ||
155 | + | ||
156 | + /** | ||
157 | + * 递减 | ||
158 | + * | ||
159 | + * @param string $key | ||
160 | + * @param int $offset | ||
161 | + * @return boolean | ||
162 | + */ | ||
163 | + public static function decrement($key, $offset = 1) | ||
164 | + { | ||
165 | + return HoodCache::Memcached('master')->decrement(self::makeKey($key, 'master'), $offset); | ||
166 | + } | ||
167 | + | ||
168 | +} |
-
Please register or login to post a comment