[Codeigniter 5]. query cache

Source: Internet
Author: User
Tags codeigniter delete cache
: This article mainly introduces [codeigniter 5] and query cache. For more information about PHP tutorials, see. Database cache

The database cache class allows you to save database query results in text files to reduce database access.

Important

When the cache is enabled, this class will be automatically loaded by the database driver. do not manually load it.

Important

Not all query results can be cached. read this page carefully.

Enable cache

Three steps are required to enable caching:

  • Create a writable directory on the server to save the cached files;
  • Set the directory path through the cachedir parameter in the application/config/database. php file;
  • Set the cache_on parameter in the application/config/database. php file to TRUE. you can also manually configure it using the following method.

Once the cache is enabled, every time a page is loaded, it is automatically cached as long as the page contains database queries.

How does cache work?

When you access the page, CodeIgniter's query cache system runs automatically. If the cache is enabled, when the page is loaded for the first time, the query result object will be serialized and saved to a text file on the server. When you access this page next time, you will directly use the cached file instead of accessing the database. in this way, your database access will be reduced to 0 on the cached page.

Only query of the Read type (SELECT) can be cached, because only such query produces results. Write-type queries (INSERT, UPDATE, etc.) do not generate results, so they are not cached.

Cached files will never expire. all queries will be available until they are cached and you delete them. You can delete the cache for a specific page or clear all the caches. In general, you can use the following functions to clear the cache when some events occur (such as adding data to the database.

Can cache improve the performance of the site?

Whether the cache can obtain performance gains depends on many factors. If you have a low-load and highly optimized database, you may not see performance improvement. If your database is being accessed in large quantities, you may see an improvement in the cache performance, provided that your file system does not have much overhead. Remember that the cache simply changes the way data is obtained, from accessing the database to accessing the file system.

For example, in some cluster server environments, caching is harmful because file system operations are too frequent. In a shared single server environment, caching can be beneficial. Unfortunately, there is no unique answer to the question about whether to cache your database. it depends entirely on your situation.

How are cached files stored?

CodeIgniter caches each query to a separate cache file. the cache file is further organized into sub-directories according to the called controller method. More accurately, sub-directories are named using the first two sections of your URI (controller name and method name.

For example, you have a blog controller and a comments method, which contain three different queries. The cache system creates a directory named blog + comments and generates three cached files under the Directory.

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

Manage your cached files

Because the cached file will not expire, your application should have a mechanism to delete the cache. for example, we assume that you have a blog and allow users to comment. whenever a new comment is submitted, you should delete the cache file corresponding to the controller method for displaying comments. There are two different methods to delete cached data.

Not all database methods are cache-compatible.

Finally, we must point out that the cached result object is only a simplified result object. as a result, several query results cannot be used.

The methods listed below cannot be used on cached result objects:

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

At the same time, the two IDs result_id and conn_id cannot be used, because these two IDs are only applicable to real-time database operations.

Function Reference

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

This method is used to manually enable/disable caching. These two methods are useful when you do not want to cache some 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 cache files on a specific page. this is useful when you need to clear the cache after updating your database.

The cache system writes the cache to the corresponding cache file based on the URI of the page you visit. for example, if you access the page example.com/index.php/blog/comments, the cache system saves the cached files to the blog + comments Directory. to delete these cached 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 cached files, for example:

$this->db->cache_delete_all();

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The preceding section describes [codeigniter 5] and the query cache, including some content. I hope my friends who are interested in the PHP Tutorial will be helpful.

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.