RedisTaggedCache.php 4.6KB

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