Cache.php 10 KB

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