Welcome Guest, Not a member yet? Register   Sign In
how to purge expired cache files?
#1

I'm hoping to cache some search results like so:
Code:
$this->load->driver('cache', array('adapter' => 'file', 'backup' => 'file'));
if ($this->cache->file->is_supported()) {
  throw new Exception("no file cache!");
}

// blah blah fetch data

// cache the fetched data for 30 days
$this->cache->save($some_cache_id, $mydata, 2592000);

In this case, $some_cache_id could be nearly anything because this id is derived from the search terms entered by the user. If a subsequent user enters the same search, I need to be able to call up those search results so the id must be derived from the user search terms. This introduces a challenge because users can search for pretty much any number of different search strings. This is likely to result in a LOT of cache files being generated. I'm concerned that this may rapidly chew up limited hard drive space on this cloud-hosted server -- I have in fact seen this exact situation occur on another project.

I've been scouring CodeIgniter's system files and it would appear that expired cache files are *only* purged if there is another request for the exact same cache id. This is implemented in the get() function in system/libraries/Cache/drivers/Cache_file.php:
Code:
    /**
     * Get all data
     *
     * Internal method to get all the relevant data about a cache item
     *
     * @param    string    $id    Cache ID
     * @return    mixed    Data array on success or FALSE on failure
     */
    protected function _get($id)
    {
        if ( ! is_file($this->_cache_path.$id))
        {
            return FALSE;
        }

        $data = unserialize(file_get_contents($this->_cache_path.$id));

        if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl'])
        {
            unlink($this->_cache_path.$id);
            return FALSE;
        }

        return $data;
    }

Is there some recommended way to make sure expired cache files get purged periodically? Perhaps the CI devs might consider implementing a probabilistic garbage collection method for purging expired cache files like PHP's session.gc_probability? Or perhaps some way to limit the cache size?

Can anyone suggest a way that I might implement a purge myself?
Reply
#2

@sneakyimp,

What about simply deleting the cache... https://codeigniter.com/user_guide/gener...ing-caches
Reply
#3

(09-12-2018, 01:27 PM)php_rocs Wrote: @sneakyimp,

What about simply deleting the cache... https://codeigniter.com/user_guide/gener...ing-caches

That will not work. I need the other files in the cache! They're cached for a reason.
Reply
#4

If your hosts support it you can setup an automatic process to purge expired cache files on a regular basis. Note: Cache is switched off by default. To enable the cache for your site see help25Confusedite Global configuration
Reply
#5

@sneakyimp,

The link that I recommended to you does allow you to specify what cache to delete.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB