PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Cache/Repository.php

https://gitlab.com/milton2913/myBlog
PHP | 521 lines | 234 code | 74 blank | 213 comment | 27 complexity | edcf70c0e22e006541774338c365c401 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Closure;
  4. use DateTime;
  5. use ArrayAccess;
  6. use Carbon\Carbon;
  7. use BadMethodCallException;
  8. use Illuminate\Contracts\Cache\Store;
  9. use Illuminate\Support\Traits\Macroable;
  10. use Illuminate\Contracts\Events\Dispatcher;
  11. use Illuminate\Contracts\Cache\Repository as CacheContract;
  12. class Repository implements CacheContract, ArrayAccess
  13. {
  14. use Macroable {
  15. __call as macroCall;
  16. }
  17. /**
  18. * The cache store implementation.
  19. *
  20. * @var \Illuminate\Contracts\Cache\Store
  21. */
  22. protected $store;
  23. /**
  24. * The event dispatcher implementation.
  25. *
  26. * @var \Illuminate\Contracts\Events\Dispatcher
  27. */
  28. protected $events;
  29. /**
  30. * The default number of minutes to store items.
  31. *
  32. * @var float|int
  33. */
  34. protected $default = 60;
  35. /**
  36. * Create a new cache repository instance.
  37. *
  38. * @param \Illuminate\Contracts\Cache\Store $store
  39. * @return void
  40. */
  41. public function __construct(Store $store)
  42. {
  43. $this->store = $store;
  44. }
  45. /**
  46. * Set the event dispatcher instance.
  47. *
  48. * @param \Illuminate\Contracts\Events\Dispatcher $events
  49. * @return void
  50. */
  51. public function setEventDispatcher(Dispatcher $events)
  52. {
  53. $this->events = $events;
  54. }
  55. /**
  56. * Fire an event for this cache instance.
  57. *
  58. * @param string $event
  59. * @param array $payload
  60. * @return void
  61. */
  62. protected function fireCacheEvent($event, $payload)
  63. {
  64. if (! isset($this->events)) {
  65. return;
  66. }
  67. switch ($event) {
  68. case 'hit':
  69. if (count($payload) == 2) {
  70. $payload[] = [];
  71. }
  72. return $this->events->fire(new Events\CacheHit($payload[0], $payload[1], $payload[2]));
  73. case 'missed':
  74. if (count($payload) == 1) {
  75. $payload[] = [];
  76. }
  77. return $this->events->fire(new Events\CacheMissed($payload[0], $payload[1]));
  78. case 'delete':
  79. if (count($payload) == 1) {
  80. $payload[] = [];
  81. }
  82. return $this->events->fire(new Events\KeyForgotten($payload[0], $payload[1]));
  83. case 'write':
  84. if (count($payload) == 3) {
  85. $payload[] = [];
  86. }
  87. return $this->events->fire(new Events\KeyWritten($payload[0], $payload[1], $payload[2], $payload[3]));
  88. }
  89. }
  90. /**
  91. * Determine if an item exists in the cache.
  92. *
  93. * @param string $key
  94. * @return bool
  95. */
  96. public function has($key)
  97. {
  98. return ! is_null($this->get($key));
  99. }
  100. /**
  101. * Retrieve an item from the cache by key.
  102. *
  103. * @param string $key
  104. * @param mixed $default
  105. * @return mixed
  106. */
  107. public function get($key, $default = null)
  108. {
  109. if (is_array($key)) {
  110. return $this->many($key);
  111. }
  112. $value = $this->store->get($this->itemKey($key));
  113. if (is_null($value)) {
  114. $this->fireCacheEvent('missed', [$key]);
  115. $value = value($default);
  116. } else {
  117. $this->fireCacheEvent('hit', [$key, $value]);
  118. }
  119. return $value;
  120. }
  121. /**
  122. * Retrieve multiple items from the cache by key.
  123. *
  124. * Items not found in the cache will have a null value.
  125. *
  126. * @param array $keys
  127. * @return array
  128. */
  129. public function many(array $keys)
  130. {
  131. $normalizedKeys = [];
  132. foreach ($keys as $key => $value) {
  133. $normalizedKeys[] = is_string($key) ? $key : $value;
  134. }
  135. $values = $this->store->many($normalizedKeys);
  136. foreach ($values as $key => &$value) {
  137. if (is_null($value)) {
  138. $this->fireCacheEvent('missed', [$key]);
  139. $value = isset($keys[$key]) ? value($keys[$key]) : null;
  140. } else {
  141. $this->fireCacheEvent('hit', [$key, $value]);
  142. }
  143. }
  144. return $values;
  145. }
  146. /**
  147. * Retrieve an item from the cache and delete it.
  148. *
  149. * @param string $key
  150. * @param mixed $default
  151. * @return mixed
  152. */
  153. public function pull($key, $default = null)
  154. {
  155. $value = $this->get($key, $default);
  156. $this->forget($key);
  157. return $value;
  158. }
  159. /**
  160. * Store an item in the cache.
  161. *
  162. * @param string $key
  163. * @param mixed $value
  164. * @param \DateTime|float|int $minutes
  165. * @return void
  166. */
  167. public function put($key, $value, $minutes = null)
  168. {
  169. if (is_array($key)) {
  170. return $this->putMany($key, $value);
  171. }
  172. $minutes = $this->getMinutes($minutes);
  173. if (! is_null($minutes)) {
  174. $this->store->put($this->itemKey($key), $value, $minutes);
  175. $this->fireCacheEvent('write', [$key, $value, $minutes]);
  176. }
  177. }
  178. /**
  179. * Store multiple items in the cache for a given number of minutes.
  180. *
  181. * @param array $values
  182. * @param float|int $minutes
  183. * @return void
  184. */
  185. public function putMany(array $values, $minutes)
  186. {
  187. $minutes = $this->getMinutes($minutes);
  188. if (! is_null($minutes)) {
  189. $this->store->putMany($values, $minutes);
  190. foreach ($values as $key => $value) {
  191. $this->fireCacheEvent('write', [$key, $value, $minutes]);
  192. }
  193. }
  194. }
  195. /**
  196. * Store an item in the cache if the key does not exist.
  197. *
  198. * @param string $key
  199. * @param mixed $value
  200. * @param \DateTime|float|int $minutes
  201. * @return bool
  202. */
  203. public function add($key, $value, $minutes)
  204. {
  205. $minutes = $this->getMinutes($minutes);
  206. if (is_null($minutes)) {
  207. return false;
  208. }
  209. if (method_exists($this->store, 'add')) {
  210. return $this->store->add($this->itemKey($key), $value, $minutes);
  211. }
  212. if (is_null($this->get($key))) {
  213. $this->put($key, $value, $minutes);
  214. return true;
  215. }
  216. return false;
  217. }
  218. /**
  219. * Increment the value of an item in the cache.
  220. *
  221. * @param string $key
  222. * @param mixed $value
  223. * @return int|bool
  224. */
  225. public function increment($key, $value = 1)
  226. {
  227. return $this->store->increment($key, $value);
  228. }
  229. /**
  230. * Decrement the value of an item in the cache.
  231. *
  232. * @param string $key
  233. * @param mixed $value
  234. * @return int|bool
  235. */
  236. public function decrement($key, $value = 1)
  237. {
  238. return $this->store->decrement($key, $value);
  239. }
  240. /**
  241. * Store an item in the cache indefinitely.
  242. *
  243. * @param string $key
  244. * @param mixed $value
  245. * @return void
  246. */
  247. public function forever($key, $value)
  248. {
  249. $this->store->forever($this->itemKey($key), $value);
  250. $this->fireCacheEvent('write', [$key, $value, 0]);
  251. }
  252. /**
  253. * Get an item from the cache, or store the default value.
  254. *
  255. * @param string $key
  256. * @param \DateTime|float|int $minutes
  257. * @param \Closure $callback
  258. * @return mixed
  259. */
  260. public function remember($key, $minutes, Closure $callback)
  261. {
  262. // If the item exists in the cache we will just return this immediately
  263. // otherwise we will execute the given Closure and cache the result
  264. // of that execution for the given number of minutes in storage.
  265. if (! is_null($value = $this->get($key))) {
  266. return $value;
  267. }
  268. $this->put($key, $value = $callback(), $minutes);
  269. return $value;
  270. }
  271. /**
  272. * Get an item from the cache, or store the default value forever.
  273. *
  274. * @param string $key
  275. * @param \Closure $callback
  276. * @return mixed
  277. */
  278. public function sear($key, Closure $callback)
  279. {
  280. return $this->rememberForever($key, $callback);
  281. }
  282. /**
  283. * Get an item from the cache, or store the default value forever.
  284. *
  285. * @param string $key
  286. * @param \Closure $callback
  287. * @return mixed
  288. */
  289. public function rememberForever($key, Closure $callback)
  290. {
  291. // If the item exists in the cache we will just return this immediately
  292. // otherwise we will execute the given Closure and cache the result
  293. // of that execution for the given number of minutes. It's easy.
  294. if (! is_null($value = $this->get($key))) {
  295. return $value;
  296. }
  297. $this->forever($key, $value = $callback());
  298. return $value;
  299. }
  300. /**
  301. * Remove an item from the cache.
  302. *
  303. * @param string $key
  304. * @return bool
  305. */
  306. public function forget($key)
  307. {
  308. $success = $this->store->forget($this->itemKey($key));
  309. $this->fireCacheEvent('delete', [$key]);
  310. return $success;
  311. }
  312. /**
  313. * Begin executing a new tags operation if the store supports it.
  314. *
  315. * @param array|mixed $names
  316. * @return \Illuminate\Cache\TaggedCache
  317. *
  318. * @throws \BadMethodCallException
  319. */
  320. public function tags($names)
  321. {
  322. if (method_exists($this->store, 'tags')) {
  323. $taggedCache = $this->store->tags($names);
  324. if (! is_null($this->events)) {
  325. $taggedCache->setEventDispatcher($this->events);
  326. }
  327. $taggedCache->setDefaultCacheTime($this->default);
  328. return $taggedCache;
  329. }
  330. throw new BadMethodCallException('This cache store does not support tagging.');
  331. }
  332. /**
  333. * Format the key for a cache item.
  334. *
  335. * @param string $key
  336. * @return string
  337. */
  338. protected function itemKey($key)
  339. {
  340. return $key;
  341. }
  342. /**
  343. * Get the default cache time.
  344. *
  345. * @return float|int
  346. */
  347. public function getDefaultCacheTime()
  348. {
  349. return $this->default;
  350. }
  351. /**
  352. * Set the default cache time in minutes.
  353. *
  354. * @param float|int $minutes
  355. * @return void
  356. */
  357. public function setDefaultCacheTime($minutes)
  358. {
  359. $this->default = $minutes;
  360. }
  361. /**
  362. * Get the cache store implementation.
  363. *
  364. * @return \Illuminate\Contracts\Cache\Store
  365. */
  366. public function getStore()
  367. {
  368. return $this->store;
  369. }
  370. /**
  371. * Determine if a cached value exists.
  372. *
  373. * @param string $key
  374. * @return bool
  375. */
  376. public function offsetExists($key)
  377. {
  378. return $this->has($key);
  379. }
  380. /**
  381. * Retrieve an item from the cache by key.
  382. *
  383. * @param string $key
  384. * @return mixed
  385. */
  386. public function offsetGet($key)
  387. {
  388. return $this->get($key);
  389. }
  390. /**
  391. * Store an item in the cache for the default time.
  392. *
  393. * @param string $key
  394. * @param mixed $value
  395. * @return void
  396. */
  397. public function offsetSet($key, $value)
  398. {
  399. $this->put($key, $value, $this->default);
  400. }
  401. /**
  402. * Remove an item from the cache.
  403. *
  404. * @param string $key
  405. * @return void
  406. */
  407. public function offsetUnset($key)
  408. {
  409. $this->forget($key);
  410. }
  411. /**
  412. * Calculate the number of minutes with the given duration.
  413. *
  414. * @param \DateTime|float|int $duration
  415. * @return float|int|null
  416. */
  417. protected function getMinutes($duration)
  418. {
  419. if ($duration instanceof DateTime) {
  420. $duration = Carbon::now()->diffInSeconds(Carbon::instance($duration), false) / 60;
  421. }
  422. return (int) ($duration * 60) > 0 ? $duration : null;
  423. }
  424. /**
  425. * Handle dynamic calls into macros or pass missing methods to the store.
  426. *
  427. * @param string $method
  428. * @param array $parameters
  429. * @return mixed
  430. */
  431. public function __call($method, $parameters)
  432. {
  433. if (static::hasMacro($method)) {
  434. return $this->macroCall($method, $parameters);
  435. }
  436. return $this->store->$method(...$parameters);
  437. }
  438. /**
  439. * Clone cache repository instance.
  440. *
  441. * @return void
  442. */
  443. public function __clone()
  444. {
  445. $this->store = clone $this->store;
  446. }
  447. }