1. Go to this page and download the library: Download h4kuna/critical-cache 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 h4kuna\CriticalCache\PSR16\Locking\CacheLockingFactory;
use h4kuna\CriticalCache\PSR16\Pool\CachePoolFactory;
$cacheFactory = new CacheLockingFactory('/my/temp');
$cachePoolFactory = new CachePoolFactory($cacheFactory);
$cache = $cachePoolFactory->create(); // by default create MemoryCache and FileSystem. You can choose redis, memcache.
$cache->set('foo', 1); // write to memory and filesystem
$cache1 = $cachePoolFactory->create();
// try to load from Memory (not found), second is Filesystem (found), and save to Memory, return result.
echo $cache1->get('foo'); // 1
/** @var \h4kuna\CriticalCache\Services\UseOneTimeService $useOneTimeService */
$timeToLive = 900; // seconds
$useOneTimeService->save('foo', 'token', $timeToLive);
// after 900 seconds or one call $useOneTimeService::get() is removed from cache
$useOneTimeService->get('foo'); // token
$useOneTimeService->get('foo'); // null
/** @var \h4kuna\CriticalCache\Services\ValidService $validToService */
$validToService->set('foo', new DateTime('tomorrow midnight')); // from is null it is mean now
$validToService->isValid('foo'); // true from 'now' to 'tomorrow midnight'
$validToService->value('foo'); // return empty string if is valid and null if is invalid
$validToService->from('foo'); // null mean unlimited or DateTimeImmutable
$validToService->to('foo'); // null mean does not exist or DateTimeImmutable
$validToService->isValid('foo'); // true the time is in range, false is out of range
$validToService->set('bar', new DateTime('tomorrow midnight'), new DateTime('+5 minutes'), 'lorem'); // the string 'lorem' it will be a valid after 5 minutes
/** @var \h4kuna\CriticalCache\Services\TokenService $tokenService */
$token = $tokenService->make(); // return string token by default uuid v4
dump($tokenService->compare($token)); // true / false
// if you want compare your self let use get()
$token = $tokenService->make(value: 'lorem');
$value = $tokenService->get($token); // lorem
$tokenService->compare(value: $value); // false because you use get()
// implement UniqueValueServiceInterface or extends UniqueValueServiceAbstract
$checkUniqueValue = new class extends \h4kuna\CriticalCache\Services\UniqueValueServiceAbstract {
public function __construct()
{
parent::__construct(new \h4kuna\CriticalCache\Tests\Mock\RandomGeneratorMock());
}
public function check(array $data): iterable {
// example: $data = ['A', 'B', 'C', 'D', 'E'];
// SELECT unique_column FROM foo WHERE unique_column IN ('A', 'B', 'C');
// return matched values, for example B, C
yield 'B';
yield 'C';
// or
return ['B', 'C'];
}
};
/** @var \h4kuna\CriticalCache\Services\UniqueHashQueueService $uniqueHash */
$value = $uniqueHash->execute($checkUniqueValue); // random unique value, A
$value = $uniqueHash->execute($checkUniqueValue); // random unique value, D
$value = $uniqueHash->execute($checkUniqueValue); // random unique value, E
/** @var \h4kuna\CriticalCache\Services\PauseAfterUse $pauseAfterUse */
/** @var \Psr\Clock\ClockInterface $clock */
$pauseService = new class ($clock, 3) extends PauseService {
protected function run(): void
{
var_dump('hello');
}
};
$pauseAfterUse->execute($pauseService); // execute run()
$pauseAfterUse->execute($pauseService); // sleep 3 seconds and execute run()
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.