-
Notifications
You must be signed in to change notification settings - Fork 0
Improved Query Caching
[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.
[b]# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #[/b]
[size=4][color=red][b]Note: [/b][/color][/size]
- Functions ending with [b]"cache_single(…)"[/b] require parameters and run [b]$this->db->cache_off()[/b] automatically once a query has been sent.
- Functions ending with [b]"cache_on(…)"[/b] require no parameters (they gather all necessary data themselves). They need to be turned of manually by running [b]$this->db->cache_off()[/b].
[b]# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #[/b]
[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]Where to use it:[/b][/color] In [b]Controllers[/b].
[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() //or better: $this->db->controller_cache_on();
?>[/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 custom defined 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]
[b]# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #[/b]
[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]Where to use it:[/b][/color] In [b]Models[/b].
[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: $this->db->model_cache_single(CLASS);
//Create cache for current model function: $this->db->model_cache_single(CLASS,FUNCTION);
//Create cache for custom model: $this->db->model_cache_single($model_name);
//Create cache for custom defined model function: $this->db->model_cache_single($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]
[b]# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #[/b]
[size=6]3. [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]Where to use it:[/b][/color] Wherever you need to.
[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_single($table_name);
//Create cache for queries handling with multiple tables: $this->db->table_cache_single(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]
[b]# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #[/b]
[size=6]4. [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]Where to use it:[/b][/color] Wherever you need to.
[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_single($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);
?