| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Repositories\Concerns; |
| 6 |
|
| 7 |
use Yatra\Utils\Cache; |
| 8 |
|
| 9 |
/** |
| 10 |
* Query-result caching for repository classes (DB reads, external API payloads composed here). |
| 11 |
* Do not use for WordPress options — call {@see get_option()} directly without wrapping in {@see Cache}. |
| 12 |
* |
| 13 |
* Pair reads with invalidation on writes: override {@see \Yatra\Repositories\BaseRepository::afterWrite} |
| 14 |
* or fire domain hooks that {@see \Yatra\Hooks\CacheHooks} / {@see \Yatra\Utils\Cache} handle. |
| 15 |
*/ |
| 16 |
trait CachesQueryResults |
| 17 |
{ |
| 18 |
/** |
| 19 |
* @param callable(): mixed $callback |
| 20 |
* |
| 21 |
* @return mixed |
| 22 |
*/ |
| 23 |
protected function cacheQueryResult(string $key, callable $callback, int $duration = 3600) |
| 24 |
{ |
| 25 |
return Cache::remember($key, $callback, $duration); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Lets application services keep orchestration while storing cache keys and TTLs on the repository. |
| 30 |
* |
| 31 |
* @param callable(): mixed $callback |
| 32 |
* |
| 33 |
* @return mixed |
| 34 |
*/ |
| 35 |
public function withQueryCache(string $key, callable $callback, int $duration = 3600) |
| 36 |
{ |
| 37 |
return $this->cacheQueryResult($key, $callback, $duration); |
| 38 |
} |
| 39 |
} |
| 40 |
|