tutorial on enabling and clearing caching in the CodeIgniter framework

Source: Internet
Author: User
Tags comments codeigniter

CodeIgniter supports caching techniques to achieve the fastest speed. Although CI has been quite efficient, the dynamic content of the Web page, the memory CPU of the host, and the reading speed of the database directly affect the loading speed of the Web page. With Web caching, your Web pages can load nearly static pages because they save the output of the program to the hard drive.

How does the cache work?

CI supports separate caching for each page, and can set cache update times. When a Web page is first loaded, the cached file is saved to the Application/cache folder. The next time you visit, the system will read the cached file directly and return it to the user's browser. If the cached file expires, it is deleted and regenerated.
Note: The benchmark tag is still available on the page that uses the cache.

Start caching

To enable caching, just put the following code in the method (function) of any one of your controllers (Controller):

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

where n is the number of minutes you want the update to be cached. You can use M/60 to pinpoint to seconds, such as 1/60, and the code above can be placed into any function within 1 seconds. The order of his presence has no effect on the cache, so put it where you think it is most logical. Once the above code is placed in the controller's method, the page is cached. Warning: Because the CI stores cached files, only the output from the view file can be cached. Note: Make sure that the Application/cache folder is writable before the cached file is generated.

Clear Cache

If you no longer want to use caching, just remove the above code from your controller. Note: This does not allow the cached file to disappear immediately, and it will automatically expire and be deleted. If you want to delete those files immediately, you must do it yourself.

You can set the cache switch manually. This is useful if you want to keep some queries from being cached. For example:

Turn on the cache switch
$this->db->cache_on ();
$query = $this->db->query ("SELECT * from MyTable");
Make the following query not 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");
Deletes a cached file with a specific Web page. If you need to clear the cache, update your database.

The caching system creates a subdirectory of the URL to be accessed in the cache directory, while storing the cached files in that subdirectory. The cached home directory is the cache directory that you set up in application/config/database.php. For example, if you are browsing a page with an address of example.com/index.php/blog/comments, the caching system puts all the generated cache files into a folder named Blog+comments. If you want to remove the cached file that corresponds to the example mentioned earlier, you need to execute the following code:

$this->db->cache_delete (' blog ', ' comments ');
$this->db->cache_delete (' blog ', ' comments '), I did not work in the actual test, do not know why, do not know is a small bug? But the following $this->db->cache _delete_all () is OK, no problem.

If you do not use any parameters, the current URI setting determines when the cache should be purged/updated.

Clears all cached files. Example:

$this->db->cache_delete_all ();


Cache Small Note

1. Database caching

The database cache is primarily for select queries

Turn on the cache switch
$this->db->cache_on ();
$query 1 = $this->db->query ("SELECT * from MyTable");

Make the following query not cached
$this->db->cache_off ();
$query 2 = $this->db->query ("SELECT * FROM members WHERE member_id = ' $current _user '");

Turn on the cache switch again
$this->db->cache_on ();
$query 3 = $this->db->query ("SELECT * from another_table");


So Query1 and Query3 are cached in the file, the cached path depends on your URL, such as the Example.com/index.php/blog/comments page, the caching system puts all the generated cache files into a blog+ Comments as the name of the folder. If you want to remove the cached file that corresponds to the example mentioned earlier, you need to execute the following code:

$this->db->cache_delete (' blog ', ' comments ');//$this->db->cache_delete (' blog ', ' comments ') #来删除缓存 If you want to clear all database caches:

$this->db->cache_delete_all (); * Its cache mode is for different URIs will generate cache files, if the URL in different parameters, the cache file will be different, resulting in a loophole. If a visitor builds an automatically generated URI and constantly initiates a request to the server, a large number of garbage files are generated instantly, resulting in a bloated system file.

2. Page Caching

$this->output->cache (n); Make sure Application/cache can write n is the number of minutes you want the update to be cached. You can use M/60 to be accurate to seconds, such as 1/60, to 1 seconds.
  

3. Sequencing the cache to a file


$this->load->driver (' Cache ', Array (' Adapter ' => ' APC ', ' Backup ' => ' file '));

if (! $foo = $this->cache->get (' foo '))
{
Echo ' Saving to the cache!<br/> ';
$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.