ThinkCache.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare(strict_types=1);
  3. namespace Webman\ThinkOrm;
  4. use DateInterval;
  5. use DateTimeInterface;
  6. use Psr\SimpleCache\CacheInterface;
  7. use Psr\SimpleCache\InvalidArgumentException;
  8. use ReflectionException;
  9. use support\think\Cache;
  10. /**
  11. * Cache adapter for ThinkORM.
  12. */
  13. class ThinkCache implements CacheInterface
  14. {
  15. /**
  16. * Clear the entire cache pool.
  17. * @access public
  18. * @return bool
  19. * @throws ReflectionException
  20. */
  21. public function clear(): bool
  22. {
  23. return Cache::clear();
  24. }
  25. /**
  26. * Get a cache entry.
  27. * @access public
  28. * @param string $key Cache key
  29. * @param mixed $default Default value if missing
  30. * @return mixed
  31. * @throws InvalidArgumentException|ReflectionException
  32. */
  33. public function get($key, mixed $default = null): mixed
  34. {
  35. return Cache::get($key, $default);
  36. }
  37. /**
  38. * Set a cache entry.
  39. * @access public
  40. * @param string $key Cache key
  41. * @param mixed $value Value to store
  42. * @param int|DateTimeInterface|DateInterval $ttl TTL; 0 means no expiry
  43. * @return bool
  44. * @throws InvalidArgumentException|ReflectionException
  45. */
  46. public function set($key, $value, $ttl = null): bool
  47. {
  48. return Cache::set($key, $value, $ttl);
  49. }
  50. /**
  51. * Delete a cache entry.
  52. * @access public
  53. * @param string $key Cache key
  54. * @return bool
  55. * @throws InvalidArgumentException|ReflectionException
  56. */
  57. public function delete($key): bool
  58. {
  59. return Cache::delete($key);
  60. }
  61. /**
  62. * Get multiple cache entries.
  63. * @access public
  64. * @param iterable $keys Cache keys
  65. * @param mixed $default Default value for missing keys
  66. * @return iterable
  67. * @throws ReflectionException
  68. */
  69. public function getMultiple($keys, $default = null): iterable
  70. {
  71. return Cache::getMultiple($keys, $default);
  72. }
  73. /**
  74. * Set multiple cache entries.
  75. * @access public
  76. * @param iterable $values Key-value pairs
  77. * @param null|int|DateInterval $ttl TTL; 0 means no expiry
  78. * @return bool
  79. * @throws ReflectionException
  80. */
  81. public function setMultiple($values, $ttl = null): bool
  82. {
  83. return Cache::setMultiple($values, $ttl);
  84. }
  85. /**
  86. * Delete multiple cache entries.
  87. * @access public
  88. * @param iterable $keys Cache keys
  89. * @return bool
  90. * @throws ReflectionException
  91. */
  92. public function deleteMultiple($keys): bool
  93. {
  94. return Cache::deleteMultiple($keys);
  95. }
  96. /**
  97. * Whether a cache key exists.
  98. * @access public
  99. * @param string $key Cache key
  100. * @return bool
  101. * @throws InvalidArgumentException|ReflectionException
  102. */
  103. public function has($key): bool
  104. {
  105. return Cache::has($key);
  106. }
  107. /**
  108. * Increment a numeric cache value (used by ThinkORM lazy-write field accumulation).
  109. *
  110. * @param string $name Cache key
  111. * @param float|int $step Step amount
  112. * @return mixed
  113. * @throws ReflectionException
  114. */
  115. public function inc(string $name, float|int $step = 1): mixed
  116. {
  117. return Cache::inc($name, $step);
  118. }
  119. /**
  120. * Decrement a numeric cache value (used by ThinkORM lazy-write field accumulation).
  121. *
  122. * @param string $name Cache key
  123. * @param float|int $step Step amount
  124. * @return mixed
  125. * @throws ReflectionException
  126. */
  127. public function dec(string $name, float|int $step = 1): mixed
  128. {
  129. return Cache::dec($name, $step);
  130. }
  131. }