Driver.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Cache\Driver;
  12. use Hyperf\Cache\Exception\InvalidArgumentException;
  13. use Hyperf\Codec\Packer\PhpSerializerPacker;
  14. use Hyperf\Contract\PackerInterface;
  15. use Hyperf\Support\Traits\InteractsWithTime;
  16. use Psr\Container\ContainerInterface;
  17. abstract class Driver implements DriverInterface
  18. {
  19. use InteractsWithTime;
  20. /**
  21. * @var ContainerInterface
  22. */
  23. protected $container;
  24. /**
  25. * @var array
  26. */
  27. protected $config;
  28. /**
  29. * @var PackerInterface
  30. */
  31. protected $packer;
  32. /**
  33. * @var string
  34. */
  35. protected $prefix;
  36. public function __construct(ContainerInterface $container, array $config)
  37. {
  38. $this->container = $container;
  39. $this->config = $config;
  40. $this->prefix = $config['prefix'] ?? 'cache:';
  41. $packerClass = $config['packer'] ?? PhpSerializerPacker::class;
  42. $this->packer = $container->get($packerClass);
  43. }
  44. public function getConnection(): mixed
  45. {
  46. throw new InvalidArgumentException('Cannot support method getConnection.');
  47. }
  48. protected function getCacheKey(string $key)
  49. {
  50. return $this->prefix . $key;
  51. }
  52. }