How to enable the CodeIgniter database cache to automatically expire

Source: Internet
Author: User

The CodeIgniter framework is a very small PHP framework. CI comes with the Database File Cache, but according to the official saying, the cache setting never expires, unless you call the method to actively delete it.

Cache files do not expire. Any queries that have been cached will remain cached until you delete them.

It feels too mentally retarded and inconvenient. Modify the db class and set an expiration time when cache is enabled. The cache automatically becomes invalid upon expiration.

1: replace 1021 rows of cache_on function in CI database/DB_dirver.php

Copy codeCode: function cache_on ($ expire_time = 0) // add parm expire time-Cache expiration time
{
$ This-> cache_expire_time = $ expire_time; // add by kenvin
$ This-> cache_on = TRUE;
Return TRUE;
}

2: Add 90 rows of read function if (FALSE = ($ cachedata = read_file ($ filepath) in CI database/DB_cache.php

Copy codeThe Code is as follows: // determine whether the expiration date exists. // cache_expire_time
If (! File_exists ($ filepath )){
Return false;
}
If ($ this-> db-> cache_expire_time> 0 & filemtime ($ filepath) db-> cache_expire_time ){
Return false;
}

In this way, change from $ this → db → cache_on ();
Copy codeThe Code is as follows: $ this → db → cache_on ($ SEC );
$ SEC indicates the cache expiration time, in seconds. For example, $ this → db → cache_on (60); indicates that the cache expires 60 seconds later.

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.