3

Possible Duplicate:
Caching readdir()

I have my site set up so that I have clean URLs and the files themselves are in various levels of folders. To manage it, I have the following script to direct my include farther down in the page.

    function listFolderFiles($dir,$exclude){
    global $flist;
    $ffs = scandir($dir); 
    foreach($ffs as $ff)
    { 
        if(is_array($exclude) and !in_array($ff,$exclude))
        { 
            if($ff != '.' && $ff != '..')
            { 
                if(!is_dir($dir.'/'.$ff))
                {       
            }
            if(is_dir($dir.'/'.$ff)) 
                {
                listFolderFiles($dir.'/'.$ff,$exclude);
            } 
            if((is_dir($dir.'/'.$ff)) != 1 && strtolower(substr($ff, strrpos($ff, '.') + 1)) == 'php')
                {
                    $name = basename($ff, ".php");
                    $flist[] .= $name;                  
                }
            }
        } 
    } 
}
listFolderFiles('content/',array('filelist.php')); 
$removing = array('_en','_es');
$pages = str_replace($removing, "", $flist);
unset($dir, $exclude, $flist);
listFolderFiles('content/Beginner/',array('filelist.php')); 
$removing = array('_en','_es');
$beginner = str_replace($removing, "", $flist);
unset($dir, $exclude, $flist);
listFolderFiles('content/Intermediate/',array('filelist.php', $beginner));
$removingi = array('_en','_es');
$intermediate = str_replace($removingi, "", $flist);
unset($dir, $exclude, $flist);
listFolderFiles('content/Advanced/',array('filelist.php', $beginner));
$removinga = array('_en','_es');
$advanced = str_replace($removinga, "", $flist);

Is there a faster way to execute this code, as it is currently used on every pageload, and I only need to update it when I add a new file.

4

1 回答 1

1

除了算法改进之外,这是一个很好的缓存候选者。看起来它可以像序列化数组并将其放入文件一样简单。然后您可以决定您的到期政策是什么。伪代码:

if(!file_exists(CACHE_NAME) || dir_cache_expired(CACHE_NAME)) {
  // ... code to build $dirlist
  file_put_contents(CACHE_NAME, serialize($dirlist));
} else {
  $dirlist = unserialize(file_get_contents(CACHE_NAME));
}

这样,您总是可以通过简单地删除缓存文件来触发缓存的重建。

于 2012-12-13T19:27:11.400 回答