選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

55 行
1.1KB

  1. <?php
  2. namespace Framework\Cache\Driver;
  3. class MemoryDriver implements Driver
  4. {
  5. private array $config = [];
  6. private array $cached = [];
  7. public function __construct(array $config)
  8. {
  9. $this->config = $config;
  10. }
  11. public function has(string $key): bool
  12. {
  13. return isset($this->cached[$key]) && $this->cached[$key]['expires'] > time();
  14. }
  15. public function get(string $key, mixed $default = null): mixed
  16. {
  17. if ($this->has($key)) {
  18. return $this->cached[$key]['value'];
  19. }
  20. return $default;
  21. }
  22. public function put(string $key, mixed $value, int $seconds = null): static
  23. {
  24. if (!is_int($seconds)) {
  25. $seconds = (int) $this->config['seconds'];
  26. }
  27. $this->cached[$key] = [
  28. 'value' => $value,
  29. 'expires' => time() + $seconds,
  30. ];
  31. return $this;
  32. }
  33. public function forget(string $key): static
  34. {
  35. unset($this->cached[$key]);
  36. return $this;
  37. }
  38. public function flush(): static
  39. {
  40. $this->cached = [];
  41. return $this;
  42. }
  43. }

Powered by TurnKey Linux.