mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Cached result of the count query on VisitsPaginatorAdapter
This commit is contained in:
parent
c4ae89a279
commit
0e4bccc4bb
@ -15,6 +15,8 @@ class VisitsPaginatorAdapter implements AdapterInterface
|
|||||||
private ShortUrlIdentifier $identifier;
|
private ShortUrlIdentifier $identifier;
|
||||||
private VisitsParams $params;
|
private VisitsParams $params;
|
||||||
|
|
||||||
|
private ?int $count = null;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
VisitRepositoryInterface $visitRepository,
|
VisitRepositoryInterface $visitRepository,
|
||||||
ShortUrlIdentifier $identifier,
|
ShortUrlIdentifier $identifier,
|
||||||
@ -38,7 +40,17 @@ class VisitsPaginatorAdapter implements AdapterInterface
|
|||||||
|
|
||||||
public function count(): int
|
public function count(): int
|
||||||
{
|
{
|
||||||
return $this->visitRepository->countVisitsByShortCode(
|
// Since a new adapter instance is created every time visits are fetched, it is reasonably safe to internally
|
||||||
|
// cache the count value.
|
||||||
|
// The reason it is cached is because the Paginator is actually calling the method twice.
|
||||||
|
// An inconsistent value could be returned if between the first call and the second one, a new visit is created.
|
||||||
|
// However, it's almost instant, and then the adapter instance is discarded immediately after.
|
||||||
|
|
||||||
|
if ($this->count !== null) {
|
||||||
|
return $this->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->count = $this->visitRepository->countVisitsByShortCode(
|
||||||
$this->identifier->shortCode(),
|
$this->identifier->shortCode(),
|
||||||
$this->identifier->domain(),
|
$this->identifier->domain(),
|
||||||
$this->params->getDateRange(),
|
$this->params->getDateRange(),
|
||||||
|
Loading…
Reference in New Issue
Block a user