bookmarks/lib/Service/FileCache.php

161 lines
3.4 KiB
PHP
Raw Normal View History

2019-05-08 14:45:16 +00:00
<?php
2020-09-21 12:25:50 +00:00
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
2019-05-08 14:45:16 +00:00
*
2020-09-21 12:25:50 +00:00
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
2019-05-08 14:45:16 +00:00
*/
2019-10-26 12:10:49 +00:00
namespace OCA\Bookmarks\Service;
2019-05-08 14:45:16 +00:00
use OCP\AppFramework\Utility\ITimeFactory;
2019-05-08 14:45:16 +00:00
use OCP\Files\IAppData;
2019-10-26 12:10:49 +00:00
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFolder;
2019-05-08 14:45:16 +00:00
use OCP\ICache;
class FileCache implements ICache {
public const TIMEOUT = 60 * 60 * 24 * 30 * 2; // two months
2019-05-08 14:45:16 +00:00
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;
}
2019-05-08 14:45:16 +00:00
try {
$this->storage = $this->appData->getFolder('cache');
2019-10-26 12:10:49 +00:00
} catch (NotFoundException $e) {
// noop
}
if ($this->storage === null || !$this->storage->fileExists('/')) {
$this->storage = $this->appData->newFolder('cache');
2019-05-08 14:45:16 +00:00
}
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;
2019-05-08 14:45:16 +00:00
}
/**
* @param string $key
* @return mixed|null
*/
public function get($key) {
$result = null;
try {
$result = $this->getStorage()->getFile($key)->getContent();
} catch (\Exception $e) {
// noop
2019-05-08 14:45:16 +00:00
}
return $result;
}
2019-10-26 12:10:49 +00:00
2019-05-08 14:45:16 +00:00
/**
* Returns the size of the stored/cached data
*
* @param string $key
* @return int|float
* @throws NotFoundException|NotPermittedException
2019-05-08 14:45:16 +00:00
*/
public function size(string $key) {
2019-05-08 14:45:16 +00:00
$result = 0;
if ($this->hasKey($key)) {
$result = $this->getStorage()->getFile($key)->getSize();
2019-05-08 14:45:16 +00:00
}
return $result;
}
2019-10-26 12:10:49 +00:00
2019-05-08 14:45:16 +00:00
/**
* @param string $key
* @param mixed $value
* @param int $ttl
* @return bool
2019-05-08 14:45:16 +00:00
*/
public function set($key, $value, $ttl = 0) {
try {
$this->getStorage()->newFile($key, $value);
} catch (NotPermittedException $e) {
return false;
}
2019-05-08 14:45:16 +00:00
return true;
}
2019-05-08 14:45:16 +00:00
/**
* @param string $key
* @return bool
*/
public function hasKey($key) {
if ($this->getStorage()->fileExists($key)) {
2019-05-08 14:45:16 +00:00
return true;
}
return false;
}
2019-10-26 12:10:49 +00:00
2019-05-08 14:45:16 +00:00
/**
* @param string $key
* @return bool
2019-10-26 12:10:49 +00:00
* @throws NotFoundException
* @throws NotPermittedException
2019-05-08 14:45:16 +00:00
*/
public function remove($key) {
return (boolean) $this->getStorage()->getFile($key)->delete();
2019-05-08 14:45:16 +00:00
}
2019-10-26 12:10:49 +00:00
2019-05-08 14:45:16 +00:00
/**
* @param string $prefix
2019-10-26 12:10:49 +00:00
* @return void
* @throws NotPermittedException
2019-05-08 14:45:16 +00:00
*/
public function clear($prefix = '') {
foreach ($this->getStorage()->getDirectoryListing() as $file) {
$file->delete();
}
2019-05-08 14:45:16 +00:00
}
2019-10-26 12:10:49 +00:00
2019-05-08 14:45:16 +00:00
/**
* Runs GC
2019-10-26 12:10:49 +00:00
*
* @throws NotPermittedException
2019-05-08 14:45:16 +00:00
*/
2020-09-21 12:17:46 +00:00
public function gc(): void {
foreach ($this->getStorage()->getDirectoryListing() as $file) {
if ($this->timeFactory->getTime() - self::TIMEOUT > $file->getMTime()) {
2019-05-08 14:45:16 +00:00
$file->delete();
}
}
}
}