1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpFastCache\Drivers\Leveldb; |
16
|
|
|
|
17
|
|
|
use LevelDB as LeveldbClient; |
18
|
|
|
use phpFastCache\Core\Pool\DriverBaseTrait; |
19
|
|
|
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; |
20
|
|
|
use phpFastCache\Core\Pool\IO\IOHelperTrait; |
21
|
|
|
use phpFastCache\Entities\driverStatistic; |
22
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
23
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverException; |
24
|
|
|
use phpFastCache\Util\Directory; |
25
|
|
|
use Psr\Cache\CacheItemInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Driver |
29
|
|
|
* @package phpFastCache\Drivers |
30
|
|
|
*/ |
31
|
|
|
class Driver implements ExtendedCacheItemPoolInterface |
32
|
|
|
{ |
33
|
|
|
use DriverBaseTrait, IOHelperTrait; |
34
|
|
|
|
35
|
|
|
const LEVELDB_FILENAME = '.database'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var LeveldbClient Instance of driver service |
39
|
|
|
*/ |
40
|
|
|
public $instance; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Driver constructor. |
44
|
|
|
* @param array $config |
45
|
|
|
* @throws phpFastCacheDriverException |
46
|
|
|
*/ |
47
|
|
|
public function __construct(array $config = []) |
48
|
|
|
{ |
49
|
|
|
$this->setup($config); |
50
|
|
|
|
51
|
|
|
if (!$this->driverCheck()) { |
52
|
|
|
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
53
|
|
|
} else { |
54
|
|
|
$this->driverConnect(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheCoreException |
61
|
|
|
*/ |
62
|
|
|
public function getLeveldbFile() |
63
|
|
|
{ |
64
|
|
|
return $this->getPath() . '/' . self::LEVELDB_FILENAME; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function driverCheck() |
71
|
|
|
{ |
72
|
|
|
return extension_loaded('Leveldb'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
77
|
|
|
* @return mixed |
78
|
|
|
* @throws \InvalidArgumentException |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
protected function driverWrite(CacheItemInterface $item) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
/** |
83
|
|
|
* Check for Cross-Driver type confusion |
84
|
|
|
*/ |
85
|
|
|
if ($item instanceof Item) { |
86
|
|
|
return $this->instance->set($item->getKey(), $this->encode($this->driverPreWrap($item))); |
87
|
|
|
} else { |
88
|
|
|
throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
protected function driverRead(CacheItemInterface $item) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$val = $this->instance->get($item->getKey()); |
99
|
|
|
if ($val == false) { |
100
|
|
|
return null; |
101
|
|
|
} else { |
102
|
|
|
return $this->decode($val); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param \Psr\Cache\CacheItemInterface $item |
108
|
|
|
* @return bool |
109
|
|
|
* @throws \InvalidArgumentException |
110
|
|
|
*/ |
111
|
|
|
protected function driverDelete(CacheItemInterface $item) |
112
|
|
|
{ |
113
|
|
|
/** |
114
|
|
|
* Check for Cross-Driver type confusion |
115
|
|
|
*/ |
116
|
|
|
if ($item instanceof Item) { |
117
|
|
|
return $this->instance->delete($item->getKey()); |
118
|
|
|
} else { |
119
|
|
|
throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
protected function driverClear() |
127
|
|
|
{ |
128
|
|
|
if ($this->instance instanceof LeveldbClient) { |
129
|
|
|
$this->instance->close(); |
130
|
|
|
$this->instance = null; |
131
|
|
|
} |
132
|
|
|
$result = LeveldbClient::destroy($this->getLeveldbFile()); |
133
|
|
|
$this->driverConnect(); |
134
|
|
|
|
135
|
|
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
protected function driverConnect() |
142
|
|
|
{ |
143
|
|
|
if ($this->instance instanceof LeveldbClient) { |
144
|
|
|
throw new \LogicException('Already connected to Leveldb database'); |
145
|
|
|
} else { |
146
|
|
|
$this->instance = $this->instance ?: new LeveldbClient($this->getLeveldbFile()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Close connection on destruct |
154
|
|
|
*/ |
155
|
|
|
public function __destruct() |
156
|
|
|
{ |
157
|
|
|
if ($this->instance instanceof LeveldbClient) { |
158
|
|
|
$this->instance->close(); |
159
|
|
|
$this->instance = null; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.