Codeigniter cache note

Source: Internet
Author: User
Tags codeigniter

 

Recently, I came into contact with codeigniter's open-source lightweight architecture, which integrates common classes and function functions in development. The cache Summary of codeigniter includes the following:

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

To clear all database caches:

$this->db->cache_delete_all();

* The cache mode generates cache files for different Uris. If the parameters in the URL are different, the cache files are different, resulting in a vulnerability. 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); // ensure that the application/cache is writable.

N is the number of minutes you want to cache updates. 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!<br />';
$foo = 'foobarbaz!';

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

echo $foo;

Additional reading: http://codeigniter.org.cn/user_guide/drivers/caching.html

Http://justcoding.iteye.com/blog/657357

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.