The Include Engine
Include Engine Features
The FileEngine is currently the default cache engine in CakePHP.
In this article I will change the behavior of the FileEngine caching class by sub-classing it to make it able to do the following:
- Creates/reads a cached PHP file (real PHP file) that stores one more variable(s).
- Although IncludeEngine is used for temporary caching (by default), however, adding the key ‘forever’ => true in its configuration will make this cached version “permanent” so the date-time checking is skipped
- The ‘serialize’ key of the configuration is still true by default, and is still recommend (more secure and even faster!) for any non-resource data type variable (int, float, string, boolean, array, object). However, you may ask the IncludeEngine to create a human readable (in our case non serialized) cached version of the variables you want to store (cache). So setting the ‘serialize’ key of the configuration to false is secure for just scalar variables and scalar arrays (int, float string, boolean and arrays that are based on primitive scalar data type
Include Engine Class
Downloaded the Include Engine From http://github.com/Harake
(I personally put the class in the “app\libs\cache” directory, and I name it as “include.php”)
Changes in your own “app\config\bootstrap.php”
You can create more than one configuration for the include engine, but assume that you want only two, you should do something like this
if (floatval(Configure::read('Cake.version')) < 1.3) { include_once APP . 'libs' . DS . 'cache' . DS . 'include.php'; } else { include_once APPLIBS . 'cache' . DS . 'include.php'; } Cache::config('cfg1', array( 'engine' => 'Include', 'path' => CACHE . 'persistent' . DS, 'prefix' => 'CFG1_', 'serialize' => true )); Cache::config('cfg2', array( 'engine' => 'Include', 'path' => CACHE . 'persistent' . DS, 'prefix' => 'CFG2_', 'forever' => true, 'serialize' => true )); Cache::config('default');
Where and how to use it
In general, it can be used anywhere in your application like this
Cache::config('cfg1'); // Comment it for 1.3 or higher
$cached_vars = Cache::read('put here the filename', 'cfg1');
if (empty($cached_vars)) {
//collect the variables with their values before caching them
Cache::write('put here the filename', array('var1_name' => 'var1_value', 'var2_name' => 'var2_value'), 'cfg1');
Cache::config('default'); // Comment it for 1.3 or higher
} else {
Cache::config('default'); // Comment it for 1.3 or higher
//variables are already cached, let us retrieve them
$var1_name = $cached_vars['var1_name'];
$var2_name = $cached_vars['var2_name'];
}
Please leave a comment, If you like this article or have one or more remark(s) on it
Just a simple question? why do I need to use this class? is not FileEngine class enough?
1) The class is useful for caching one or more variables with their values in a file, while FileEngine stores only the value of one variable without the name of that variable.
2) the class is useful when you want to cache permanently a variable and you are sure that this variable will not be changed at any rate, while File Engine stores always the expiration date of the cache even if you are sure that the cached variable is not going to be changed!
I think the second reason is not so much important like the first one.
Currently I am not interested to use the class for production mode, but I am sure for learning purpose it makes me learn how to create your own file caching. thanks
thanks for being interested in some way or another.