Readdir () caching

Is there a readdir () result in the cache? Right now I am reading readdir () in the directory tree every time I enter a specific webpage into a website.

UPDATE:

  • The directory structure is the same for all users.
  • Unfortunately, my shared host does not support APC or memcache: - (
0
source share
3 answers

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); } } 
+1
source

You can save it in a session variable if you start a session. Look at the session_start () function, etc.

0
source

You can cache any PHP serializable structure using a number of methods. PHP ships with APC these days, so I suggest looking at caching APC objects.

http://uk1.php.net/manual/en/ref.apc.php

You must have some logic to clear the cache when changing the directory structure.

0
source

All Articles