1
|
|
|
<?php /** MicroXcacheDriver */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Cache\Driver; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
6
|
|
|
use Micro\Base\IContainer; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class XcacheDriver |
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 XcacheDriver 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('Extension XCache not installed'); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function check() |
46
|
|
|
{ |
47
|
|
|
return extension_loaded('xcache') ? true : false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function delete($name) |
54
|
|
|
{ |
55
|
|
|
return xcache_unset($name); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
|
|
public function clean() |
62
|
|
|
{ |
63
|
|
|
/** @noinspection PhpUndefinedConstantInspection */ |
64
|
|
|
for ($i = 0, $cnt = xcache_count(XC_TYPE_VAR); $i < $cnt; $i++) { |
65
|
|
|
/** @noinspection PhpUndefinedConstantInspection */ |
66
|
|
|
if (xcache_clear_cache(XC_TYPE_VAR, $i) === false) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
|
|
public function info() |
78
|
|
|
{ |
79
|
|
|
/** @noinspection PhpUndefinedConstantInspection */ |
80
|
|
|
return xcache_count(XC_TYPE_VAR); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
|
|
public function getMeta($id) |
87
|
|
|
{ |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function increment($name, $offset = 1) |
95
|
|
|
{ |
96
|
|
|
$val = $this->get($name) + $offset; |
97
|
|
|
|
98
|
|
|
return $this->set($name, $val); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
*/ |
104
|
|
|
public function get($name) |
105
|
|
|
{ |
106
|
|
|
return xcache_isset($name) ? xcache_get($name) : false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritdoc |
111
|
|
|
*/ |
112
|
|
|
public function set($name, $value, $duration = 0) |
113
|
|
|
{ |
114
|
|
|
return xcache_set($name, $value, $duration); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @inheritdoc |
119
|
|
|
*/ |
120
|
|
|
public function decrement($name, $offset = 1) |
121
|
|
|
{ |
122
|
|
|
$val = $this->get($name) - $offset; |
123
|
|
|
|
124
|
|
|
return $this->set($name, $val); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|