Setting.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace app\common\model;
  3. use think\Cache;
  4. /**
  5. * 系统配置表
  6. * @author Administrator
  7. */
  8. class Setting extends \think\Model
  9. {
  10. protected $pk = 'id';
  11. protected $table = 'cy_setting';
  12. protected $dateFormat = false; //时间戳格式不自动转换
  13. const CACHE_KEY = "clearable_get_setting:";
  14. const CACHE_TAG = "clearable_get_setting";
  15. protected static function init()
  16. {
  17. Setting::afterUpdate(function ($info) {
  18. Cache::clear(self::CACHE_TAG);
  19. Cache::rm(self::CACHE_KEY . $info['name']);
  20. });
  21. Setting::afterWrite(function ($info) {
  22. Cache::clear(self::CACHE_TAG);
  23. Cache::rm(self::CACHE_KEY . $info['name']);
  24. });
  25. }
  26. public static function getSetting($name = '')
  27. {
  28. if (empty($name)) {
  29. return false;
  30. }
  31. $cache_key = self::CACHE_KEY.$name;
  32. $info = Cache::get($cache_key);
  33. if (empty($info)) {
  34. $info = self::where(['name' => $name])->value('value');
  35. if (empty($info)) {
  36. return false;
  37. }
  38. Cache::tag(self::CACHE_TAG)->set($cache_key, $info, 3600 * 24);
  39. }
  40. return $info;
  41. }
  42. }