-
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathFileCache.php
More file actions
162 lines (145 loc) Β· 3.41 KB
/
Copy pathFileCache.php
File metadata and controls
162 lines (145 loc) Β· 3.41 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
<?php
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Bookmarks\Service;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\ICache;
class FileCache implements ICache {
public const TIMEOUT = 60 * 60 * 24 * 30 * 2; // two months
protected $storage;
/**
* @var ITimeFactory
*/
private $timeFactory;
/**
* @var IAppData
*/
private $appData;
public function __construct(IAppData $appData, ITimeFactory $timeFactory) {
$this->appData = $appData;
$this->timeFactory = $timeFactory;
}
public static function isAvailable(): bool {
return true;
}
/**
* @return ISimpleFolder
* @throws NotPermittedException
*/
private function getStorage(): ISimpleFolder {
if ($this->storage !== null) {
return $this->storage;
}
try {
$this->storage = $this->appData->getFolder('cache');
} catch (NotFoundException $e) {
// noop
}
if ($this->storage === null || !$this->storage->fileExists('/')) {
$this->storage = $this->appData->newFolder('cache');
}
if (!$this->storage->fileExists('CACHEDIR.TAG')) {
try {
$this->storage->newFile('CACHEDIR.TAG',
'Signature: 8a477f597d28d172789f06886806bc55' . "\r\n"
. '# This file is a cache directory tag created by the nextcloud bookmarks app.' . "\r\n"
. '# For information about cache directory tags, see:' . "\r\n"
. '# http://www.brynosaurus.com/cachedir/)' . "\r\n"
);
} catch (NotPermittedException $e) {
// No op
}
}
return $this->storage;
}
/**
* @param string $key
* @return mixed|null
*/
public function get($key) {
$result = null;
try {
$result = $this->getStorage()->getFile($key)->getContent();
} catch (\Exception $e) {
// noop
}
return $result;
}
/**
* Returns the size of the stored/cached data
*
* @param string $key
* @return int|float
* @throws NotFoundException|NotPermittedException
*/
public function size(string $key) {
$result = 0;
if ($this->hasKey($key)) {
$result = $this->getStorage()->getFile($key)->getSize();
}
return $result;
}
/**
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
*/
public function set($key, $value, $ttl = 0) {
try {
$this->getStorage()->newFile($key, $value);
} catch (NotPermittedException $e) {
return false;
}
return true;
}
/**
* @param string $key
* @return bool
*/
public function hasKey($key) {
if ($this->getStorage()->fileExists($key)) {
return true;
}
return false;
}
/**
* @param string $key
* @return bool
* @throws NotFoundException
* @throws NotPermittedException
*/
public function remove($key) {
return (bool)$this->getStorage()->getFile($key)->delete();
}
/**
* @param string $prefix
* @return bool
* @throws NotPermittedException
*/
public function clear($prefix = '') {
foreach ($this->getStorage()->getDirectoryListing() as $file) {
$file->delete();
}
return true;
}
/**
* Runs GC
*
* @throws NotPermittedException
*/
public function gc(): void {
foreach ($this->getStorage()->getDirectoryListing() as $file) {
if ($this->timeFactory->getTime() - self::TIMEOUT > $file->getMTime()) {
$file->delete();
}
}
}
}