1. Go to this page and download the library: Download infira/cachly library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Infira\Cachly\CacheItem;
if (!Cachly::has('myKey')) {
Cachly::put('myKey', 'my Value', '+1 day'); //save value immediately and will expire after one day
}
Cachly::get('key','defaultValue'); //defaultValue
Cachly::get('key'); //null
Cachly::put('key','stored value'); //CacheItem
Cachly::get('key','defaultValue'); //stored value
Cachly::get('key'); //stored value
//run callback when value does not exist
Cachly::get('compute-key','defaultValue'); //defaultValue
Cachly::get('compute-key'); //null
Cachly::get('compute-key', function (CacheItem $item) {
$item->expiresAfter(3600);
// do computations
$computedValue = 'foobar';
return $computedValue;
});
Cachly::get('compute-key','defaultValue'); //foobar
Cachly::get('compute-key'); //foobar
$item = Cachly::set('array', ['init value']);
$item[] = 'value2';
$item['key'] = 'value3';
Cachly::get('array'); //foobar
function filterItems(DateTimeInterface $date, array $filters): mixed
{
//add as many variables as you want as long last variable is callable
return Cachly::once('getDataFromDataBase', $date, $filters, function (CacheItem $item) use ($date, $filters) {
$item->expiresAfter(3600);
return db()->where('date', $date)->where($filters);
});
}
$item = Cachly::set('key2','value1')->expire('tomorrow')->save(); //saves value to database
$item->save(); //will not save because nothing has changed since last save
$item->commit(); //will save value without checking changes
$item->set('newValue')->save(); //will save
$item->set('newValue')->save(); //do nothing
$item->expire(5)->save(); //will save
$autoSave = Cachly::set('key2','value1')->autoSave(true); //tries save after every change
$autoSave->set('new value'); //will call save()
$autoSave->expire('tomorrow'); //will also call save