CodeIgniter-MY_Model icon indicating copy to clipboard operation
CodeIgniter-MY_Model copied to clipboard

Cache does not reset for subsequent queries.

Open louiswalch opened this issue 8 years ago • 6 comments

If you call a model with set_cache, subsequent calls to that same model without caching will return the original cached result. The cache name should clear after query is complete.

Example:

$all = $this->test_model->set_cache('all_records')->get_all();

$just_one = $this->test_model->get(1);

In this example, $just_one will contain all records (the cached result of the first query). The only way to get around this appear to be once you start calling a model with set_cache, then every other query after that has to called with a unique set_cache as well. This is undesirable, as it would be nice to be able to cache only certain queries.

louiswalch avatar Aug 01 '16 18:08 louiswalch

Same issue so cache is unusable. Tried to use resetcache but this delete all cache files. In complement of precedent post


$all = $this->test_model->set_cache('all_records')->get_all();
$somethingelse = $this->something->get() or get_all();
$just_one = $this->test_model->get(1);

Provides the same result, $just_one = $all

stefanKBL avatar Dec 22 '16 09:12 stefanKBL

Please, @skobeo give it a try now...

avenirer avatar Dec 22 '16 14:12 avenirer

@avenirer thanks. At the first execution (with empty cache) it's ok. But at the second execution (caching the first query but not the second one) , got an error "Maximum function nesting level of '256' reached". If a cache both queries, it's ok. exact context is content model with translation (one) translation model with content (one)

$this->data['static_pages'] = $this->content_model
            ->where(array('content_type'=>'page','published'=>1))
            ->with_translation(array('fields'=>'title,url','where'=>'language_slug=' . $this->db->escape($this->data['lang_slug'])))
            ->set_cache('get_static_pages_' . $this->data['lang_slug'], 360000)
            ->get_all();
$content = $this->content_translation_model
                ->where(array('language_slug'=>$this->data['lang_slug'], 'url'=>$url))
                ->with_content('fields:published, created_by, published_at')
                //->set_cache($url, 360000)
                ->get();

If uncomment cache, ok. Else, try more than 256 so error. Trying to understand what happens by putting some log in the pre_after_read function which seems to be called a lot of time by the get function but no way for explanation. Row returned in the get function is correct (One row of course).

stefanKBL avatar Dec 23 '16 09:12 stefanKBL

Did you put the $content (the get() query) inside a loop?

avenirer avatar Dec 23 '16 10:12 avenirer

No. simple get() query. When i log $data passed to _prep_after_read function(), datas from the first query are ok, only one result coming from $this->content_translation_model->where(...).

But datas coming from the second query (with_content) loop :

  • One is the result of with_content
  • Next in the content of the with_translation called and cached by the first set of queries ($this->data['static_pages'])
  • Next is the result of with_content
  • etc.

stefanKBL avatar Dec 23 '16 12:12 stefanKBL

Any news on this, I'd love to use caching but this is blocking me.

louiswalch avatar Feb 16 '17 18:02 louiswalch