-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheHandler.php
84 lines (69 loc) · 2.38 KB
/
CacheHandler.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
namespace Pandawa\Component\Eloquent;
use Illuminate\Cache\TaggedCache;
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Pandawa\Contracts\Eloquent\Cache\CacheHandlerInterface;
use Pandawa\Contracts\Eloquent\QueryBuilderInterface;
/**
* @author Iqbal Maulana <iq.bluejack@gmail.com>
*/
class CacheHandler implements CacheHandlerInterface
{
const MODELS = 'models';
const QUERIES = 'queries';
public function __construct(
protected readonly CacheRepository $repository,
protected readonly ?int $ttl = 60 * 60 * 24,
) {
}
public function rememberModel(Eloquent $model): Eloquent
{
return $this->getCacheTags($model, static::MODELS)->remember(
$model->getKey(),
$this->ttl,
fn() => $model,
);
}
public function rememberQuery(QueryBuilderInterface $queryBuilder, mixed $value): mixed
{
return $this->getCacheTags($queryBuilder->getModel(), static::QUERIES)->remember(
$this->makeKeyFromQueryBuilder($queryBuilder),
$this->ttl,
fn() => $value,
);
}
public function getByKey(Eloquent $model, int|string $key): ?Eloquent
{
return $this->getCacheTags($model, static::MODELS)->get($key);
}
public function getByQuery(QueryBuilderInterface $queryBuilder): mixed
{
return $this->getCacheTags($queryBuilder->getModel(), static::QUERIES)->get(
$this->makeKeyFromQueryBuilder($queryBuilder),
);
}
public function invalidate(Eloquent $model): Eloquent
{
$this->getCacheTags($model, static::MODELS)->delete($model->getKey());
$this->getCacheTags($model, static::QUERIES)->flush();
return $model;
}
protected function makeKeyFromQueryBuilder(QueryBuilderInterface $queryBuilder): string
{
$parts = [
$queryBuilder->toSql(),
implode('-', $queryBuilder->getBindings()),
];
return base64_encode(implode('.', $parts));
}
protected function getTagName(Eloquent $model, string $type): string
{
return $model->getTable().'.'.$type;
}
protected function getCacheTags(Eloquent $model, string $type): TaggedCache
{
return $this->repository->tags($this->getTagName($model, $type));
}
}