MenuCache.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. *
  12. * @return mixed
  13. */
  14. protected function remember(\Closure $builder)
  15. {
  16. if (! $this->enableCache()) {
  17. return $builder();
  18. }
  19. return $this->getStore()->remember($this->getCacheKey(), null, $builder);
  20. }
  21. /**
  22. * @return bool|void
  23. */
  24. public function flushCache()
  25. {
  26. if (! $this->enableCache()) {
  27. return;
  28. }
  29. return $this->getStore()->delete($this->getCacheKey());
  30. }
  31. /**
  32. * @return string
  33. */
  34. protected function getCacheKey()
  35. {
  36. return sprintf($this->cacheKey, (int) static::withPermission());
  37. }
  38. /**
  39. * @return bool
  40. */
  41. public function enableCache()
  42. {
  43. return config('admin.menu.cache.enable');
  44. }
  45. /**
  46. * Get cache store.
  47. *
  48. * @return \Illuminate\Contracts\Cache\Repository
  49. */
  50. public function getStore()
  51. {
  52. return Cache::store(config('admin.menu.cache.store', 'file'));
  53. }
  54. }