0

I have a SettingsSiteTable object with the following method for reading settings in the database and storing the results in a cache.

// GET ALL CONFIG SETTINGS
function getConfigs(){

    if(($settings_site = Cache::read($this->key)) === false) {
        $settings_site = $this->find('list', [
            'keyField' => 'key',
            'valueField' => 'value'
        ])->toArray();
        Cache::write($this->key, $settings_site, 'settings');
    }

    return $settings_site;
}  // END GET CONFIGS FUNCTION

$this->key is 'SettingsSite' and there appears to be no issue with that. I have also added use Cake\Cache\Cache to the table object file.

My cache config in the app.php file is as follows:

'Cache' => [
    'default' => [
        'className' => 'File',
        'path' => CACHE,
    ],
    'settings' => [
        'className' => 'File',
        'duration' => '+6 hours',
        'path' => CACHE . 'settings/',
    ],
    '_cake_core_' => [
        // ...
    ],
    '_cake_model_' => [
        // ...
    ],
],

The cache appears to save successfully. I am able to view the cache file located at tmp/cache/settings/settings_site (not sure what the file extension is)

However, if I change the data in the database and refresh the page, the updated information is displayed meaning it is not reading from the cache correctly and it is re-querying the results.

What am I doing wrong? I got this code directly from the 3.X Cookbook: Writing to a Cache

bowlerae
  • 924
  • 1
  • 14
  • 37
  • So I think it has something to do with the face that I'm caching the result set after the query has already been executed...I think I'm on to something. Except now I get an error message "You cannot serialize or unserialize PDO instances"...still in need of help – bowlerae Nov 02 '15 at 15:49
  • Ok NOW I think it has to do with the fact that I don't specify the config in the Cache::read() so it is using default, but I'm writing to 'settings' – bowlerae Nov 02 '15 at 16:05

1 Answers1

2

Silly me I realized I left out the config option during Cache:read() so it was reading from the default config but I was writing to the 'settings' config.

// GET ALL CONFIG SETTINGS
function getConfigs(){

    if(($settings_site = Cache::read($this->key, 'settings')) === false) {
        $settings_site = $this->find('list', [
            'keyField' => 'key',
            'valueField' => 'value'
        ])->toArray();

         Cache::write($this->key, $settings_site, 'settings');
    }


    return $settings_site;
}  // END GET CONFIGS FUNCTION
bowlerae
  • 924
  • 1
  • 14
  • 37