1
|
|
|
<?php /** MicroApcDriver */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Cache\Driver; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
6
|
|
|
use Micro\Base\IContainer; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ApcDriver |
10
|
|
|
* |
11
|
|
|
* @author Oleg Lunegov <[email protected]> |
12
|
|
|
* @link https://github.com/linpax/microphp-framework |
13
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
14
|
|
|
* @license /LICENSE |
15
|
|
|
* @package Micro |
16
|
|
|
* @subpackage Cache\Driver |
17
|
|
|
* @version 1.0 |
18
|
|
|
* @since 1.0 |
19
|
|
|
*/ |
20
|
|
|
class ApcDriver extends BaseCacheDriver |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Constructor |
24
|
|
|
* |
25
|
|
|
* @access public |
26
|
|
|
* |
27
|
|
|
* @param IContainer $container |
28
|
|
|
* @param array $config config array |
29
|
|
|
* |
30
|
|
|
* @result void |
31
|
|
|
* @throws Exception |
32
|
|
|
*/ |
33
|
|
|
public function __construct(IContainer $container, array $config = []) |
34
|
|
|
{ |
35
|
|
|
parent::__construct($container, $config); |
36
|
|
|
|
37
|
|
|
if (!$this->check()) { |
38
|
|
|
throw new Exception('APC cache not installed'); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function check() |
46
|
|
|
{ |
47
|
|
|
if (extension_loaded('apc') && ini_get('apc.enabled')) { |
48
|
|
|
return true; |
49
|
|
|
} else { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
public function get($name) |
58
|
|
|
{ |
59
|
|
|
$values = apc_fetch($name); |
60
|
|
|
|
61
|
|
|
return is_array($values) ? $values : []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public function set($name, $value, $duration = 300, $new = false) |
68
|
|
|
{ |
69
|
|
|
if ($new === true) { |
70
|
|
|
return apc_add($name, $value, $duration); |
71
|
|
|
} else { |
72
|
|
|
return apc_store($name, $value, $duration); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function delete($name) |
80
|
|
|
{ |
81
|
|
|
return apc_delete($name); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
*/ |
87
|
|
|
public function clean() |
88
|
|
|
{ |
89
|
|
|
if (extension_loaded('apc')) { |
90
|
|
|
return apc_clear_cache(); |
91
|
|
|
} else { |
92
|
|
|
return apc_clear_cache('user'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
*/ |
99
|
|
|
public function info($type = null) |
100
|
|
|
{ |
101
|
|
|
return apc_cache_info($type); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritdoc |
106
|
|
|
*/ |
107
|
|
|
public function getMeta($id) |
108
|
|
|
{ |
109
|
|
|
$success = false; |
110
|
|
|
|
111
|
|
|
$stored = apc_fetch($id, $success); |
112
|
|
|
if ($success === false || count($stored) !== 3) { |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
list($data, $time, $ttl) = $stored; |
117
|
|
|
|
118
|
|
|
return ['expire' => $time + $ttl, 'mtime' => $time, 'data' => unserialize($data)]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritdoc |
123
|
|
|
*/ |
124
|
|
|
public function increment($name, $offset = 1) |
125
|
|
|
{ |
126
|
|
|
return apc_inc($name, $offset); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritdoc |
131
|
|
|
*/ |
132
|
|
|
public function decrement($name, $offset = 1) |
133
|
|
|
{ |
134
|
|
|
return apc_dec($name, $offset); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|