This is documentation for Kohana v2.3.x.
Status | Draft |
---|---|
Todo | Needs Review |
Query caching allows for speed improvements when multiple, identical queries are run. It caches the results of all queries, so when the next one is run, it uses that cache result instead of querying the database again. Please note that this is only useful for SELECT statements, and INSERT or UPDATE statements may corrupt your cache results.
It works automatically when you have the config option set, and only has one method associated with it.
Please note that this is not a substitute for properly designing your application. Generally this is only needed on very large applications where hundreds of queries are called and it's (nearly) impossible to properly optimize them all.
This method clears the database cache. If you pass an SQL statement as a string parameter, it will only clear the cache for that statement. If you pass TRUE
as a parameter, it will clear the cache for the last run statement, equivalent to $db→clear_cache($db→last_query())
.
$db = new Database(); $query = $db->from('records')->where(array('title' => 'foobar'))->get(); // Work with the results... ... // Now somewhere else I need to run the same query: $query2 = $db->from('records')->where(array('title' => 'foobar'))->get(); // This will grab the results from my cache! ... $status = $db->insert('records', array('id' => 10, 'title' => 'foobar', 'content' => 'foobar')); // Now I inserted a row that has corrupted the cache. I need to clear it before I run the same query: $db->clear_cache(TRUE); // Same as $db->clear_cache($db->last_query()); OR $db->clear_cache(); // This clears the whole cache. //Now I can safely run my query again. $query3 = $db->from('records')->where(array('title' => 'foobar'))->get(); // New results returned and set to my cache.