| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- namespace app\common\repositories\Cache;
- use Illuminate\Cache\Repository;
- use Illuminate\Config\Repository as ConfigRepository;
- use app\common\repositories\BaseRepository;
- abstract class BaseCacheDecorator implements BaseRepository
- {
- /**
- * @var \app\common\repositories\BaseRepository
- */
- protected $repository;
- /**
- * @var Repository
- */
- protected $cache;
- /**
- * @var string The entity name
- */
- protected $entityName;
- /**
- * @var string The application locale
- */
- protected $locale;
- /**
- * @var int caching time
- */
- protected $cacheTime;
- public function __construct()
- {
- $this->cache = app(Repository::class);
- $this->cacheTime = app(ConfigRepository::class)->get('cache.time', 60);
- $this->locale = app()->getLocale();
- }
- /**
- * @inheritdoc
- */
- 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);
- }
- );
- }
- /**
- * @inheritdoc
- */
- public function all()
- {
- return $this->cache
- ->tags([$this->entityName, 'global'])
- ->remember("{$this->locale}.{$this->entityName}.all", $this->cacheTime,
- function () {
- return $this->repository->all();
- }
- );
- }
- /**
- * @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);
- }
- );
- }
- /**
- * @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);
- }
- );
- }
- /**
- * @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);
- }
- );
- }
- /**
- * @inheritdoc
- */
- public function create($data)
- {
- $this->cache->tags($this->entityName)->flush();
- return $this->repository->create($data);
- }
- /**
- * @inheritdoc
- */
- public function update($model, $data)
- {
- $this->cache->tags($this->entityName)->flush();
- return $this->repository->update($model, $data);
- }
- /**
- * @inheritdoc
- */
- public function destroy($model)
- {
- $this->cache->tags($this->entityName)->flush();
- return $this->repository->destroy($model);
- }
- /**
- * @inheritdoc
- */
- 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);
- }
- );
- }
- /**
- * @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);
- }
- );
- }
- /**
- * @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);
- }
- );
- }
- /**
- * @inheritdoc
- */
- public function clearCache()
- {
- return $this->cache->tags($this->entityName)->flush();
- }
- }
|