[CodeIgniter five], query cache

Source: Internet
Author: User
Tags codeigniter
Database Cache Classes

The database Cache class allows you to save database query results in a text file to reduce database access.

Important

When the cache is enabled, this class is automatically loaded by the database driver and should not be loaded manually.

Important

Not all query results can be cached, please read the contents of this page carefully.

Enable caching

Enabling caching requires three steps:

    • Create a writable directory on the server to save the cache file;
    • Set its directory path through the Cachedir parameter in the file application/config/database.php;
    • By setting the cache_on parameter in file application/config/database.php to TRUE, you can also configure it manually using the following method.

Once the cache is enabled, each time the page is loaded, it is automatically cached whenever the page contains a database query.

How does the cache work?

When you access the page, the CodeIgniter query cache 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 loads for the first time. The next time you visit the page, the cache file will be used directly instead of accessing the database, so that your database access will drop to 0 on the cached page.

Only queries of the read type (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.

The cached files never expire, and all queries are cached until you delete them and will always be available. You can delete the cache for a specific page, or you can erase all the caches. In general, you can use the following function to clear the cache when certain events occur, such as when you add data to a database.

Can caching improve the performance of your site?

Whether the cache can gain performance gains depends on many factors. If you have a low-load and highly optimized database, you may not see a performance boost. And if your database is being accessed massively, you might see an increase in post-cache sex, provided your filesystem doesn't have much overhead. One thing to keep in mind is that caching simply changes the way data is fetched, 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 you need to cache your database, which depends entirely on your situation.

How is the cache file stored?

CodeIgniter caches each query into its own cache file, which is further organized into its own subdirectories, based on the called controller method. To be more precise, subdirectories are named using 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 cache system creates a directory named Blog+comments and generates three cache files in that directory.

If your URI contains dynamic queries (for example, when paging is used), each query instance generates its own cache file, so that the number of cached files may end up being several times the number of queries on your page.

Manage your cache files

Since the cache file does not expire, there should be a mechanism to delete the cache in your application, for example, let's say you have a blog and allow users to comment, and whenever a new comment is submitted, you should delete the cache file that corresponds to the Controller method that displays the comment. Two different ways to delete cached data are described below.

Not all database methods are cache-compatible

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 that are not available.

The methods listed below are not available on cached result objects:

    • Num_fields ()
    • Field_names ()
    • Field_data ()
    • Free_result ()

Also, the two IDs of result_id and conn_id are not available because these two IDs only apply to real-time database operations.

Function reference

$this->db->cache_on ()/$this->db->cache_off ()

For manually enabling/disabling caching, 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 of F for this one Query$this->db->cache_off (), $query = $this->db->query ("SELECT * from the 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 the cache file for a specific page, which is useful when you need to clear the cache after updating your database.

The cache system writes the cache to the appropriate cache file based on the URI of the page you visit, for example, if you are accessing the Example.com/index.php/blog/comments page, the cache file will be saved to the Blog+comments directory. , to delete these cache files, you can use:

$this->db->cache_delete (' blog ', ' comments ');

If you do not provide any parameters, the cache file corresponding to the current URI will be cleared.

$this->db->cache_delete_all ()

Clear all the cache files, for example:

$this->db->cache_delete_all ();

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the [CodeIgniter five], query cache, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.