DictCache.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. declare(strict_types=1);
  8. namespace plugin\saiadmin\app\cache;
  9. use plugin\saiadmin\app\logic\system\SystemDictTypeLogic;
  10. use plugin\saiadmin\app\model\system\SystemDictType;
  11. use support\think\Cache;
  12. /**
  13. * 字典信息缓存
  14. */
  15. class DictCache
  16. {
  17. /**
  18. * 读取缓存配置
  19. * @return array
  20. */
  21. public static function cacheConfig(): array
  22. {
  23. return config('plugin.saiadmin.saithink.dict_cache', [
  24. 'expire' => 60 * 60 * 24 * 365,
  25. 'tag' => 'saiadmin:dict_cache',
  26. ]);
  27. }
  28. /**
  29. * 获取全部字典
  30. */
  31. public static function getDictAll(): array
  32. {
  33. $cache = static::cacheConfig();
  34. // 直接从缓存获取
  35. $data = Cache::get($cache['tag']);
  36. if ($data) {
  37. return $data;
  38. }
  39. // 获取信息并返回
  40. $data = static::setDictAll();
  41. if ($data) {
  42. return $data;
  43. }
  44. return [];
  45. }
  46. /**
  47. * 获取单个字典
  48. */
  49. public static function getDict($code): array
  50. {
  51. $data = static::getDictAll();
  52. if (isset($data[$code])) {
  53. return $data[$code];
  54. } else {
  55. return [];
  56. }
  57. }
  58. /**
  59. * 设置全部字典
  60. */
  61. public static function setDictAll(): array
  62. {
  63. $cache = static::cacheConfig();
  64. $data = (new SystemDictTypeLogic)->getDictAll();
  65. Cache::set($cache['tag'], $data, $cache['expire']);
  66. return $data;
  67. }
  68. /**
  69. * 清除全部字典信息
  70. */
  71. public static function clear(): bool
  72. {
  73. $cache = static::cacheConfig();
  74. return Cache::delete($cache['tag']);
  75. }
  76. }