Repository navigation
Expand file tree
/
Copy pathFileSystemDriver.php
More file actions
175 lines (143 loc) · 4.18 KB
/
Copy pathFileSystemDriver.php
File metadata and controls
175 lines (143 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Cache\Driver;
use Hyperf\Cache\Collector\FileStorage;
use Hyperf\Cache\Exception\CacheException;
use Hyperf\Cache\Exception\InvalidArgumentException;
use Hyperf\Support\Filesystem\Filesystem;
use Psr\Container\ContainerInterface;
class FileSystemDriver extends Driver
{
/**
* @var string
*/
protected $storePath = BASE_PATH . '/runtime/caches';
/**
* @var Filesystem
*/
private $filesystem;
public function __construct(ContainerInterface $container, array $config)
{
parent::__construct($container, $config);
if (! is_dir($this->storePath)) {
$results = mkdir($this->storePath, 0777, true);
if (! $results) {
throw new CacheException('Has no permission to create cache directory!');
}
}
$this->filesystem = $container->get(Filesystem::class);
}
public function getCacheKey(string $key)
{
return $this->getPrefix() . $key . '.cache';
}
public function get($key, $default = null): mixed
{
$file = $this->getCacheKey($key);
if (! file_exists($file)) {
return $default;
}
/** @var FileStorage $obj */
$obj = $this->packer->unpack($this->filesystem->get($file));
if ($obj->isExpired()) {
return $default;
}
return $obj->getData();
}
public function fetch(string $key, $default = null): array
{
$file = $this->getCacheKey($key);
if (! file_exists($file)) {
return [false, $default];
}
/** @var FileStorage $obj */
$obj = $this->packer->unpack($this->filesystem->get($file));
if ($obj->isExpired()) {
return [false, $default];
}
return [true, $obj->getData()];
}
public function set($key, $value, $ttl = null): bool
{
$seconds = $this->secondsUntil($ttl);
$file = $this->getCacheKey($key);
$content = $this->packer->pack(new FileStorage($value, $seconds));
$result = $this->filesystem->put($file, $content);
return (bool) $result;
}
public function delete($key): bool
{
$file = $this->getCacheKey($key);
if (file_exists($file)) {
if (! is_writable($file)) {
return false;
}
unlink($file);
}
return true;
}
public function clear(): bool
{
return $this->clearPrefix('');
}
public function getMultiple($keys, $default = null): iterable
{
if (! is_array($keys)) {
throw new InvalidArgumentException('The keys is invalid!');
}
$result = [];
foreach ($keys as $key) {
$result[$key] = $this->get($key, $default);
}
return $result;
}
public function setMultiple($values, $ttl = null): bool
{
if (! is_array($values)) {
throw new InvalidArgumentException('The values is invalid!');
}
$seconds = $this->secondsUntil($ttl);
foreach ($values as $key => $value) {
$this->set($key, $value, $seconds);
}
return true;
}
public function deleteMultiple($keys): bool
{
if (! is_array($keys)) {
throw new InvalidArgumentException('The keys is invalid!');
}
foreach ($keys as $key) {
$this->delete($key);
}
return true;
}
public function has($key): bool
{
$file = $this->getCacheKey($key);
return file_exists($file);
}
public function clearPrefix(string $prefix): bool
{
$files = glob($this->getPrefix() . $prefix . '*');
foreach ($files as $file) {
if (is_dir($file)) {
continue;
}
unlink($file);
}
return true;
}
protected function getPrefix()
{
return $this->storePath . DIRECTORY_SEPARATOR . $this->prefix;
}
}