MenuCache.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Dcat\Admin\Models;
  3. use Illuminate\Support\Facades\Cache;
  4. trait MenuCache
  5. {
  6. protected $cacheKey = 'dcat-admin-menus';
  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->cacheKey, 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->cacheKey);
  31. }
  32. /**
  33. * @return bool
  34. */
  35. public function enableCache()
  36. {
  37. return config('admin.menu.cache.enable');
  38. }
  39. /**
  40. * Get cache store.
  41. *
  42. * @return \Illuminate\Contracts\Cache\Repository
  43. */
  44. public function getStore()
  45. {
  46. return Cache::store(config('admin.menu.cache.store', 'file'));
  47. }
  48. }