Saturday 26 January 2013

CodeIgniter Cache Helper


 I am a huge fan of output caching and CodeIgniter has a very effective output caching system, however, it is lacking some very important functions for managing the cache files. CodeIgniter Cache Helper fixes that issue by giving you a simple helper that you can add to your CodeIgniter project that will let you manage your output cache files in code.
For more information on caching in CodeIgniter you should take a look at my article Caching with CodeIgniter: Zen, headaches and performance.

Requirements

  • CodeIgniter 2.0 (It can be modified to work in <1.7.2 by changing “APPPATH” to “BASEPATH”)

Download

I keep the file updated and open in the GitHub Repository.

Installation

Add the cache_helper.php file to your “helpers” directory in the application folder.

Usage

Load the helper any time you want to use one of it’s functions
1
$this->load->helper('cache');
You can then call any of the functions directly. Helper functions do not need to be called through CodeIgniter’s $this object.

Function reference

get_cache_folder()

Returns a string containing the path to the cache directory.
Example return
'/var/www/application/cache'

get_cache_file($uri_string)

Returns a string with the path to the cache file for a specific URI.
Notes
  • Call this function with the URI string of the resource you are looking for (e.g. ‘blog/article/123′). This is designed to be used with CodeIgniter’s uri_string()method from the URI Class.
  • This function does not check for the existence of that cache file, it merely tells you where CodeIgniter would cache the file.
Example return
'/var/www/application/cache/d41d8cd98f00b204e9800998ecf8427e'

get_all_cache_files()

Returns an object containing information for every file in the cache directory.
Notes
Uses the get_dir_file_info() function from the File Helper. The returned object will be in that format.

delete_cache($uri_string)

Attempts to delete the cached file for the specified resource. Returns TRUE upon success or if that file does not exist, returns FALSE on failure.
Notes
  • Call this function with the URI string of the resource you are looking for (e.g. ‘blog/article/123′). This is designed to be used with CodeIgniter’s uri_string()method from the URI Class.

delete_all_cache()

Attempts to delete ALL cache files. Does not return anything.

delete_expired_cache()

Attempts to delete all cache files which have expired. Does not return anything.
Notes
  • This function will iterate through every file in the cache directory and try to read the expiration time from the file. If you have a large cache directory and/or slow I/O then this can be a slow operation.

get_cache_expiration($uri_string)

Attempts to read the expiration time stamp from the cache file for the specified resource. Returns an integer with the time stamp (UNIX time stamp in time() format) if it could find and parse the file, returns FALSE if there is no cache file for the resource or if there was a problem reading/parsing the file.
Notes
  • Call this function with the URI string of the resource you are looking for (e.g. ‘blog/article/123′). This is designed to be used with CodeIgniter’s uri_string()method from the URI Class.

No comments:

Post a Comment