Database Cache Classes
The database Cache class allows you to save database query results in a text file to reduce database access.
Important
When caching is enabled, this class is automatically loaded by the database driver, not manually.
Important
Not all query results can be cached, please read the contents of this page carefully. Enable caching
Enabling caching requires three steps: Creating a writable directory on the server to save the cached file, setting its directory path through the Cachedir parameter in the file application/config/database.php, by application/config the file The cache_on parameter in/database.php is set to TRUE or can be manually configured using the following method.
Once the cache is enabled, every time the page is loaded, whenever the page contains a database query, it is automatically cached. how the cache works.
When you visit the page, the CodeIgniter query caching system runs automatically. If the cache is enabled, the query result object is serialized and saved to a text file on the server when the page is first loaded. The next time you visit the page, you will use the cached file directly instead of accessing the database, so that on the cached page, your database access will be reduced to 0.
Queries that have only read types (SELECT) can be cached because only such queries produce results. Write-type queries (INSERT, UPDATE, and so on) do not generate results, so they are not cached.
Cached files never expire, and all queries will remain available until you delete them. You can delete the cache for specific pages, or you can empty all the caches. In general, you can use the following function to clear the cache when certain events occur, such as adding data to the database. can caching improve the performance of your site?
The ability of caching to gain performance depends on a number of factors. If you have a low load and highly optimized database, you may not see performance improvements. And if your database is being accessed heavily, you may see a boost in cache sex If your file system doesn't have much overhead. One thing to keep in mind is that caching simply changes the way data is obtained, from accessing the database to accessing the file system.
For example, in some clustered server environments, caching is actually harmful because the file system is operating too frequently. Caching can be beneficial in a shared, single server environment. Unfortunately, there is no single answer to the question of whether or not you need to cache your database, and it all depends on your situation. how the cached files are stored.
The CodeIgniter caches each query into its own cache file, which is further organized into its own subdirectories according to the controller methods that are invoked. More precisely, the subdirectory is named after the first two paragraphs of your URI (Controller name and method name).
For example, you have a blog controller and a comments method that contains three different queries. The caching system creates a directory named Blog+comments and generates three cached files in the directory.
If your URI contains a dynamic query (for example, when you use pagination), each instance of the query generates its own cached file, so the number of cached files may end up being several times the number of queries on your page. Manage your cached files
Since cached files do not expire, you should have a mechanism for removing caching in your application, for example, we assume you have a blog and allow users to comment, and whenever you submit a new comment, you should remove the cached file for the Controller method that displays the comment. Here are two different ways to delete cached data. not all database methods are compatible with caching
Finally, we have to point out that the cached result object is just a simplified version of the result object, because of this, there are several methods of query results can not be used.
The methods listed below are not available on cached result objects: Num_fields () field_names () Field_data () Free_result ()
Also, the two IDs for result_id and conn_id are not available because these two IDs apply only to real-time database operations. Function Reference $this->db->cache_on ()/$this->db->cache_off ()
Used to enable/disable caching manually, these two methods are useful when you do not want to cache certain queries. Example:
Turn caching on
$this->db->cache_on ();
$query = $this->db->query ("SELECT * from MyTable");
Turn caching off for this one query
$this->db->cache_off ();
$query = $this->db->query ("SELECT * FROM members WHERE member_id = ' $current _user '");
Turn caching back on
$this->db->cache_on ();
$query = $this->db->query ("SELECT * from another_table");
$this->db->cache_delete ()
Delete cached files for a particular page, which is useful when you need to clear the cache after you update your database.
The caching system writes the cache to the appropriate cache file according to the URI of the page you are accessing, for example, if you are accessing the Example.com/index.php/blog/comments page, the caching system will save the cached file to the Blog+comments directory , to remove these cached files, you can use:
$this->db->cache_delete (' blog ', ' comments ');
If you do not provide any parameters, the cached file corresponding to the current URI will be cleared. $this->db->cache_delete_all ()
Clears all cached files, for example:
$this->db->cache_delete_all ();