Skip to content

Improved Query Caching

World Wide Web Server edited this page Jul 4, 2012 · 30 revisions

[size=6][b][color=red][Edit: This article is still work in progress!][/color][/b][/size]

The way CodeIgniter does Query Caching (namely Controller-based caching) [b]works fine with small and decentralized pages[/b] where all controllers are pretty much independent. But as soon as you have a [b]model that's shared[/b] by a handful of controllers, you [b]end up with a big mess[/b].

Just take a model for generating the data for a tag cloud that's displayed on every page. You would end up with dozens of duplicates and handling those caches would suck as hell.

[url=http://codeigniter.com/forums/viewthread/78146/]See this topic for discussion[/url]

I got pretty sick of this and thus I of re-wrote pretty much CI's entire caching storage mechanisms. Using my code CI now supports [b]several[/b] different ways to cache database queries.

Documentation:

[size=6]1. [b]Controller[/b][/size]

[color=orange][b]Purpose:[/b][/color] For controllers-centric and fairly [b]simple projects[/b] with mostly independent controllers. [color=green][i]This is just what you know from CI 1.6.x.[/i][/color]

[color=orange][b]File Storage:[/b][/color] [color=blue]application / cache_database / [b]controller/ + controller + function + md5hash[/b][/color] [i](Folder is created automatically)[/i]

[color=orange][b]Sample Code:[/b][/color]

[size=4][b]Activating Controller Caches:[/b][/size] [code]<?php

//Create cache for current controller function: $this->db->cache_on(); //alias to controller_cache_on() $this->db->controller_cache_on(); $this->db->controller_cache_on(CLASS,FUNCTION);

//Create cache for current controller: $this->db->controller_cache_on(CLASS);

//Create cache for custom controller: $this->db->controller_cache_on($controller_name);

//Create cache for custom defined controller function: $this->db->controller_cache_on($controller_name, $function_name);

?>[/code]

[size=4][b]Clearing Controller Caches:[/b][/size] [code]<?php

//Delete cache of current controller function: $this->db->cache_delete(); //alias to delete_controller_cache() $this->db->controller_cache_delete();

//Delete cache of current controller $this->db->controller_cache_delete($model_name);

//Delete cache of custom defined controller function: $this->db->controller_cache_delete($model_name, $function_name);

?>[/code]

# # #

[size=6]2. [b]Model[/b][/size]

[color=orange][b]Purpose:[/b][/color] For model-centric and [b]semi-complex projects[/b] with multiple controllers sharing the same model.

[color=orange][b]File Storage:[/b][/color] [color=blue]application / cache_database / [b] model/ + model + function + md5hash[/b][/color] [i](Folder is created automatically)[/i]

[color=orange][b]Sample Code:[/b][/color]

[size=4][b]Activating Model Caches:[/b][/size] [code]<?php

//Create cache for current model function: $this->db->model_cache_on(CLASS,FUNCTION);

//Create cache for current model: $this->db->model_cache_on(CLASS);

//Create cache for custom model: $this->db->model_cache_on($model_name);

//Create cache for custom defined model function: $this->db->model_cache_on($model_name, $function_name);

?>[/code]

[size=4][b]Clearing Model Caches:[/b][/size] [code]<?php

//Delete cache of current model $this->db->model_cache_delete(CLASS);

//Delete cache of current model function: $this->db->model_cache_delete(CLASS,FUNCTION);

//Delete cache of custom defined model: $this->db->model_cache_delete($model_name);

//Delete cache of custom defined model function: $this->db->model_cache_delete($model_name, $function_name);

?>[/code]

# # #

[size=6]2. [b]Database Table[/b][/size]

[color=orange][b]Purpose:[/b][/color] For all of us who want the [b]best possible performance[/b] and [b]most advanced caching management[/b].

[color=orange][b]File Storage:[/b][/color] [color=blue]application / cache_database / [b]table/ + table1 + table2 + table3 + md5hash[/b][/color] [i](Folder is created automatically)[/i]

[color=red][b]Important:[/b][/color] [b]Active Record Queries support automatic table detection[/b] while [b]custom queries require tablenames to be provided manually[/b]. (This might get improved, though)

[color=orange][b]Sample Code:[/b][/color]

[size=4][b]Activating Table Caches:[/b][/size] [code]<?php

//Create cache for queries handling with one single table: $this->db->table_cache_on($table_name);

//Create cache for queries handling with multiple tables: $this->db->table_cache_on(array($table_name_1, $table_name_2));

?>[/code]

[size=4][b]Clearing Table Caches:[/b][/size] [code]<?php

//Delete all caches containing data from a particular table: $this->db->table_cache_delete($table_name);

?>[/code]

# # #

[size=6]2. [b]Key-Value[/b][/size]

[color=orange][b]Purpose:[/b][/color] For a [b]lightweight[/b] but [b]useful[/b] caching option.

[color=orange][b]File Storage:[/b][/color] [color=blue]application / cache_database / [b]key / + key + md5hash[/b][/color] [i](Folder is created automatically)[/i]

[color=orange][b]Sample Code:[/b][/color]

[size=4][b]Activating Key Caches:[/b][/size] [code]<?php

//Create cache for a particular key: $this->db->key_cache_on($key_name);

?>[/code]

[size=4][b]Clearing Key Caches:[/b][/size] [code]<?php

//Delete all caches associated with a particular key: $this->db->key_cache_delete($key_name);

?>[/code]

# # #

Clone this wiki locally