InteractsWithCache.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace Bytedance\Kernel\Traits;
  16. use Bytedance\Kernel\Exceptions\InvalidArgumentException;
  17. use Bytedance\Kernel\ServiceContainer;
  18. use Psr\Cache\CacheItemPoolInterface;
  19. use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;
  20. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  21. use Symfony\Component\Cache\Psr16Cache;
  22. use Symfony\Component\Cache\Simple\FilesystemCache;
  23. /**
  24. * Trait InteractsWithCache.
  25. *
  26. * @author overtrue <i@overtrue.me>
  27. */
  28. trait InteractsWithCache
  29. {
  30. /**
  31. * @var \Psr\SimpleCache\CacheInterface
  32. */
  33. protected $cache;
  34. /**
  35. * Get cache instance.
  36. *
  37. * @return \Psr\SimpleCache\CacheInterface
  38. *
  39. * @throws \Bytedance\Kernel\Exceptions\InvalidArgumentException
  40. */
  41. public function getCache()
  42. {
  43. if ($this->cache) {
  44. return $this->cache;
  45. }
  46. if (property_exists($this, 'app') && $this->app instanceof ServiceContainer && isset($this->app['cache'])) {
  47. $this->setCache($this->app['cache']);
  48. // Fix PHPStan error
  49. assert($this->cache instanceof \Psr\SimpleCache\CacheInterface);
  50. return $this->cache;
  51. }
  52. return $this->cache = $this->createDefaultCache();
  53. }
  54. /**
  55. * Set cache instance.
  56. *
  57. * @param \Psr\SimpleCache\CacheInterface|\Psr\Cache\CacheItemPoolInterface $cache
  58. *
  59. * @return $this
  60. *
  61. * @throws \Bytedance\Kernel\Exceptions\InvalidArgumentException
  62. */
  63. public function setCache($cache)
  64. {
  65. if (empty(\array_intersect([SimpleCacheInterface::class, CacheItemPoolInterface::class], \class_implements($cache)))) {
  66. throw new InvalidArgumentException(\sprintf('The cache instance must implements %s or %s interface.', SimpleCacheInterface::class, CacheItemPoolInterface::class));
  67. }
  68. if ($cache instanceof CacheItemPoolInterface) {
  69. if (!$this->isSymfony43OrHigher()) {
  70. throw new InvalidArgumentException(sprintf('The cache instance must implements %s', SimpleCacheInterface::class));
  71. }
  72. $cache = new Psr16Cache($cache);
  73. }
  74. $this->cache = $cache;
  75. return $this;
  76. }
  77. /**
  78. * @return \Psr\SimpleCache\CacheInterface
  79. */
  80. protected function createDefaultCache()
  81. {
  82. if ($this->isSymfony43OrHigher()) {
  83. return new Psr16Cache(new FilesystemAdapter('bytedance', 1500));
  84. }
  85. return new FilesystemCache();
  86. }
  87. /**
  88. * @return bool
  89. */
  90. protected function isSymfony43OrHigher(): bool
  91. {
  92. return \class_exists('Symfony\Component\Cache\Psr16Cache');
  93. }
  94. }