1. Go to this page and download the library: Download henzeb/warmable 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/ */
henzeb / warmable example snippets
use Henzeb\Warmable\Warmable;
use Psr\SimpleCache\CacheInterface;
use DateTimeInterface;
use DateInterval;
class HeavyOperation extends Warmable
{
protected function key() : string
{
// ... return your key
}
protected function ttl() : DateTimeInterface|DateInterval|int|null
{
// ... return your desired ttl
}
protected function gracePeriod() : DateTimeInterface|DateInterval|int|null
{
// ... return your desired grace period
}
protected function cache(): CacheInterface
{
// ... your cache implementation
}
protected function warmable(): mixed
{
// ... your heavy operation
}
}
// equivalent to new HeavyOperation();
HeavyOperation::make();
// equivalent to new HeavyOperation(new YourService());
HeavyOperation::make(new YourService());
use Henzeb\Warmable\Warmable;
class HeavyOperation extends Warmable
{
// ...
public function warmable(int $id): mixed {
// ...
return 'this is '.$id;
}
}
HeavyOperation::with(12)->get(); // returns 'this is 12'
HeavyOperation::with(14)->get(); // returns 'this is 14'
HeavyOperation::with(12)->get(); // returns 'this is 12' from cache
HeavyOperation::with(14)->get(); // returns 'this is 14' from cache
use Henzeb\Warmable\Warmable;
class HeavyOperation extends Warmable {
protected function key() : string{
return 'your-key';
}
}
HeavyOperation::get(); // retrieves from 'your-key'
HeavyOperation::with(12)->get(); // retrieves from 'your-key.<sha1 hash>'
HeavyOperation::withKey('other-key')->get(); // retrieves from 'other-key'
HeavyOperation::withKey('other-key')
->with(12)
->get(); // retrieves from 'other-key.<sha1 hash>'
use Psr\SimpleCache\CacheInterface;
HeavyOperation::withCache(<CacheInterface::class>);
HeavyOperation::make()->withCache(<CacheInterface::class>);
use Henzeb\Warmable\Warmable;
class HeavyOperation extends Warmable
{
protected function ttl(): DateTimeInterface|DateInterval|int|null {
return DateInterval::createFromDateString('300 seconds');
}
}
use Henzeb\Warmable\Warmable;
// expires on given date
HeavyOperation::withTtl(new DateTime('2024-02-31 23:00'));
// expires in 300 seconds
HeavyOperation::withTtl(
DateInterval::createFromDateString('300 seconds')
);
// expires in 300 seconds
HeavyOperation::withTtl(300);
use Henzeb\Warmable\Warmable;
class HeavyOperation extends Warmable
{
protected function ttl(): DateTimeInterface|DateInterval|int|null {
return 300
}
protected function gracePeriod(): DateTimeInterface|DateInterval|int|null {
return 300
}
}
// returns true
HeavyOperation::missing();
// returns false
HeavyOperation::get(); // will preheat the cache
HeavyOperation::make()->missing();
HeavyOperation::missing();
$operation = HeavyOperation::make();
$operation->isPreheated(); // returns false
$operation->get(); // will preheat the cache
$operation->isPreheated(); // returns true
HeavyOperation::withoutPreheating()->get(); // returns null when not warmed up
// and when called again:
HeavyOperation::withoutPreheating()->get(); // returns null when not warmed up
// would preheat the warmable data.
HeavyOperation::withPreheating()->get();
// would preheat the data after shutdown, and return default instead.
HeavyOperation::withPreheating()->get([]);