Files
portal-api/app/Repositories/Customer/CustomerCachedRepository.php
T

76 lines
2.4 KiB
PHP

<?php
namespace App\Repositories\Customer;
use App\Repositories\BaseCachedRepository;
use Illuminate\Support\Facades\Cache;
class CustomerCachedRepository extends BaseCachedRepository implements CustomerRepositoryInterface
{
public function __construct(
public CustomerRepository $customerRepository
) {
parent::__construct();
}
public function getModel()
{
return $this->customerRepository->getModel();
}
public function filter(
array $data = [],
array $relationships = [],
array $withCount = [],
array $select = ['*'],
bool $returnQuery = false
) {
if (
$returnQuery ||
!empty($data['paginate'])
) {
return $this->customerRepository
->filter($data, $relationships, $withCount, $select, $returnQuery);
}
$cacheKey = $this->getCacheKey(md5(json_encode([
'filter' => compact('data', 'relationships', 'withCount', 'select')
])));
return Cache::remember(
$cacheKey,
$this->cacheTime,
fn() =>
$this->customerRepository
->filter($data, $relationships, $withCount, $select, $returnQuery)
);
}
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findWithRelationships' => [
'id' => $id,
'relationships' => $relationships
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($id, $relationships, $withCount, $withSum) {
return $this->customerRepository->findWithRelationships($id, $relationships, $withCount, $withSum);
});
}
public function findBy(string $key = '', string $value = '')
{
$cacheKey = $this->getCacheKey(md5(json_encode([
'findBy' => [
'key' => $key,
'value' => $value
]
])));
return Cache::remember($cacheKey, $this->cacheTime, function () use ($key, $value) {
return $this->customerRepository->findBy($key, $value);
});
}
public function onlyTrashed(array $relationships = [], array $withCount = [])
{
return $this->customerRepository->onlyTrashed($relationships, $withCount);
}
}