-
-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathCacheContract.php
61 lines (50 loc) · 1.56 KB
/
CacheContract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
*
* This file is part of Phpfastcache.
*
* @license MIT License (MIT)
*
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
*
* @author Georges.L (Geolim4) <[email protected]>
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
*/
declare(strict_types=1);
namespace Phpfastcache;
use DateInterval;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;
class CacheContract
{
protected CacheItemPoolInterface $cacheInstance;
public function __construct(CacheItemPoolInterface $cacheInstance)
{
$this->cacheInstance = $cacheInstance;
}
/**
* @throws InvalidArgumentException
*/
public function get(string|\Stringable $cacheKey, callable $callback, DateInterval|int|null $expiresAfter = null): mixed
{
$cacheItem = $this->cacheInstance->getItem((string) $cacheKey);
if (!$cacheItem->isHit()) {
/*
* Parameter $cacheItem will be available as of 8.0.6
*/
$cacheItem->set($callback($cacheItem));
if ($expiresAfter) {
$cacheItem->expiresAfter($expiresAfter);
}
$this->cacheInstance->save($cacheItem);
}
return $cacheItem->get();
}
/**
* @throws InvalidArgumentException
*/
public function __invoke(string $cacheKey, callable $callback, DateInterval|int|null $expiresAfter = null): mixed
{
return $this->get($cacheKey, $callback, $expiresAfter);
}
}