-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCache.php
More file actions
102 lines (80 loc) · 2.59 KB
/
Copy pathCache.php
File metadata and controls
102 lines (80 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* 缓存基类
* @author Xijin.Xiao
*/
class Cache {
// private static $cache;
private static $local;
private static $remote;
private function __construct(){}
static function encodeKey($key){
// if(is_array($key))
// foreach ($key as $key_num=>$key_val)
// $key[$key_num] = self::getPrefix().$key_val;
// else
// $key = self::getPrefix().$key;
return $key;
}
static function getPrefix(){
// return \Cfg::get('rpc.http_port').\Cfg::getEnvName();
return '';
}
static function decodeKey($key){
// $prefix = self::getPrefix();
// if($prefix == substr($key, 0,strlen($prefix)))
// return substr($key, strlen($prefix));
// else
return $key;
}
static private function getLocalCacheClient(){
if(self::$local)
return self::$local;
self::$local = new \Yac(self::getPrefix());
return self::$local;
}
static private function getRemoteCacheClient(){
if(self::$remote)
return self::$remote;
self::$remote = new \Client\Redis('master');
return self::$remote;
}
static public function set($key, $value, $expire = 0){
$l_ret = self::set_l($key, $value, $expire);
$r_ret = self::set_r($key, $value, $expire);
return $l_ret&$r_ret;
}
static public function set_l($key, $value, $expire = 0){
return self::getLocalCacheClient()->set($key, $value, $expire);
}
static public function set_r($key, $value, $expire = 0){
return self::getRemoteCacheClient()->set(self::encodeKey($key), $value, $expire);
}
static public function del($key){
$l_ret = self::del_l($key);
$r_ret = self::del_r($key);
return $l_ret&$r_ret;
}
static public function del_l($key){
return self::getLocalCacheClient()->delete($key);
}
static public function del_r($key){
return self::getRemoteCacheClient()->del(self::encodeKey($key));
}
static public function get($key){
if($ret = self::get_l($key))
return $ret;
if($ret = self::get_r($key))
return $ret;
}
static public function get_l($key){
return self::getLocalCacheClient()->get($key);
}
static public function get_r($key){
return self::getRemoteCacheClient()->get(self::encodeKey($key));
}
public static function __callStatic($name, $arguments)
{
return call_user_func_array([self::getRemoteCacheClient(),$name],self::encodeKey($arguments));
}
}