2018-06-06 23:20:29 +02:00
|
|
|
<?php namespace Todaymade\Daux;
|
|
|
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Todaymade\Daux\Daux;
|
|
|
|
|
2018-06-07 20:40:38 +02:00
|
|
|
class Cache
|
|
|
|
{
|
2020-04-22 21:55:53 +02:00
|
|
|
public static $printed = false;
|
2018-06-06 23:20:29 +02:00
|
|
|
|
2019-11-28 23:32:33 +01:00
|
|
|
public static function getDirectory(): string
|
2018-06-06 23:20:29 +02:00
|
|
|
{
|
|
|
|
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "dauxio" . DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
if (!Cache::$printed) {
|
|
|
|
Cache::$printed = true;
|
|
|
|
Daux::writeln("Using cache dir '$dir'", OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store an item in the cache for a given number of minutes.
|
|
|
|
*
|
|
|
|
* @param string $key
|
2019-11-28 23:32:33 +01:00
|
|
|
* @param string $value
|
2018-06-06 23:20:29 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
2019-11-28 23:32:33 +01:00
|
|
|
public static function put(string $key, string $value): void
|
2018-06-06 23:20:29 +02:00
|
|
|
{
|
|
|
|
Cache::ensureCacheDirectoryExists($path = Cache::path($key));
|
2018-06-07 20:40:38 +02:00
|
|
|
file_put_contents($path, $value);
|
2018-06-06 23:20:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the file cache directory if necessary.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-11-28 23:32:33 +01:00
|
|
|
protected static function ensureCacheDirectoryExists(string $path): void
|
2018-06-06 23:20:29 +02:00
|
|
|
{
|
|
|
|
$parent = dirname($path);
|
|
|
|
|
|
|
|
if (!file_exists($parent)) {
|
|
|
|
mkdir($parent, 0777, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item from the cache.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-11-28 23:32:33 +01:00
|
|
|
public static function forget(string $key): bool
|
2018-06-06 23:20:29 +02:00
|
|
|
{
|
|
|
|
$path = Cache::path($key);
|
|
|
|
|
|
|
|
if (file_exists($path)) {
|
|
|
|
return unlink($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve an item from the cache by key.
|
|
|
|
*
|
2019-11-28 23:32:33 +01:00
|
|
|
* @param string $key
|
2018-06-06 23:20:29 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-11-28 23:32:33 +01:00
|
|
|
public static function get(string $key): ?string
|
2018-06-06 23:20:29 +02:00
|
|
|
{
|
|
|
|
$path = Cache::path($key);
|
|
|
|
|
|
|
|
if (file_exists($path)) {
|
|
|
|
return file_get_contents($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the full path for the given cache key.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-11-28 23:32:33 +01:00
|
|
|
protected static function path(string $key): string
|
2018-06-06 23:20:29 +02:00
|
|
|
{
|
|
|
|
$parts = array_slice(str_split($hash = sha1($key), 2), 0, 2);
|
2018-06-07 20:40:38 +02:00
|
|
|
return Cache::getDirectory() . '/' . implode('/', $parts) . '/' . $hash;
|
2018-06-06 23:20:29 +02:00
|
|
|
}
|
2018-06-07 20:40:38 +02:00
|
|
|
|
2019-11-28 23:32:33 +01:00
|
|
|
public static function clear(): void
|
2018-06-07 20:40:38 +02:00
|
|
|
{
|
|
|
|
Cache::rrmdir(Cache::getDirectory());
|
|
|
|
}
|
|
|
|
|
2019-11-28 23:32:33 +01:00
|
|
|
protected static function rrmdir(string $dir): void
|
2018-06-07 20:40:38 +02:00
|
|
|
{
|
|
|
|
if (is_dir($dir)) {
|
|
|
|
$objects = scandir($dir);
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
if ($object != "." && $object != "..") {
|
|
|
|
if (is_dir($dir . "/" . $object)) {
|
|
|
|
Cache::rrmdir($dir . "/" . $object);
|
|
|
|
} else {
|
|
|
|
unlink($dir . "/" . $object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rmdir($dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|