
July 29th, 2003
10:32 PM
with Mr. Jones
Status: Offline!
<?php
# page.php
if( filemtime("/path/to/directory/.") <= filemtime('cache.csx') ) {
$files = unserialize(file_get_contents('cache.csx'));
} else {
# old/bad cache
echo('Rewriting Cache!');
# read dir
$handle = opendir('dirname');
while(false !== ($File = readdir($Handle))){
$fileArray[] = $File;
}
# store serialize() dir in cache.csx
$h = fopen($filename, 'w+')
fwrite($h, serialize($fileArray));
# set files array to $files
$files = $fileArray;
}
# now you have dirlist stored in $files
foreach($files as $file) {
print $file;
}
?>
___________________
http://www.philbrodeur.com - Expert PHP Development and Tutorials
Last edited by Phil, July 29th, 2003 10:51 PM (Edited 1 times)

July 29th, 2003
10:40 PM
Neversidian
Status: Offline!
well if you use a php upload script you can just an element to the serialized array when you upload it. or you could have it run once a day with a date('dmy') as the time. if the current day doesnt match the stored day scan the directory.
___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!

July 29th, 2003
10:43 PM
Ares, you just create a cache file by writing to cache.csx?
___________________
Current Project: LivermoreMuscle.com
Sig Hosted by Motorspin

July 29th, 2003
10:53 PM
with Mr. Jones
Status: Offline!
If its on w+, yeah, it'll create it if it doesnt exist. It might cause errors the first time - i dont now how filemtime handles nonexistant files.
as for the cache file... its basically a text file with a serialized array written it it. I just put a cool extension on it to help it stand out among other files - personal preference; it would work as .txt, .php etc
___________________
http://www.philbrodeur.com - Expert PHP Development and Tutorials

July 29th, 2003
10:57 PM
i though were actually CACHING someting, no just writing some junk to a file.
___________________
Current Project: LivermoreMuscle.com
Sig Hosted by Motorspin

July 29th, 2003
11:36 PM
with Mr. Jones
Status: Offline!
no, this is fake caching. I dont know how to do real caching; do you have any links?
you could also put it in database
___________________
http://www.philbrodeur.com - Expert PHP Development and Tutorials

July 30th, 2003
12:21 AM
TF Retiree
Status: Offline!
Nah I have bad luck with databases. *goes off to try the code*

July 30th, 2003
05:31 AM
Would someone like to outline the difference between "fake" and "real" caching for me?
If you're preventing the server from executing unnecessary code on script execution by retrieving pre-calculated values, are you not using the principle of caching?
___________________
Adam Goossens -- PHP is my mother tounge.
Linux: ( kernel.org | winehq ) -- f33l the p0w3r.
Nobody replying to your questions? Getting flamed? Getting told to RTFM? Ask your questions the right way.
Last edited by Jeb, July 30th, 2003 05:34 AM (Edited 1 times)

July 30th, 2003
09:24 PM
with Mr. Jones
Status: Offline!
Jeb... I was thinking about this in the shower. Caching means its actually stored on the users machine - thats REAL caching. I figure you could do this with a PHP script by storing when you alst added info and then mess around with last-modified headers, but I havent tried it.
I use 'fake caching' to take the load off the database or the the processer. It could be storing arrays that don't change often in a text file, or storing templates in files vs database...
thats my take on it.
___________________
http://www.philbrodeur.com - Expert PHP Development and Tutorials

July 31st, 2003
11:16 AM
Mmm hmm....
I don't think there is a real and fake caching. To cache something is to buffer data so that it can be accessed quicker than normal at a later time, and be periodically updated to reflect any changes to the source data.
But maybe I've misunderstood the definition....
___________________
Adam Goossens -- PHP is my mother tounge.
Linux: ( kernel.org | winehq ) -- f33l the p0w3r.
Nobody replying to your questions? Getting flamed? Getting told to RTFM? Ask your questions the right way.
Last edited by Jeb, July 31st, 2003 11:23 AM (Edited 1 times)