0
0

Cache.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use think\cache\Driver;
  13. /**
  14. * Class Cache
  15. *
  16. * @package think
  17. *
  18. * @mixin Driver
  19. * @mixin \think\cache\driver\File
  20. */
  21. class Cache
  22. {
  23. /**
  24. * 缓存实例
  25. * @var array
  26. */
  27. protected $instance = [];
  28. /**
  29. * 缓存配置
  30. * @var array
  31. */
  32. protected $config = [];
  33. /**
  34. * 操作句柄
  35. * @var object
  36. */
  37. protected $handler;
  38. public function __construct(array $config = [])
  39. {
  40. $this->config = $config;
  41. $this->init($config);
  42. }
  43. /**
  44. * 连接缓存
  45. * @access public
  46. * @param array $options 配置数组
  47. * @param bool|string $name 缓存连接标识 true 强制重新连接
  48. * @return Driver
  49. */
  50. public function connect(array $options = [], $name = false)
  51. {
  52. if (false === $name) {
  53. $name = md5(serialize($options));
  54. }
  55. if (true === $name || !isset($this->instance[$name])) {
  56. $type = !empty($options['type']) ? $options['type'] : 'File';
  57. if (true === $name) {
  58. $name = md5(serialize($options));
  59. }
  60. $this->instance[$name] = Loader::factory($type, '\\think\\cache\\driver\\', $options);
  61. }
  62. return $this->instance[$name];
  63. }
  64. /**
  65. * 自动初始化缓存
  66. * @access public
  67. * @param array $options 配置数组
  68. * @param bool $force 强制更新
  69. * @return Driver
  70. */
  71. public function init(array $options = [], $force = false)
  72. {
  73. if (is_null($this->handler) || $force) {
  74. if ('complex' == $options['type']) {
  75. $default = $options['default'];
  76. $options = isset($options[$default['type']]) ? $options[$default['type']] : $default;
  77. }
  78. $this->handler = $this->connect($options);
  79. }
  80. return $this->handler;
  81. }
  82. public static function __make(Config $config)
  83. {
  84. return new static($config->pull('cache'));
  85. }
  86. public function getConfig()
  87. {
  88. return $this->config;
  89. }
  90. public function setConfig(array $config)
  91. {
  92. $this->config = array_merge($this->config, $config);
  93. }
  94. /**
  95. * 切换缓存类型 需要配置 cache.type 为 complex
  96. * @access public
  97. * @param string $name 缓存标识
  98. * @return Driver
  99. */
  100. public function store($name = '')
  101. {
  102. if ('' !== $name && 'complex' == $this->config['type']) {
  103. return $this->connect($this->config[$name], strtolower($name));
  104. }
  105. return $this->init();
  106. }
  107. public function __call($method, $args)
  108. {
  109. return call_user_func_array([$this->init(), $method], $args);
  110. }
  111. }