Codeigniter – how to avoid database caching for sessions Codeigniter – how to avoid database caching for sessions codeigniter codeigniter

Codeigniter – how to avoid database caching for sessions


In CodeIgniter caching is an all-or-nothing solution, you can't enable it for individual tables.

There are two options I can think of:

  • don't use CI's generic caching, but use a cache library like http://philsturgeon.co.uk/code/codeigniter-cache
  • hack db_driver.php. On line 265 (CI 1.7.2) it checks if caching is enabled. Alter that line so it doesn't do it when it's a SELECT on the sessions table.


In version up to 2.2.1 there could be simple solution:

in file system/libraries/Session.php

find line with string "$query = $this->CI->db->get($this->sess_table_name);"

  1. before the line put:

    // saving cache_on$cache_on = $this->CI->db->cache_on;$this->CI->db->cache_on = false;
  2. after the line put:

    // restoring cache_on$this->CI->db->cache_on = $cache_on;

This will prevent mysql caching for this exact session query, leaving your cache logic as it is.

Mind that this edit concerns framework system file, so if you upgrade CI to upper version - this hack will be lost.