views:

13

answers:

1

hi. i am writing a cache module in php. it tries to write a cache with a $string+timestamp as a filename. i dont have problem with writing the cache.

the problem is i do a foreach loop to get the cache that i want.

this is the logic that i use for getting the cache

foreach ($filenames as $filename){ 
    if(strstr($filename,$cachename)){//if found 
        if(check_timestamp($filename,time()))
                          display_cace($filename);  
        break;
    } 
}

but when it tries to get and read the cache, it slows the server down. imagine that i have 10000 cache file in a folder, and i need to check for every file in that cache folder.

so how do you think the best way of doing this.

here i explain again, because even me still dont understand my written question.. :D

i write cache file with this format filename_timestamp.. e.g cache_function_random_news_191982899010 in a folder ./cache/

when i want to get the cache, i only pass "cache_function_random_news_" and check recursively on that folder. if i find something with that needle on a file name, display it, and break.

but checking recursively on a 10000 files in a folder is not a good thing yeah? please give me your opinion ok, that would clarify more. thanks.

A: 

Browsers and web servers work around the cache maintenance issue by maintaining an 'index'. You can maintain this index in either a file(binary/text) or a database.

For example:

  1. Whenever you create a new cache file, add a row/entry to the table/file.
  2. Then just use table/file to quickly search for cache-file existence
  3. You can also mark unnecessary/obsolete files using a flag in the record
  4. Then periodically (using a Cron job or some other technique) delete the obsolete cache files.

This approach will greatly improve the performance.

Abhijeet Pathak