Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for purging stale/expired cache #106

Open
jamieburchell opened this issue May 14, 2018 · 6 comments
Open

Support for purging stale/expired cache #106

jamieburchell opened this issue May 14, 2018 · 6 comments

Comments

@jamieburchell
Copy link

It seems that there's no mechanism in place to purge old/expired cache entries. I'm using the Doctrine file system storage adaptor and I've run in to a situation where the disk is filling up. I don't want to simply purge everything, because some of the cache files will still be valid.

What's the best way to implement something that can do this?

@catalinux
Copy link

Wouldn't be more eficient to mount tmpfs on that folder? It would be also faster.

@jamieburchell
Copy link
Author

Possibly, but I don't think that helps with the original problem - deleting expired cache files

@levsoroka
Copy link

it may be possible to do a cron job that would remove files past certain date.

@jamieburchell
Copy link
Author

jamieburchell commented Mar 12, 2019

it may be possible to do a cron job that would remove files past certain date.

Indeed. I came up with this inspired by how Doctrine file cache works; it's not pretty but it does the job.

$files = new \RecursiveIteratorIterator(
	new \RecursiveDirectoryIterator('/path/to/cache', \FilesystemIterator::SKIP_DOTS),
	\RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($files as $file) {

	if (is_dir($file)) {
		continue;
	}

	$resource = fopen($file, 'r');
	$lifetime = -1;
	$line = fgets($resource);

	if ($line !== false) {
		$lifetime = (int) $line;
	}

	if ($lifetime !== 0 && $lifetime < time()) {
		fclose($resource);
		@unlink($file);
	}
}

@tkhamez
Copy link

tkhamez commented Jun 29, 2019

The lifetime will often be 0, see CacheEntry::getTTL(), so you need to read the cached element:

    if ($lifetime !== 0 && $lifetime < time()) {
        fclose($resource);
        @unlink($file);
    } else {
        $data  = '';
        while (($line = fgets($resource)) !== false) {
            $data .= $line;
        }
        fclose($resource);

        $cache = unserialize(unserialize($data));
        if ($cache instanceof CacheEntry && ! $cache->isFresh()) {
            @unlink($file);
        }
    }

@jamieburchell
Copy link
Author

It would be nice to have something ready made for this, or being able to utilise the existing methods rather than duplicating most of the code from them and rolling our own thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants