Cache

Koldy offers easy way to cache and work with cached data. To be able to use caching, you need to configure it in configs/cache.php.

Currently available cache drivers are:

DevNull: \Koldy\Cache\Driver\DevNull
Request: \Koldy\Cache\Driver\Request
Files: \Koldy\Cache\Driver\Files
Database: \Koldy\Cache\Driver\Db
Memcached: \Koldy\Cache\Driver\Memcached

Configuration

All cache configuration is located in configs/cache.php and you are able to define more then one cache driver. Every cache driver configuration block must be defined by the following pattern:


'driver-key' => array(
    
'enabled' => true,
    
'driver_class' => '\Path\To\Cache\Driver\Class',
    
'options' => array(
        
// this array will be the constructor of driver_class
    
)
)

If this is the first defined driver in your configs/cache.php, then you can simply use it by calling the static methods in Cache class as in the examples below. If this is not the first driver defined, then you'll need to use it first by calling the driver method in Cache class. Learn more about multiple cache drivers.

If you try to use the cache driver that is disabled, then nothing will happen.

Storing items

Basic usage


Cache::set('key''some value'); // will cache the value for default duration and will return true if value is successfully stored

Storing item to cache with custom duration


Cache::set('key''some value'time() + 3600*24); // cache for one day

It is possible to store array or object instead of string or integer, but be aware that serializing and unserializing complex arrays or object can be time expensive.
Arrays and objects won't be serialized or unserialized if cache engine supports that feature.

Storing item forever (without expiration time)


Cache::setForever('key''some value');

Be aware that your value might still be deleted according to your cache driver way of work.

Storing item if key doesn't exists


Cache::add('name''value'); // returns true if key is set

Getting stored items


Cache::get('name'); // returns value or null if key doesn't exists or cache is disabled

Checking items


Cache::has('key');

Deleting items


Cache::delete('key');

Get key or set value

This is probably the method you'll use the most. In your project, you'll always want to fetch something from cache or set something in cache if item doesn't exists or item expired.


$cachedValue Cache::getOrSet('key', function() {
    
// do whatever, probably query database
    
return $value;
});

$cachedValue Cache::getOrSet('key', function() {
    return 
rand(1,1000);
}, 
time() + 3600*24*10); // hold cache for 10 days

Increment or decrement

If you stored number value into cache, you can easily increment it or decrement it.


Cache::increment('key'); // will increment number value for 1
Cache::increment('key'5); // will increment for 5

Cache::decrement('key'); // will decrement number value for 1
Cache::decrement('key'5); // will decrement for 5

Multiple cache engines

You're able to work with more types of cache engines simultaneously just by calling driver method and passing the key from configuration.


Cache::driver('my-memcache')->set('key''value');
Cache::driver('my-memcache')->delete('key');
Cache::driver('your-something')->has('key');

If you want to work with default cache driver (the first one defined), then you can simply use just static methods in Cache class.


Cache::set('name''value');
Cache::get('name');
// and etc...

Creating your own cache driver

To create your custom cache driver, you need to:

• Create class that extends \Koldy\Cache\Driver\AbstractDriver
• Set cache driver_class to your new class in configs/cache.php
• And thats it. Simple as that.

JSONLog