-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathFileSystemCache.php
More file actions
63 lines (58 loc) Β· 1.53 KB
/
FileSystemCache.php
File metadata and controls
63 lines (58 loc) Β· 1.53 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
<?php
declare(strict_types=1);
namespace LanguageServer\Cache;
/**
* Caches content on the file system
*/
class FileSystemCache implements Cache
{
/**
* @var string
*/
public $cacheDir;
public function __construct()
{
if (strtoupper(substr(php_uname('s'), 0, 3)) === 'WIN') {
$this->cacheDir = getenv('LOCALAPPDATA') . '\\PHP Language Server\\';
} else if (getenv('XDG_CACHE_HOME')) {
$this->cacheDir = getenv('XDG_CACHE_HOME') . '/phpls/';
} else {
$this->cacheDir = getenv('HOME') . '/.phpls/';
}
}
/**
* Gets a value from the cache
*
* @param string $key
* @return \Generator <mixed>
*/
public function get(string $key): \Generator
{
try {
$file = $this->cacheDir . urlencode($key);
$content = yield \Amp\File\get($file);
return unserialize($content);
} catch (\Exception $e) {
return null;
}
}
/**
* Sets a value in the cache
*
* @param string $key
* @param mixed $value
* @return \Generator
*/
public function set(string $key, $value): \Generator
{
$file = $this->cacheDir . urlencode($key);
$dir = dirname($file);
if (yield \Amp\File\isfile($dir)) {
yield \Amp\File\unlink($dir);
}
if (!yield \Amp\File\exists($dir)) {
yield \Amp\File\mkdir($dir, 0777, true);
}
yield \Amp\File\put($file, serialize($value));
}
}