Documentation
Cache: Introduction
Cygnite provides a api for various caching system. Cache allow you to faster access of frequently used data. You may use caching system when large amount of code piece always returns you same data. By default, Cygnite is configured to use the file cache driver, which stores the json serialized data in the filesystem in public/storage/cache/ folder. For larger applications, it is recommended that you use an memory based caching system, such as Memcached or APC.
Cygnite provides you popular APC, Memcached, File caching api for caching data.
- i. File Cache
- ii. APC Cache
- iii. MemCache
- iii. Memcached
- iii. Redis Cache
Configurations
The cache configuration is located at src/Apps/Configs/application.php. In this file you may specify cache driver settings which will be used throughout the application. You may use any caching mechanism on the fly, just need to give the type in factory method. Default file caching mechanism stores cache files in public/storage/cache/ with extension .cache. You can change the name, path, and the extension of the cache file.
Using File Cache: Storing Data In The File Cache
You can store data in file cache by using factory object as below. For example-
use Cygnite\Cache\Factory\Cache;
$file = Cache::make('file', function ($file)
{
$file->store('name', 'Cygnite PHP Framework');
return $file;
});
Storing Data In The File Cache With Expire Time
You can also set expire time for file cache, meaning that cache will be destroyed after the expire time.
use Cygnite\Cache\Factory\Cache;
$file = Cache::make('file', function ($file)
{
$file->store('name', 'Cygnite PHP Framework', $expireTime);
return $file;
});
Storing Data In Different File: Naming Cache
By default store() method will save all your cached information into single file. But if you wish to store data into different file based on the name you provide, then you may follow below example.
use Cygnite\Cache\Factory\Cache;
$file = Cache::make('file', function ($file)
{
$file->as('some-name')->store('name', 'Cygnite PHP Framework');
return $file;
});
// you can get information from cache using same name alias
echo $file->as('some-name')->get('name');
Checking For Existence In Cache
use Cygnite\Cache\Factory\Cache;
$file = Cache::make('file', function ($file)
{
$file->as('some-name')->store('name', 'Cygnite PHP Framework'); // with alias
$file->store('key', 'Store My Content Into Cache'); // Without aliasing
return $file;
});
if ($file->has('key')) {
// .....
}
Or
// Checking existence of cache
if ($file->as('some-name')->has('name')) {
// ......
}
Retrieving Data From The File Cache
Your data are stored as json format into file cache. You can simply retrieve data from the cache by the key which used to store.
echo $file->get('name');
Destroying Data From The File Cache
You can destroy all cache from the file using destroyAll(); method and also using destroyExpiredCache() method to destroy only specific cached data which are expired.
$file->destroyExpiredCache();
$file->destroyAll();
Using APC Cache
You should have APC extension loaded into your machine in order to use APC caching system, else cache will throw exception.
Storing An Item Into APC Cache
You can use APC caching technique to boost your application performance. Storing data into APC is similar like file cache.
use Cygnite\Cache\Factory\Cache;
$apc = Cache::make('apc', function ($apc)
{
$apc->store('name', 'Welcome To Cygnite PHP Framework');
return $apc;
});
Setting Lifetime For APC Cache
Default lifetime of APC cache is 600s. You can optionally set lifetime (expire time) of APC cache as below.
$apc->setLifeTime('Time in sec');
Retrieving An Item From APC Memory
echo $apc->get('name');
Destroy Data From The APC Memory
Use the same key to destroy data from APC memory.
$apc->destroy('name');
Using MemCache Memory
Before start using remember you must have Memcache configured into your system. Once you configure 'Memcache' class will be available to use.
Creating A New Server
You need to create a new server in order to save or retrieve data from Memcache. By default, Cygnite connect with localhost and port number 11211.
use Cygnite\Cache\Factory\Cache;
$memcache = Cache::make('memcache', function ($memcache)
{
$memcache->create('host-name', 'port-number');
return $memcache;
});
Storing An Item Into Memcache Memory
use Cygnite\Cache\Factory\Cache;
$memcache = Cache::make('memcache', function ($memcache)
{
// will connect with default host and port
$memcache->create()
->store('key', 'value');
return $memcache;
});
Retrieving Item From Memcache Memory
echo $memcache->get('key');
Destroying Item From Memcache
$memcache->destroy('key');
Using Memcached
Storing An Item Into Memcached
use Cygnite\Cache\Factory\Cache;
$memcached = Cache::make('memcached', function ($memcached)
{
// will connect with default host and port
$memcached->store('key', 'value');
return $memcached;
});
Retrieving Item From Memcached Memory
echo $memcached->get('key');
Destroying Item From Memcached
$memcached->destroy('key');
Using Redis
Getting Connection
You may get the redis connection object by simply calling connection method. Below example to retrieve default connection instance of Redis server.
use Cygnite\Cache\Factory\Cache;
// will return you default connection instance
$redis = Cache::make('redis', function ($redis)
{
return $redis->connection();
});
You may pass server name in the connection method to retrieve the connection instance. The server configuration defined in src/Apps/Configs/application.php file.
$redis->connection('some-other');
The redis cache driver uses popular Predis\Client internally. For more details feature you may have a look at Predis documentation.
Storing An Item Into Redis
You may use store method to store some item into Redis cache. By default the data store into the cache for 1 min, you may specify the minutes simply passing third parameter into the store method.
use Cygnite\Cache\Factory\Cache;
$redis = Cache::make('redis', function ($redis)
{
$redis->store('key', 'value');
return $redis;
});
You can also store the value into redis cache without time frame using set() method.
$redis->set('key', 'value');
Retrieving Item From Redis
echo $redis->get('key');
Incrementing Value
$redis->increment($key, $value = 1);
Decrementing Value
$redis->decrement($key, $value = 1);
Destroying Item From Redis
$redis->destroy('key');
Getting Instance Of Cache Drivers
If you don't wish to use Closure syntax, you can also get the instance of driver using make() method call.
use Cygnite\Cache\Factory\Cache;
$file = Cache::make('file'); // Will return file cache instance
$apc = Cache::make('apc'); // Will return APC cache instance
$memcache = Cache::make('memcache'); // Will return Memcache instance
That's all about the cygnite caching system. You can also optionally make use of any composer packages for other caching mechanism.