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 phpFastCache\Core\DriverAbstract; |
18
|
|
|
use phpFastCache\Core\PathSeekerTrait; |
19
|
|
|
use phpFastCache\Core\StandardPsr6StructureTrait; |
20
|
|
|
use phpFastCache\Entities\driverStatistic; |
21
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
22
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverException; |
23
|
|
|
use phpFastCache\Util\Directory; |
24
|
|
|
use Psr\Cache\CacheItemInterface; |
25
|
|
|
use LevelDB as LeveldbClient; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Driver |
29
|
|
|
* @package phpFastCache\Drivers |
30
|
|
|
*/ |
31
|
|
|
class Driver extends DriverAbstract |
32
|
|
|
{ |
33
|
|
|
use PathSeekerTrait, StandardPsr6StructureTrait; |
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
|
|
View Code Duplication |
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
|
|
|
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
|
|
|
unset($this->instance); |
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
|
|
|
|
150
|
|
|
/******************** |
151
|
|
|
* |
152
|
|
|
* PSR-6 Extended Methods |
153
|
|
|
* |
154
|
|
|
*******************/ |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return driverStatistic |
158
|
|
|
*/ |
159
|
|
|
public function getStats() |
160
|
|
|
{ |
161
|
|
|
return (new driverStatistic()) |
162
|
|
|
->setData(implode(', ', array_keys($this->itemInstances))) |
163
|
|
|
->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($this->getLeveldbFile())) |
164
|
|
|
->setSize(Directory::dirSize($this->getLeveldbFile())); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Close connection on destruct |
169
|
|
|
*/ |
170
|
|
|
public function __destruct() |
171
|
|
|
{ |
172
|
|
|
$this->instance->close(); |
173
|
|
|
} |
174
|
|
|
} |
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.