Welcome Guest, Not a member yet? Register   Sign In
Khaos :: KhCache
#1

[eluser]Neophyte[/eluser]
Updated - 26/03/2008 - Download - Khaos::KhCache-0.3

Introduction
KhCache is a pretty basic caching library currently with two containers (File and APC), the library is designed to complement codeigniters own caching system by allowing you to cache the output of smaller segments of code when no full page cache is available for CI to directly display.

Quick Reference
Code:
/*
* KhCache
*
* mixed  fetch ( string $key)
* bool   store ( string $key, mixed $data [, int $ttl] )
* mixed  call ( callback $func [, array $args [, int $ttl ]] )
* bool   delete ( string $key )
* bool   delete_all ( )
* string generatekey ( mixed $arg1  [, mixed $arg2  [, mixed $...  ]] )
* int    get_hits ( )
* int    get_misses ( )
*/

// Example General Usage
if (($data = $this->khcache->fetch('test')) === false)
{
    /*
     * Resource intensive code here which places data to be
     * cached within the $data variable.
     */
            
    // Save Cache
    $this->khcache->store('test', $data);
}

// Example Function Caching
$data = $this->khcache->call('transform_xsl', array($xsl, $xml));
Configuration
KhCache will work right out the box aslong as the system/cache folder is writable. However if you wish to change the behaviour of KhCache then create the config file khaos.php.

Below is a sample config which represents the default behaviour of KhCache.
Code:
$config['cache']  = array('container' => 'File',
                           'ttl'       => 3600,
                           'File'      => array('store'           => BASEPATH.'cache/',
                                                'auto_clean'      => 10,
                                                'auto_clean_life' => 3600,                        
                                                'auto_clean_all'  => false));
everything in the config file is optional so options only need to be specified if you wish to override the default behaviour. for example if you wished to use APC then the config would simply be
Code:
$config['cache'] = array('container' => 'APC');
auto_clean - By default there is a 1 in 10 chance of the cache dir being cleaned, set to false to disable.
auto_clean_life - Files older than this will be removed.
auto_clean_all - By default only cache files prefixed with khcache_ are removed set to true to have khcache clean the entire directory
#2

[eluser]wiredesignz[/eluser]
Once again, really nice code Neophyte. Thanks Wink
#3

[eluser]Neophyte[/eluser]
ChangeLog - KhCache - 0.2

Feature - new method 'call' to make function caching easier
Feature - auto cleaning when using File container (based on the PEAR::Cache_Lite approach)
#4

[eluser]Tom Glover[/eluser]
Nice and simple. The Best way to code ;-) .
#5

[eluser]Neophyte[/eluser]
ChangeLog - KhCache - 0.3

Feature - new method 'delete_all' to clear all cache items
#6

[eluser]BorisK[/eluser]
This is pure gold. CI and EE should come with granular caching by default. I don't understand how people run large sites without granular caching and expiration.

Your use of containers is great. Leaves room for other things like memcaching or even DB caching drivers.

Thank you for sharing.
#7

[eluser]Majd Taby[/eluser]
You have a typo in the comments, it's "instantiate", not "instanciate" Tongue
#8

[eluser]Neophyte[/eluser]
I'll consider changing it in the next release, no promises though Tongue
#9

[eluser]Adaptive[/eluser]
On one of my codeigniter sites I use different views based on the user IP address. I can only use db cache, on specific queries. Is there a good way to use KhCache?
#10

[eluser]louis w[/eluser]
This looks very interesting, thanks for posting this Neophyte.

I understand this is a new project, but I would like to see a little more documentation.

Can this be called from a View? I think it would be nice to be able to cache your view as the final output but also be able to cache pieces of the view at different cache times to save on other processing.




Theme © iAndrew 2016 - Forum software by © MyBB