How to enable and clear cache in the CodeIgniter framework-PHP source code

Source: Internet
Author: User
Tags codeigniter
Many new users of CodeIgniter framework startup and caching do not know how to perform this operation. Let's take a look at an example of how to enable and clear the cache in CodeIgniter framework, as shown below. Many new users of CodeIgniter framework startup and caching do not know how to perform this operation. Let's take a look at an example of how to enable and clear the cache in CodeIgniter framework, as shown below.

Script ec (2); script

Codeigniter supports the caching technology to achieve the fastest speed. Although CI has been quite efficient, factors such as the dynamic content in the webpage, the memory CPU of the host, and the read speed of the database directly affect the loading speed of the webpage. Depending on the Web cache, your web page can load almost static Web pages because they save the results output by the program to the hard disk.

How does cache work?

CI supports separate cache for each page and allows you to set the cache Update time. When a webpage is loaded for the first time, the cached file is saved to the application/cache folder. During the next visit, the system will directly read the cached file and return it to the user's browser. If the cached file expires, it will be deleted and regenerated.
Note: The Benchmark label is still available on the page that uses the cache.

Start cache

To enable the caching function, you only need to put the following code in the function of any controller:

$ This-> output-> cache (n );

N indicates the number of minutes in which you want to cache updates. You can use m/60 to get accurate to seconds, for example, 1/60. The Code precise to 1 second can be placed in any function. The order of appearance does not affect the cache, so place it where you think it is most logical. Once the above Code is put into the Controller method, the page will be cached. Warning because CI stores cached files, only the view File output can be cached. Note: Before the cache file is generated, make sure that the application/cache folder is writable.

Clear Cache

If you no longer want to use the cache, you only need to delete the above Code from your controller. Note: This will not cause the cached file to disappear immediately. It will automatically expire and be deleted. If you want to delete those files immediately, you must do it yourself.

You can manually set the cache switch. This function is useful if you want to retain some queries that are not cached. For example:

// Turn on the cache Switch
$ This-> db-> cache_on ();
$ Query = $ this-> db-> query ("SELECT * FROM mytable ");
// Disable the following query from being cached
$ 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 ");
Delete cached files and specific webpages. If you need to clear the cache, update your database.

The cache system creates a subdirectory corresponding to the accessed URL in the cache directory, and stores the cached file in that subdirectory. the main directory of the cache is in application/config/database. cache directory set in php. for example, if you are browsing the page with the address example.com/index.php/blog/comments, the cache system will put all generated cache files in a folder named "blog + comments. if you want to delete the cache file corresponding to this example, You need to execute the following code:

$ This-> db-> cache_delete ('blog ', 'comments ');
$ This-> db-> cache_delete ('blog ', 'comments'), which does not take effect during actual testing. I don't know why, and I don't know if it's a small bug? However, the following $ this-> db-> cache_delete_all () is acceptable and there is no problem.

If you do not use any parameters, the current URI settings determine when to clear/update the cache.

Clear all cached files. Example:

$ This-> db-> cache_delete_all ();


Cache note

1. database cache

The database cache is mainly used for SELECT queries.

// Turn on the cache Switch
$ This-> db-> cache_on ();
$ Query1 = $ this-> db-> query ("SELECT * FROM mytable ");

// Disable the following query from being cached
$ This-> db-> cache_off ();
$ Query2 = $ this-> db-> query ("SELECT * FROM members WHERE member_id = '$ current_user '");

// Enable cache again
$ This-> db-> cache_on ();
$ Query3 = $ this-> db-> query ("SELECT * FROM another_table ");


In this way, query1 and query3 are cached in the file. The cache Path depends on your URL, for example, example.com/index.php/blog/commentspage, the cache system puts all generated cache files in a folder named "blog + comments. if you want to delete the cache file corresponding to this example, You need to execute the following code:

$ This-> db-> cache_delete ('blog ', 'comments'); // $ this-> db-> cache_delete ('blog', 'comments ') # To delete the cache if you want to clear all database caches:

$ This-> db-> cache_delete_all (); * The cache mode generates a cache file for different Uris. If the parameters in the URL are different, the cache file will be different, as a result, the vulnerability is generated. If a visitor builds an automatic URI and continuously initiates requests to the server, a large amount of junk files will be generated instantly, resulting in a bloated system file.

2. Page Cache

$ This-> output-> cache (n); // make sure that application/cache write n is the number of minutes you want to update the cache. You can use m/60 to get accurate to the second. For example, 1/60 is accurate to 1 second.
  

3. Sequential cache to files


$ This-> load-> driver ('cache', array ('adapter '=> 'apc', 'backup '=> 'file '));

If (! $ Foo = $ this-> cache-> get ('foo '))
{
Echo 'saving to the cache!
';
$ Foo = 'foobarbaz! ';

// Save into the cache for 5 minutes
$ This-> cache-> save ('foo', $ foo, 300 );
}

Echo $ foo;

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.