Extracting commonly used logic to use cache tags to a helper method.

This also generated the cache key automatically, or can be overwritten with a second optional parameter to the remember() method.
parent dc642f1c
...@@ -43,15 +43,9 @@ abstract class BaseCacheDecorator implements BaseRepository ...@@ -43,15 +43,9 @@ abstract class BaseCacheDecorator implements BaseRepository
*/ */
public function find($id) public function find($id)
{ {
return $this->cache return $this->remember(function () use ($id) {
->tags([$this->entityName, 'global']) return $this->repository->find($id);
->remember( });
"{$this->locale}.{$this->entityName}.find.{$id}",
$this->cacheTime,
function () use ($id) {
return $this->repository->find($id);
}
);
} }
/** /**
...@@ -59,15 +53,9 @@ abstract class BaseCacheDecorator implements BaseRepository ...@@ -59,15 +53,9 @@ abstract class BaseCacheDecorator implements BaseRepository
*/ */
public function all() public function all()
{ {
return $this->cache return $this->remember(function () {
->tags([$this->entityName, 'global']) return $this->repository->all();
->remember( });
"{$this->locale}.{$this->entityName}.all",
$this->cacheTime,
function () {
return $this->repository->all();
}
);
} }
/** /**
...@@ -75,15 +63,9 @@ abstract class BaseCacheDecorator implements BaseRepository ...@@ -75,15 +63,9 @@ abstract class BaseCacheDecorator implements BaseRepository
*/ */
public function allWithBuilder() : Builder public function allWithBuilder() : Builder
{ {
return $this->cache return $this->remember(function () {
->tags([$this->entityName, 'global']) return $this->repository->allWithBuilder();
->remember( });
"{$this->locale}.{$this->entityName}.allWithBuilder",
$this->cacheTime,
function () {
return $this->repository->allWithBuilder();
}
);
} }
/** /**
...@@ -91,15 +73,9 @@ abstract class BaseCacheDecorator implements BaseRepository ...@@ -91,15 +73,9 @@ abstract class BaseCacheDecorator implements BaseRepository
*/ */
public function paginate($perPage = 15) public function paginate($perPage = 15)
{ {
return $this->cache return $this->remember(function () use ($perPage) {
->tags([$this->entityName, 'global']) return $this->repository->paginate($perPage);
->remember( });
"{$this->locale}.{$this->entityName}.paginate.{$perPage}",
$this->cacheTime,
function () use ($perPage) {
return $this->repository->paginate($perPage);
}
);
} }
/** /**
...@@ -107,15 +83,9 @@ abstract class BaseCacheDecorator implements BaseRepository ...@@ -107,15 +83,9 @@ abstract class BaseCacheDecorator implements BaseRepository
*/ */
public function allTranslatedIn($lang) public function allTranslatedIn($lang)
{ {
return $this->cache return $this->remember(function () use ($lang) {
->tags([$this->entityName, 'global']) return $this->repository->allTranslatedIn($lang);
->remember( });
"{$this->locale}.{$this->entityName}.allTranslatedIn.{$lang}",
$this->cacheTime,
function () use ($lang) {
return $this->repository->allTranslatedIn($lang);
}
);
} }
/** /**
...@@ -123,15 +93,9 @@ abstract class BaseCacheDecorator implements BaseRepository ...@@ -123,15 +93,9 @@ abstract class BaseCacheDecorator implements BaseRepository
*/ */
public function findBySlug($slug) public function findBySlug($slug)
{ {
return $this->cache return $this->remember(function () use ($slug) {
->tags([$this->entityName, 'global']) return $this->repository->findBySlug($slug);
->remember( });
"{$this->locale}.{$this->entityName}.findBySlug.{$slug}",
$this->cacheTime,
function () use ($slug) {
return $this->repository->findBySlug($slug);
}
);
} }
/** /**
...@@ -169,17 +133,9 @@ abstract class BaseCacheDecorator implements BaseRepository ...@@ -169,17 +133,9 @@ abstract class BaseCacheDecorator implements BaseRepository
*/ */
public function findByAttributes(array $attributes) public function findByAttributes(array $attributes)
{ {
$tagIdentifier = json_encode($attributes); return $this->remember(function () use ($attributes) {
return $this->repository->findByAttributes($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);
}
);
} }
/** /**
...@@ -187,17 +143,9 @@ abstract class BaseCacheDecorator implements BaseRepository ...@@ -187,17 +143,9 @@ abstract class BaseCacheDecorator implements BaseRepository
*/ */
public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc') public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
{ {
$tagIdentifier = json_encode($attributes); return $this->remember(function () use ($attributes, $orderBy, $sortOrder) {
return $this->repository->getByAttributes($attributes, $orderBy, $sortOrder);
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);
}
);
} }
/** /**
...@@ -205,17 +153,9 @@ abstract class BaseCacheDecorator implements BaseRepository ...@@ -205,17 +153,9 @@ abstract class BaseCacheDecorator implements BaseRepository
*/ */
public function findByMany(array $ids) public function findByMany(array $ids)
{ {
$tagIdentifier = json_encode($ids); return $this->remember(function () use ($ids) {
return $this->repository->findByMany($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);
}
);
} }
/** /**
...@@ -225,4 +165,49 @@ abstract class BaseCacheDecorator implements BaseRepository ...@@ -225,4 +165,49 @@ abstract class BaseCacheDecorator implements BaseRepository
{ {
return $this->cache->tags($this->entityName)->flush(); 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
);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment