Skip to content

Commit

Permalink
Extracting commonly used logic to use cache tags to a helper method.
Browse files Browse the repository at this point in the history
This also generated the cache key automatically, or can be overwritten with a second optional parameter to the remember() method.
  • Loading branch information
nWidart committed Nov 20, 2017
1 parent dc642f1 commit 48df358
Showing 1 changed file with 72 additions and 87 deletions.
159 changes: 72 additions & 87 deletions Modules/Core/Repositories/Cache/BaseCacheDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,95 +43,59 @@ public function __construct()
*/
public function find($id)
{
return $this->cache
->tags([$this->entityName, 'global'])
->remember(
"{$this->locale}.{$this->entityName}.find.{$id}",
$this->cacheTime,
function () use ($id) {
return $this->repository->find($id);
}
);
return $this->remember(function () use ($id) {
return $this->repository->find($id);
});
}

/**
* @inheritdoc
*/
public function all()
{
return $this->cache
->tags([$this->entityName, 'global'])
->remember(
"{$this->locale}.{$this->entityName}.all",
$this->cacheTime,
function () {
return $this->repository->all();
}
);
return $this->remember(function () {
return $this->repository->all();
});
}

/**
* @inheritdoc
*/
public function allWithBuilder() : Builder
{
return $this->cache
->tags([$this->entityName, 'global'])
->remember(
"{$this->locale}.{$this->entityName}.allWithBuilder",
$this->cacheTime,
function () {
return $this->repository->allWithBuilder();
}
);
return $this->remember(function () {
return $this->repository->allWithBuilder();
});
}

/**
* @inheritdoc
*/
public function paginate($perPage = 15)
{
return $this->cache
->tags([$this->entityName, 'global'])
->remember(
"{$this->locale}.{$this->entityName}.paginate.{$perPage}",
$this->cacheTime,
function () use ($perPage) {
return $this->repository->paginate($perPage);
}
);
return $this->remember(function () use ($perPage) {
return $this->repository->paginate($perPage);
});
}

/**
* @inheritdoc
*/
public function allTranslatedIn($lang)
{
return $this->cache
->tags([$this->entityName, 'global'])
->remember(
"{$this->locale}.{$this->entityName}.allTranslatedIn.{$lang}",
$this->cacheTime,
function () use ($lang) {
return $this->repository->allTranslatedIn($lang);
}
);
return $this->remember(function () use ($lang) {
return $this->repository->allTranslatedIn($lang);
});
}

/**
* @inheritdoc
*/
public function findBySlug($slug)
{
return $this->cache
->tags([$this->entityName, 'global'])
->remember(
"{$this->locale}.{$this->entityName}.findBySlug.{$slug}",
$this->cacheTime,
function () use ($slug) {
return $this->repository->findBySlug($slug);
}
);
return $this->remember(function () use ($slug) {
return $this->repository->findBySlug($slug);
});
}

/**
Expand Down Expand Up @@ -169,53 +133,29 @@ public function destroy($model)
*/
public function findByAttributes(array $attributes)
{
$tagIdentifier = json_encode($attributes);

return $this->cache
->tags([$this->entityName, 'global'])
->remember(
"{$this->locale}.{$this->entityName}.findByAttributes.{$tagIdentifier}",
$this->cacheTime,
function () use ($attributes) {
return $this->repository->findByAttributes($attributes);
}
);
return $this->remember(function () use ($attributes) {
return $this->repository->findByAttributes($attributes);
});
}

/**
* @inheritdoc
*/
public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
{
$tagIdentifier = json_encode($attributes);

return $this->cache
->tags([$this->entityName, 'global'])
->remember(
"{$this->locale}.{$this->entityName}.findByAttributes.{$tagIdentifier}.{$orderBy}.{$sortOrder}",
$this->cacheTime,
function () use ($attributes, $orderBy, $sortOrder) {
return $this->repository->getByAttributes($attributes, $orderBy, $sortOrder);
}
);
return $this->remember(function () use ($attributes, $orderBy, $sortOrder) {
return $this->repository->getByAttributes($attributes, $orderBy, $sortOrder);
});
}

/**
* @inheritdoc
*/
public function findByMany(array $ids)
{
$tagIdentifier = json_encode($ids);

return $this->cache
->tags([$this->entityName, 'global'])
->remember(
"{$this->locale}.{$this->entityName}.findByMany.{$tagIdentifier}",
$this->cacheTime,
function () use ($ids) {
return $this->repository->findByMany($ids);
}
);
return $this->remember(function () use ($ids) {
return $this->repository->findByMany($ids);
});
}

/**
Expand All @@ -225,4 +165,49 @@ public function clearCache()
{
return $this->cache->tags($this->entityName)->flush();
}

/**
* @param \Closure $callback
* @param null|string $key
* @return mixed
*/
protected function remember(\Closure $callback, $key = null)
{
$cacheKey = $this->makeCacheKey($key);

return $this->cache
->tags([$this->entityName, 'global'])
->remember($cacheKey, $this->cacheTime, $callback);
}

/**
* Generate a cache key with the called method name and its arguments
* If a key is provided, use that instead
* @param null|string $key
* @return string
*/
private function makeCacheKey($key = null): string
{
if ($key !== null) {
return $key;
}

$cacheKey = $this->getBaseKey();

$backtrace = debug_backtrace()[2];

return sprintf("$cacheKey %s %s", $backtrace['function'], \serialize($backtrace['args']));
}

/**
* @return string
*/
protected function getBaseKey(): string
{
return sprintf(
'asgardcms -locale:%s -entity:%s',
$this->locale,
$this->entityName
);
}
}

0 comments on commit 48df358

Please sign in to comment.