Question

I am using CakePHP 1.3 and trying to enable cache for view pages, cache system works fine and caches all pages. But when we add a new post (insert new record to database) or edit an old one (update a record of the table) CakePHP deletes all cached pages, not just the edited page!

app/config/core.php :

Cache::config('default', array('engine' => 'File','duration' => 8640000));

app/controllers/articles_controller.php :

var $helpers = array('Cache');
var $cacheAction = array(
    'view' => array('duration' => 8640000), 
    'latest' => array('duration' => 8640000), 
);

How can I tell Cake to delete just the cached version of changed page and not all cached pages?

Was it helpful?

Solution

This it actually pretty hard, so I can't just give you a piece of code to solve this. You need to edit the actual cake files in the lib folder that manage caching. Note: this is super not recommended by the cake people. However the lib/Cake/Cache/Engine/FileEngine.php is the file that has the operations of the file engine. You seem interested in the delete function:

/**
 * Delete a key from the cache
 *
 * @param string $key Identifier for the data
 * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
 */
public function delete($key) {
    if ($this->_setKey($key) === false || !$this->_init) {
        return false;
    }
    $path = $this->_File->getRealPath();
    $this->_File = null;

    //@codingStandardsIgnoreStart
    return @unlink($path);
    //@codingStandardsIgnoreEnd
}

Also, instead of editing the core cake files you could add your own file engine and use parted of the cake engine by moving the code and just extending the code there (that's totally cool in open source).

Its also possible that by reading the code used to implement the file caching engine you will find your actual solution. Good Luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top