CacheManager.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Closure;
  4. use InvalidArgumentException;
  5. use Illuminate\Contracts\Cache\Store;
  6. use Illuminate\Contracts\Cache\Factory as FactoryContract;
  7. use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
  8. /**
  9. * @mixin \Illuminate\Contracts\Cache\Repository
  10. */
  11. class CacheManager implements FactoryContract
  12. {
  13. /**
  14. * The application instance.
  15. *
  16. * @var \Illuminate\Foundation\Application
  17. */
  18. protected $app;
  19. /**
  20. * The array of resolved cache stores.
  21. *
  22. * @var array
  23. */
  24. protected $stores = [];
  25. /**
  26. * The registered custom driver creators.
  27. *
  28. * @var array
  29. */
  30. protected $customCreators = [];
  31. /**
  32. * Create a new Cache manager instance.
  33. *
  34. * @param \Illuminate\Foundation\Application $app
  35. * @return void
  36. */
  37. public function __construct($app)
  38. {
  39. $this->app = $app;
  40. }
  41. /**
  42. * Get a cache store instance by name.
  43. *
  44. * @param string|null $name
  45. * @return \Illuminate\Contracts\Cache\Repository
  46. */
  47. public function store($name = null)
  48. {
  49. $name = $name ?: $this->getDefaultDriver();
  50. return $this->stores[$name] = $this->get($name);
  51. }
  52. /**
  53. * Get a cache driver instance.
  54. *
  55. * @param string|null $driver
  56. * @return mixed
  57. */
  58. public function driver($driver = null)
  59. {
  60. return $this->store($driver);
  61. }
  62. /**
  63. * Attempt to get the store from the local cache.
  64. *
  65. * @param string $name
  66. * @return \Illuminate\Contracts\Cache\Repository
  67. */
  68. protected function get($name)
  69. {
  70. return $this->stores[$name] ?? $this->resolve($name);
  71. }
  72. /**
  73. * Resolve the given store.
  74. *
  75. * @param string $name
  76. * @return \Illuminate\Contracts\Cache\Repository
  77. *
  78. * @throws \InvalidArgumentException
  79. */
  80. protected function resolve($name)
  81. {
  82. $config = $this->getConfig($name);
  83. if (is_null($config)) {
  84. throw new InvalidArgumentException("Cache store [{$name}] is not defined.");
  85. }
  86. if (isset($this->customCreators[$config['driver']])) {
  87. return $this->callCustomCreator($config);
  88. } else {
  89. $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
  90. if (method_exists($this, $driverMethod)) {
  91. return $this->{$driverMethod}($config);
  92. } else {
  93. throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
  94. }
  95. }
  96. }
  97. /**
  98. * Call a custom driver creator.
  99. *
  100. * @param array $config
  101. * @return mixed
  102. */
  103. protected function callCustomCreator(array $config)
  104. {
  105. return $this->customCreators[$config['driver']]($this->app, $config);
  106. }
  107. /**
  108. * Create an instance of the APC cache driver.
  109. *
  110. * @param array $config
  111. * @return \Illuminate\Cache\ApcStore
  112. */
  113. protected function createApcDriver(array $config)
  114. {
  115. $prefix = $this->getPrefix($config);
  116. return $this->repository(new ApcStore(new ApcWrapper, $prefix));
  117. }
  118. /**
  119. * Create an instance of the array cache driver.
  120. *
  121. * @return \Illuminate\Cache\ArrayStore
  122. */
  123. protected function createArrayDriver()
  124. {
  125. return $this->repository(new ArrayStore);
  126. }
  127. /**
  128. * Create an instance of the file cache driver.
  129. *
  130. * @param array $config
  131. * @return \Illuminate\Cache\FileStore
  132. */
  133. protected function createFileDriver(array $config)
  134. {
  135. return $this->repository(new FileStore($this->app['files'], $config['path']));
  136. }
  137. /**
  138. * Create an instance of the Memcached cache driver.
  139. *
  140. * @param array $config
  141. * @return \Illuminate\Cache\MemcachedStore
  142. */
  143. protected function createMemcachedDriver(array $config)
  144. {
  145. $prefix = $this->getPrefix($config);
  146. $memcached = $this->app['memcached.connector']->connect(
  147. $config['servers'],
  148. $config['persistent_id'] ?? null,
  149. $config['options'] ?? [],
  150. array_filter($config['sasl'] ?? [])
  151. );
  152. return $this->repository(new MemcachedStore($memcached, $prefix));
  153. }
  154. /**
  155. * Create an instance of the Null cache driver.
  156. *
  157. * @return \Illuminate\Cache\NullStore
  158. */
  159. protected function createNullDriver()
  160. {
  161. return $this->repository(new NullStore);
  162. }
  163. /**
  164. * Create an instance of the Redis cache driver.
  165. *
  166. * @param array $config
  167. * @return \Illuminate\Cache\RedisStore
  168. */
  169. protected function createRedisDriver(array $config)
  170. {
  171. $redis = $this->app['redis'];
  172. $connection = $config['connection'] ?? 'default';
  173. return $this->repository(new RedisStore($redis, $this->getPrefix($config), $connection));
  174. }
  175. /**
  176. * Create an instance of the database cache driver.
  177. *
  178. * @param array $config
  179. * @return \Illuminate\Cache\DatabaseStore
  180. */
  181. protected function createDatabaseDriver(array $config)
  182. {
  183. $connection = $this->app['db']->connection($config['connection'] ?? null);
  184. return $this->repository(
  185. new DatabaseStore(
  186. $connection, $config['table'], $this->getPrefix($config)
  187. )
  188. );
  189. }
  190. /**
  191. * Create a new cache repository with the given implementation.
  192. *
  193. * @param \Illuminate\Contracts\Cache\Store $store
  194. * @return \Illuminate\Cache\Repository
  195. */
  196. public function repository(Store $store)
  197. {
  198. $repository = new Repository($store);
  199. if ($this->app->bound(DispatcherContract::class)) {
  200. $repository->setEventDispatcher(
  201. $this->app[DispatcherContract::class]
  202. );
  203. }
  204. return $repository;
  205. }
  206. /**
  207. * Get the cache prefix.
  208. *
  209. * @param array $config
  210. * @return string
  211. */
  212. protected function getPrefix(array $config)
  213. {
  214. return $config['prefix'] ?? $this->app['config']['cache.prefix'];
  215. }
  216. /**
  217. * Get the cache connection configuration.
  218. *
  219. * @param string $name
  220. * @return array
  221. */
  222. protected function getConfig($name)
  223. {
  224. return $this->app['config']["cache.stores.{$name}"];
  225. }
  226. /**
  227. * Get the default cache driver name.
  228. *
  229. * @return string
  230. */
  231. public function getDefaultDriver()
  232. {
  233. return $this->app['config']['cache.default'];
  234. }
  235. /**
  236. * Set the default cache driver name.
  237. *
  238. * @param string $name
  239. * @return void
  240. */
  241. public function setDefaultDriver($name)
  242. {
  243. $this->app['config']['cache.default'] = $name;
  244. }
  245. /**
  246. * Register a custom driver creator Closure.
  247. *
  248. * @param string $driver
  249. * @param \Closure $callback
  250. * @return $this
  251. */
  252. public function extend($driver, Closure $callback)
  253. {
  254. $this->customCreators[$driver] = $callback->bindTo($this, $this);
  255. return $this;
  256. }
  257. /**
  258. * Dynamically call the default driver instance.
  259. *
  260. * @param string $method
  261. * @param array $parameters
  262. * @return mixed
  263. */
  264. public function __call($method, $parameters)
  265. {
  266. return $this->store()->$method(...$parameters);
  267. }
  268. }