1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* |
5
|
|
|
* @author Joas Schilling <[email protected]> |
6
|
|
|
* @author Lukas Reschke <[email protected]> |
7
|
|
|
* @author Roeland Jago Douma <[email protected]> |
8
|
|
|
* @author Sebastian Wessalowski <[email protected]> |
9
|
|
|
* @author Thomas Müller <[email protected]> |
10
|
|
|
* @author Vincent Petry <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @license AGPL-3.0 |
13
|
|
|
* |
14
|
|
|
* This code is free software: you can redistribute it and/or modify |
15
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
16
|
|
|
* as published by the Free Software Foundation. |
17
|
|
|
* |
18
|
|
|
* This program is distributed in the hope that it will be useful, |
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21
|
|
|
* GNU Affero General Public License for more details. |
22
|
|
|
* |
23
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
25
|
|
|
* |
26
|
|
|
*/ |
27
|
|
|
namespace OCA\FaceRecognition\Service; |
28
|
|
|
|
29
|
|
|
use OCP\Files\IAppData; |
30
|
|
|
use OCP\Files\NotFoundException; |
31
|
|
|
use OCP\Files\NotPermittedException; |
32
|
|
|
use OCP\ICache; |
33
|
|
|
|
34
|
|
|
class FileCache implements ICache { |
35
|
|
|
|
36
|
|
|
const TIMEOUT = 60*60*24*30*2; // two months |
37
|
|
|
|
38
|
|
|
protected $storage; |
39
|
|
|
|
40
|
|
|
public function __construct(IAppData $appData) { |
41
|
|
|
try { |
42
|
|
|
$this->storage = $appData->getFolder('cache'); |
43
|
|
|
} catch (NotFoundException $e) { |
44
|
|
|
$appData->newFolder('cache'); |
45
|
|
|
$this->storage = $appData->getFolder('cache'); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $key |
51
|
|
|
* @return mixed|null |
52
|
|
|
* @throws NotFoundException |
53
|
|
|
* @throws NotPermittedException |
54
|
|
|
*/ |
55
|
|
|
public function get($key) { |
56
|
|
|
$result = null; |
57
|
|
|
if ($this->hasKey($key)) { |
58
|
|
|
$result = $this->storage->getFile($key)->getContent(); |
59
|
|
|
} |
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the size of the stored/cached data |
65
|
|
|
* |
66
|
|
|
* @param string $key |
67
|
|
|
* @return int |
68
|
|
|
* @throws NotFoundException |
69
|
|
|
*/ |
70
|
|
|
public function size($key) { |
71
|
|
|
$result = 0; |
72
|
|
|
if ($this->hasKey($key)) { |
73
|
|
|
$result = $this->storage->getFile($key)->getSize(); |
74
|
|
|
} |
75
|
|
|
return $result; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $key |
80
|
|
|
* @param mixed $value |
81
|
|
|
* @param int $ttl |
82
|
|
|
* @return bool|mixed |
83
|
|
|
* @throws NotFoundException |
84
|
|
|
* @throws NotPermittedException |
85
|
|
|
*/ |
86
|
|
|
public function set($key, $value, $ttl = 0) { |
87
|
|
|
$file = $this->storage->newFile($key); |
88
|
|
|
$file->putContent($value); |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
/** |
92
|
|
|
* @param string $key |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function hasKey($key) { |
96
|
|
|
if ($this->storage->fileExists($key)) { |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $key |
104
|
|
|
* @return bool|mixed |
105
|
|
|
* @throws NotFoundException |
106
|
|
|
* @throws NotPermittedException |
107
|
|
|
*/ |
108
|
|
|
public function remove($key) { |
109
|
|
|
return $this->storage->getFile($key)->delete(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $prefix |
114
|
|
|
* @return void |
115
|
|
|
* @throws NotPermittedException |
116
|
|
|
*/ |
117
|
|
|
public function clear($prefix = '') { |
118
|
|
|
$this->storage->delete(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Runs GC |
123
|
|
|
* |
124
|
|
|
* @throws NotPermittedException |
125
|
|
|
*/ |
126
|
|
|
public function gc() { |
127
|
|
|
foreach ($this->storage->getDirectoryListing() as $file) { |
128
|
|
|
if (time() - self::TIMEOUT > $file->getMTime()) { |
129
|
|
|
$file->delete(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|