1
0
mirror of https://github.com/danog/file.git synced 2024-11-30 04:19:39 +01:00
file/lib/StatCache.php
2015-08-08 10:42:41 -04:00

61 lines
1.5 KiB
PHP

<?php
namespace Amp\File;
class StatCache {
private static $cache = [];
private static $timeouts = [];
private static $ttl = 3;
private static $now = null;
private static $isInitialized = false;
private static function init() {
self::$isInitialized = true;
self::$now = \time();
\Amp\repeat(function () {
self::$now = $now = \time();
foreach (self::$cache as $path => $expiry) {
if ($now > $expiry) {
unset(
self::$cache[$path],
self::$timeouts[$path]
);
} else {
break;
}
}
}, 1000, ["keep_alive" => false]);
}
public static function get($path) {
return isset(self::$cache[$path]) ? self::$cache[$path] : null;
}
public static function set($path, array $stat) {
if (self::$ttl <= 0) {
return;
}
if (empty(self::$isInitialized)) {
self::init();
}
self::$cache[$path] = $stat;
self::$timeouts[$path] = self::$now + self::$ttl;
}
public static function ttl($seconds) {
self::$ttl = (int) $seconds;
}
public static function clear($path = null) {
if (isset($path)) {
unset(
self::$cache[$path],
self::$timeouts[$path]
);
} else {
self::$cache = [];
self::$timeouts = [];
}
}
}