1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wechat\CacheDriver; |
4
|
|
|
|
5
|
|
|
use Wechat\CacheDriver\BaseDriver; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* 文件缓存驱动. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
class RedisDriver extends BaseDriver |
12
|
|
|
{ |
13
|
|
|
private $redis; |
14
|
|
|
private $pre; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$pre = 'WeixinApi:'; |
19
|
|
|
parent::__construct($pre); |
20
|
|
|
|
21
|
|
|
if (!class_exists('redis')) { |
22
|
|
|
exit('Redis初始化连接失败'); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
ini_set('default_socket_timeout', 50);//socket连接超时时间; |
26
|
|
|
$this->redis = new \redis(); |
27
|
|
|
// 如果用框架 此处最好走配置文件 |
28
|
|
|
$host = '127.0.0.1'; |
29
|
|
|
$port = '6379'; |
30
|
|
|
$this->pre = $pre; |
31
|
|
|
$link = $this->redis->connect($host, $port, 40); |
32
|
|
|
|
33
|
|
|
if (!$link) { |
34
|
|
|
exit('Redis初始化连接失败'); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 根据缓存名获取缓存内容. |
40
|
|
|
* |
41
|
|
|
* @param string $name |
42
|
|
|
* |
43
|
|
|
* @return bool|mixed|string |
44
|
|
|
*/ |
45
|
|
|
public function _get($name) |
46
|
|
|
{ |
47
|
|
|
$name = $this->createFileName($name); |
48
|
|
|
$data = $this->redis->get($name); |
49
|
|
|
if ($data) { |
50
|
|
|
$data = $this->unpackData($data); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $data; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 根据缓存名 设置缓存值和超时时间. |
58
|
|
|
* |
59
|
|
|
* @param string $name 缓存名 |
60
|
|
|
* @param void $value 缓存值 |
61
|
|
|
* @param int $expires 超时时间 |
62
|
|
|
* |
63
|
|
|
* @return boolean; |
|
|
|
|
64
|
|
|
*/ |
65
|
|
|
public function _set($name, $value, $expires) |
66
|
|
|
{ |
67
|
|
|
$name = $this->createFileName($name); |
68
|
|
|
$data = $this->packData($value); |
69
|
|
|
if ($expires === 0) { |
70
|
|
|
return $this->redis->set($name, $data); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->redis->setex($name, $expires, $data); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* 数据打包. |
78
|
|
|
* |
79
|
|
|
* @param void $data 缓存值 |
80
|
|
|
* @param int $expires 超时时间 |
|
|
|
|
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
private function packData($data) |
85
|
|
|
{ |
86
|
|
|
return serialize($data); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 数据解包. |
91
|
|
|
* |
92
|
|
|
* @param $data |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
private function unpackData($data) |
97
|
|
|
{ |
98
|
|
|
return unserialize($data); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* 创建缓存文件名. |
103
|
|
|
* |
104
|
|
|
* @param string $name 缓存名 |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
private function createFileName($name) |
109
|
|
|
{ |
110
|
|
|
return $this->pre . md5($name); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.