You can use Memcache with filemtime
$path = __DIR__ . "/test"; $cache = new Memcache(); $cache->addserver("localhost"); $key = sha1($path); $info = $cache->get(sha1($path)); if ($info && $info->time == filemtime($path)) { echo "Cache Copy ", date("Ymd g:i:s", $info->time); } else { $info = new stdClass(); $info->readDir = array_map("strval", iterator_to_array(new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS))); $info->time = filemtime($path); $cache->set($key, $info, MEMCACHE_COMPRESSED, 0); echo "Path Changed ", date("Ymd g:i:s", $info->time); } var_dump(array_values($info->readDir));
Refresh
Unfortunately, my shared host does not support APC or memcache: - (
You can use file systems
$path = __DIR__ . "/test"; $cache = new MyCache(__DIR__ . "/a"); $key = sha1($path); $info = $cache->get($key); if ($info && $info->time == filemtime($path)) { echo "Cache Copy ", date("Ymd g:i:s", $info->time); } else { $info = new stdClass(); $info->readDir = array_map("strval", iterator_to_array(new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS))); $info->time = filemtime($path); $cache->set($key, $info, MEMCACHE_COMPRESSED, 0); echo "Path Changed ", date("Ymd g:i:s", $info->time); } var_dump(array_values((array) $info->readDir));
Used class
class MyCache { private $path; function __construct($path) { is_writable($path) or trigger_error("Path Not Writeable"); is_dir($path) or trigger_error("Path Not a Directory"); $this->path = $path; } function get($key) { $file = $this->path . DIRECTORY_SEPARATOR . $key . ".cache"; if (! is_file($file)) return false; $data = file_get_contents($file); substr($data, 0, 2) == "##" and $data = gzinflate(substr($data, 2)); return json_decode($data); } function set($key, $value, $compression = 0) { $data = json_encode($value); $compression and $data = gzdeflate($data, 9) and $data = "##" . $data; return file_put_contents($this->path . DIRECTORY_SEPARATOR . $key . ".cache", $data); } }
source share