MenuCache.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Dcat\Admin\Models;
  3. use Illuminate\Support\Facades\Cache;
  4. trait MenuCache
  5. {
  6. protected $cacheKey = 'dcat-admin-menus-%d';
  7. /**
  8. * Get an item from the cache, or execute the given Closure and store the result.
  9. *
  10. * @param \Closure $builder
  11. * @return mixed
  12. */
  13. protected function remember(\Closure $builder)
  14. {
  15. if (!$this->enableCache()) {
  16. return $builder();
  17. }
  18. return $this->getStore()->remember($this->getCacheKey(), null, $builder);
  19. }
  20. /**
  21. * Delete all items from the cache.
  22. *
  23. * @return bool
  24. */
  25. public function destroyCache()
  26. {
  27. if (!$this->enableCache()) {
  28. return null;
  29. }
  30. return $this->getStore()->delete($this->getCacheKey());
  31. }
  32. /**
  33. * @return string
  34. */
  35. protected function getCacheKey()
  36. {
  37. return sprintf($this->cacheKey, (int)static::withPermission());
  38. }
  39. /**
  40. * @return bool
  41. */
  42. public function enableCache()
  43. {
  44. return config('admin.menu.cache.enable');
  45. }
  46. /**
  47. * Get cache store.
  48. *
  49. * @return \Illuminate\Contracts\Cache\Repository
  50. */
  51. public function getStore()
  52. {
  53. return Cache::store(config('admin.menu.cache.store', 'file'));
  54. }
  55. }