Skip to content

Improved Query Caching

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

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

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]four[/b] different ways to cache database queries.

# # #

[size=4]1. [b]Controller[/b][/size]-based for controllers-centric and fairly [b]simple projects[/b] with mostly independent controllers. [color=green][i](already available in CI 1.6.x)[/i][/color] No further code would be necessary.

Cache files would be stored like this: [color=blue]application / cache_database / [b]controller_based / controller + function / md5hash[/b][/color]

# # #

[size=4]2. [b]Model[/b][/size]-based for model-centric and [b]semi-complex projects[/b] with controllers sharing the same model. [color=red][i](not yet available in CI 1.6.x)[/i][/color]

Cache files would be stored like this: [color=blue]application / cache_database / [b]model_based / model / md5hash[/b][/color]

[color=red]Likely implementation issues with this option:[/color] It would require some method for detecting the name of the model which is running the DB query.

The code for this mechanism would be quite similar to the controller-based approach.

# # #

[size=4][b]3. DBTable[/b][/size]-based for all of us who want the [b]best possible performance[/b] and [b]most advanced caching management[/b]. [color=red][i](not yet available in CI 1.6.x)[/i][/color]

Cache files would be stored like this: [color=blue]application / cache_database / [b]table_based / + table1 + table2 + table3 + md5hash[/b][/color]

[color=red]Likely implementation issues with this option:[/color] It would require some reliable parsing methods for detecting the name of the tables used in the DB query.

# # #

  1. [size=4][b]Key[/b]=>[b]Value[/b][/size]-based for a lightweight but useful caching option. [color=red][i](not yet available in CI 1.6.x)[/i][/color]

Cache files would be stored like this: [color=blue]application / cache_database / [b]key_based / key[/b][/color]

# # #

It even[b]allows geeky users[/b] to use all available methods [b]simultaneously[/b]!

Clone this wiki locally