BaseCacheDecorator.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\common\repositories\Cache;
  3. use Illuminate\Cache\Repository;
  4. use Illuminate\Config\Repository as ConfigRepository;
  5. use app\common\repositories\BaseRepository;
  6. abstract class BaseCacheDecorator implements BaseRepository
  7. {
  8. /**
  9. * @var \app\common\repositories\BaseRepository
  10. */
  11. protected $repository;
  12. /**
  13. * @var Repository
  14. */
  15. protected $cache;
  16. /**
  17. * @var string The entity name
  18. */
  19. protected $entityName;
  20. /**
  21. * @var string The application locale
  22. */
  23. protected $locale;
  24. /**
  25. * @var int caching time
  26. */
  27. protected $cacheTime;
  28. public function __construct()
  29. {
  30. $this->cache = app(Repository::class);
  31. $this->cacheTime = app(ConfigRepository::class)->get('cache.time', 60);
  32. $this->locale = app()->getLocale();
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function find($id)
  38. {
  39. return $this->cache
  40. ->tags([$this->entityName, 'global'])
  41. ->remember("{$this->locale}.{$this->entityName}.find.{$id}", $this->cacheTime,
  42. function () use ($id) {
  43. return $this->repository->find($id);
  44. }
  45. );
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function all()
  51. {
  52. return $this->cache
  53. ->tags([$this->entityName, 'global'])
  54. ->remember("{$this->locale}.{$this->entityName}.all", $this->cacheTime,
  55. function () {
  56. return $this->repository->all();
  57. }
  58. );
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function paginate($perPage = 15)
  64. {
  65. return $this->cache
  66. ->tags([$this->entityName, 'global'])
  67. ->remember("{$this->locale}.{$this->entityName}.paginate.{$perPage}", $this->cacheTime,
  68. function () use ($perPage) {
  69. return $this->repository->paginate($perPage);
  70. }
  71. );
  72. }
  73. /**
  74. * @inheritdoc
  75. */
  76. public function allTranslatedIn($lang)
  77. {
  78. return $this->cache
  79. ->tags([$this->entityName, 'global'])
  80. ->remember("{$this->locale}.{$this->entityName}.allTranslatedIn.{$lang}", $this->cacheTime,
  81. function () use ($lang) {
  82. return $this->repository->allTranslatedIn($lang);
  83. }
  84. );
  85. }
  86. /**
  87. * @inheritdoc
  88. */
  89. public function findBySlug($slug)
  90. {
  91. return $this->cache
  92. ->tags([$this->entityName, 'global'])
  93. ->remember("{$this->locale}.{$this->entityName}.findBySlug.{$slug}", $this->cacheTime,
  94. function () use ($slug) {
  95. return $this->repository->findBySlug($slug);
  96. }
  97. );
  98. }
  99. /**
  100. * @inheritdoc
  101. */
  102. public function create($data)
  103. {
  104. $this->cache->tags($this->entityName)->flush();
  105. return $this->repository->create($data);
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. public function update($model, $data)
  111. {
  112. $this->cache->tags($this->entityName)->flush();
  113. return $this->repository->update($model, $data);
  114. }
  115. /**
  116. * @inheritdoc
  117. */
  118. public function destroy($model)
  119. {
  120. $this->cache->tags($this->entityName)->flush();
  121. return $this->repository->destroy($model);
  122. }
  123. /**
  124. * @inheritdoc
  125. */
  126. public function findByAttributes(array $attributes)
  127. {
  128. $tagIdentifier = json_encode($attributes);
  129. return $this->cache
  130. ->tags([$this->entityName, 'global'])
  131. ->remember("{$this->locale}.{$this->entityName}.findByAttributes.{$tagIdentifier}", $this->cacheTime,
  132. function () use ($attributes) {
  133. return $this->repository->findByAttributes($attributes);
  134. }
  135. );
  136. }
  137. /**
  138. * @inheritdoc
  139. */
  140. public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc')
  141. {
  142. $tagIdentifier = json_encode($attributes);
  143. return $this->cache
  144. ->tags([$this->entityName, 'global'])
  145. ->remember("{$this->locale}.{$this->entityName}.findByAttributes.{$tagIdentifier}.{$orderBy}.{$sortOrder}", $this->cacheTime,
  146. function () use ($attributes, $orderBy, $sortOrder) {
  147. return $this->repository->getByAttributes($attributes, $orderBy, $sortOrder);
  148. }
  149. );
  150. }
  151. /**
  152. * @inheritdoc
  153. */
  154. public function findByMany(array $ids)
  155. {
  156. $tagIdentifier = json_encode($ids);
  157. return $this->cache
  158. ->tags([$this->entityName, 'global'])
  159. ->remember("{$this->locale}.{$this->entityName}.findByMany.{$tagIdentifier}", $this->cacheTime,
  160. function () use ($ids) {
  161. return $this->repository->findByMany($ids);
  162. }
  163. );
  164. }
  165. /**
  166. * @inheritdoc
  167. */
  168. public function clearCache()
  169. {
  170. return $this->cache->tags($this->entityName)->flush();
  171. }
  172. }