user4271704 user4271704 - 1 year ago 72
PHP Question

How to set an expiry for a cache file?

I am wondering how cache systems like doctrine-cache or zend-cache, set an expiry for cache files if those files are not in /tmp folder. How is possible to set an expiry for cache files? If I want to use my own cache file system with php rather than using doctrine or zend, how can I set an expiry for it if I don't want to place it in /tmp folder?

Answer Source

If statement in another answer is wrong. It must be:

if (file_exists($file) && time()-filemtime($file) > $expire){


if (file_exists($file) && (filemtime($file) > (time() - $expire )){

Regarding your comment in another answer, rename() or any other function doesn't check whether a file is locked, you need to acquire lock with flock() before writing and you must check with the same in any other scripts of your product.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download