/vendor/laravel/framework/src/Illuminate/Cache/RedisTaggedCache.php

https://gitlab.com/madwanz64/laravel · PHP · 198 lines · 79 code · 24 blank · 95 comment · 2 complexity · 637e8b8498229dfd66869b3316c804bf MD5 · raw file

  1. <?php
  2. namespace Illuminate\Cache;
  3. class RedisTaggedCache extends TaggedCache
  4. {
  5. /**
  6. * Forever reference key.
  7. *
  8. * @var string
  9. */
  10. const REFERENCE_KEY_FOREVER = 'forever_ref';
  11. /**
  12. * Standard reference key.
  13. *
  14. * @var string
  15. */
  16. const REFERENCE_KEY_STANDARD = 'standard_ref';
  17. /**
  18. * Store an item in the cache.
  19. *
  20. * @param string $key
  21. * @param mixed $value
  22. * @param \DateTimeInterface|\DateInterval|int|null $ttl
  23. * @return bool
  24. */
  25. public function put($key, $value, $ttl = null)
  26. {
  27. if ($ttl === null) {
  28. return $this->forever($key, $value);
  29. }
  30. $this->pushStandardKeys($this->tags->getNamespace(), $key);
  31. return parent::put($key, $value, $ttl);
  32. }
  33. /**
  34. * Increment the value of an item in the cache.
  35. *
  36. * @param string $key
  37. * @param mixed $value
  38. * @return void
  39. */
  40. public function increment($key, $value = 1)
  41. {
  42. $this->pushStandardKeys($this->tags->getNamespace(), $key);
  43. parent::increment($key, $value);
  44. }
  45. /**
  46. * Decrement the value of an item in the cache.
  47. *
  48. * @param string $key
  49. * @param mixed $value
  50. * @return void
  51. */
  52. public function decrement($key, $value = 1)
  53. {
  54. $this->pushStandardKeys($this->tags->getNamespace(), $key);
  55. parent::decrement($key, $value);
  56. }
  57. /**
  58. * Store an item in the cache indefinitely.
  59. *
  60. * @param string $key
  61. * @param mixed $value
  62. * @return bool
  63. */
  64. public function forever($key, $value)
  65. {
  66. $this->pushForeverKeys($this->tags->getNamespace(), $key);
  67. return parent::forever($key, $value);
  68. }
  69. /**
  70. * Remove all items from the cache.
  71. *
  72. * @return bool
  73. */
  74. public function flush()
  75. {
  76. $this->deleteForeverKeys();
  77. $this->deleteStandardKeys();
  78. return parent::flush();
  79. }
  80. /**
  81. * Store standard key references into store.
  82. *
  83. * @param string $namespace
  84. * @param string $key
  85. * @return void
  86. */
  87. protected function pushStandardKeys($namespace, $key)
  88. {
  89. $this->pushKeys($namespace, $key, self::REFERENCE_KEY_STANDARD);
  90. }
  91. /**
  92. * Store forever key references into store.
  93. *
  94. * @param string $namespace
  95. * @param string $key
  96. * @return void
  97. */
  98. protected function pushForeverKeys($namespace, $key)
  99. {
  100. $this->pushKeys($namespace, $key, self::REFERENCE_KEY_FOREVER);
  101. }
  102. /**
  103. * Store a reference to the cache key against the reference key.
  104. *
  105. * @param string $namespace
  106. * @param string $key
  107. * @param string $reference
  108. * @return void
  109. */
  110. protected function pushKeys($namespace, $key, $reference)
  111. {
  112. $fullKey = $this->store->getPrefix().sha1($namespace).':'.$key;
  113. foreach (explode('|', $namespace) as $segment) {
  114. $this->store->connection()->sadd($this->referenceKey($segment, $reference), $fullKey);
  115. }
  116. }
  117. /**
  118. * Delete all of the items that were stored forever.
  119. *
  120. * @return void
  121. */
  122. protected function deleteForeverKeys()
  123. {
  124. $this->deleteKeysByReference(self::REFERENCE_KEY_FOREVER);
  125. }
  126. /**
  127. * Delete all standard items.
  128. *
  129. * @return void
  130. */
  131. protected function deleteStandardKeys()
  132. {
  133. $this->deleteKeysByReference(self::REFERENCE_KEY_STANDARD);
  134. }
  135. /**
  136. * Find and delete all of the items that were stored against a reference.
  137. *
  138. * @param string $reference
  139. * @return void
  140. */
  141. protected function deleteKeysByReference($reference)
  142. {
  143. foreach (explode('|', $this->tags->getNamespace()) as $segment) {
  144. $this->deleteValues($segment = $this->referenceKey($segment, $reference));
  145. $this->store->connection()->del($segment);
  146. }
  147. }
  148. /**
  149. * Delete item keys that have been stored against a reference.
  150. *
  151. * @param string $referenceKey
  152. * @return void
  153. */
  154. protected function deleteValues($referenceKey)
  155. {
  156. $values = array_unique($this->store->connection()->smembers($referenceKey));
  157. if (count($values) > 0) {
  158. foreach (array_chunk($values, 1000) as $valuesChunk) {
  159. $this->store->connection()->del(...$valuesChunk);
  160. }
  161. }
  162. }
  163. /**
  164. * Get the reference key for the segment.
  165. *
  166. * @param string $segment
  167. * @param string $suffix
  168. * @return string
  169. */
  170. protected function referenceKey($segment, $suffix)
  171. {
  172. return $this->store->getPrefix().$segment.':'.$suffix;
  173. }
  174. }