Cache_factory.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 全局缓存类
  13. // +----------------------------------------------------------------------
  14. namespace addons\cms\library;
  15. use think\facade\Cache;
  16. class Cache_factory
  17. {
  18. protected static $instance = null;
  19. protected $cacheList = [
  20. 'Category' => [
  21. 'name' => '栏目索引',
  22. 'model' => 'Category',
  23. 'action' => 'category_cache',
  24. ],
  25. 'Model' => [
  26. 'name' => '模型列表',
  27. 'model' => 'Models',
  28. 'action' => 'model_cache',
  29. ],
  30. 'ModelField' => [
  31. 'name' => '模型字段',
  32. 'model' => 'ModelField',
  33. 'action' => 'model_field_cache',
  34. ],
  35. ];
  36. /**
  37. * @param 缓存实例化
  38. * @return static
  39. */
  40. public static function instance($options = [])
  41. {
  42. if (is_null(self::$instance)) {
  43. self::$instance = new self($options);
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 获取缓存
  49. * @param type $name 缓存名称
  50. * @return null
  51. */
  52. public function get($name)
  53. {
  54. $cache = Cache::get($name);
  55. if (!empty($cache)) {
  56. return $cache;
  57. } else {
  58. //尝试生成缓存
  59. return $this->runUpdate($name);
  60. }
  61. return null;
  62. }
  63. /**
  64. * 写入缓存
  65. * @param string $name 缓存变量名
  66. * @param type $value 存储数据
  67. * @param type $expire 有效时间(秒)
  68. * @return boolean
  69. */
  70. public function set($name, $value, $expire = null)
  71. {
  72. return Cache::tag('cms')->set($name, $value, $expire);
  73. }
  74. /**
  75. * 删除缓存
  76. * @param string $name 缓存变量名
  77. * @return boolean
  78. */
  79. public function remove($name)
  80. {
  81. return Cache::delete($name);
  82. }
  83. /**
  84. * 更新缓存
  85. * @param type $name 缓存key
  86. * @return boolean
  87. */
  88. public function runUpdate($name)
  89. {
  90. if (empty($name)) {
  91. return false;
  92. }
  93. //查询缓存key
  94. $cacheList = $this->cacheList;
  95. foreach ($cacheList as $k => $v) {
  96. $modelClass = "\\addons\\cms\\model\\{$v['model']}";
  97. $model = new $modelClass;
  98. if ($v['action']) {
  99. $action = $v['action'];
  100. $model->$action(); //执行方法
  101. }
  102. }
  103. //再次加载
  104. return Cache::get($name);
  105. }
  106. }