説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Cache_factory.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 全局缓存类
  13. // +----------------------------------------------------------------------
  14. namespace libs;
  15. use think\Db;
  16. use think\facade\Cache;
  17. class Cache_factory
  18. {
  19. protected static $instance = null;
  20. /**
  21. * @param 缓存实例化
  22. * @return static
  23. */
  24. public static function instance($options = [])
  25. {
  26. if (is_null(self::$instance)) {
  27. self::$instance = new self($options);
  28. }
  29. return self::$instance;
  30. }
  31. /**
  32. * 获取缓存
  33. * @param type $name 缓存名称
  34. * @return null
  35. */
  36. public function get($name)
  37. {
  38. $cache = Cache::get($name);
  39. if (!empty($cache)) {
  40. return $cache;
  41. } else {
  42. //尝试生成缓存
  43. return $this->runUpdate($name);
  44. }
  45. return null;
  46. }
  47. /**
  48. * 写入缓存
  49. * @param string $name 缓存变量名
  50. * @param type $value 存储数据
  51. * @param type $expire 有效时间(秒)
  52. * @return boolean
  53. */
  54. public function set($name, $value, $expire = null)
  55. {
  56. return Cache::set($name, $value, $expire);
  57. }
  58. /**
  59. * 删除缓存
  60. * @param string $name 缓存变量名
  61. * @return boolean
  62. */
  63. public function remove($name)
  64. {
  65. return Cache::rm($name, null);
  66. }
  67. /**
  68. * 更新缓存
  69. * @param type $name 缓存key
  70. * @return boolean
  71. */
  72. public function runUpdate($name)
  73. {
  74. if (empty($name)) {
  75. return false;
  76. }
  77. //查询缓存key
  78. $cacheList = Db::name('cache')->where(array('key' => $name))->order(array('id' => 'DESC'))->select();
  79. if (empty($cacheList)) {
  80. return false;
  81. }
  82. foreach ($cacheList as $config) {
  83. if (empty($config)) {
  84. $this->error = '没有可需要更新的缓存信息!';
  85. return false;
  86. }
  87. $mo = '';
  88. if (empty($config['module'])) {
  89. $mo = "common/{$config['model']}";
  90. } else {
  91. $mo = "{$config['module']}/{$config['model']}";
  92. }
  93. $model = model($mo);
  94. if ($config['action']) {
  95. $action = $config['action'];
  96. $model->$action(); //执行方法
  97. }
  98. }
  99. //再次加载
  100. return Cache::get($name);
  101. }
  102. }