Nishchay supports two caching servers memcached and redisdb. To make caching work caching server and its extensions must be installed.
Settings related to cache are contained in
Cache instance of any config can get using
Nishchay::getCache method. This method accepts one optional parameter.
Above will return instance of cache handler of default config. To get instance of specified config pass config name:
All of the methods of cache instance are same for both cache server and it also works same.
To update or add an item into cache call
set method. This method has following parameters:
|key||Key for the item|
|expiration||Expiration time in seconds for the item. Default is zero(No to expire).|
Nishchay::getCache()->set('key', 'value', 1000);
To add multiple items at a time call
setMulti. This method returns count of item which has been added or updated in cache. Each element of an items must be in
expiration order. We can omit expiration value to make item not to expire.
Nishchay::getCache()->setMulti([ ['key1', 'value1', 1000] # Add key1 with value1 having expiration of 1000 seconds ['key2', 'value2'] # Add key2 with value2 having no expiration ]);
add item only if it does not already exists in cache:
Nishchay::getCache()->add('key', 'value', 1000);
Parameter of this method are same as
get method we can get an item from cache, this method returns
null if there's no item for the passed key.
$item = Nishchay::getCache()->get('key');
We can also get multiple items at item. If there's no item for the key it returns
null for it:
$items = Nishchay::getCache()->getMulti(['key1', 'key2']);
To remove an item from cache, call
remove method with key. It returns true, if item has been removed from cache.
Remove multiple items
To remove multiple items at a time pass list of keys which need to be removed in
Replace method works same as
set method but method does not adds item if it does not exists. It returns
false if item does not exists in cache:
Nishchay::getCache()->replace('key', 'value', 1000);
Notes are visible to you only and its for your use only. Its good to note a point of what you read.