sectionable.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php namespace Laravel\Cache\Drivers;
  2. abstract class Sectionable extends Driver {
  3. /**
  4. * Indicates that section caching is implicit based on keys.
  5. *
  6. * @var bool
  7. */
  8. public $implicit = true;
  9. /**
  10. * The implicit section key delimiter.
  11. *
  12. * @var string
  13. */
  14. public $delimiter = '::';
  15. /**
  16. * Retrieve a sectioned item from the cache driver.
  17. *
  18. * @param string $section
  19. * @param string $key
  20. * @param mixed $default
  21. * @return mixed
  22. */
  23. public function get_from_section($section, $key, $default = null)
  24. {
  25. return $this->get($this->section_item_key($section, $key), $default);
  26. }
  27. /**
  28. * Write a sectioned item to the cache.
  29. *
  30. * @param string $section
  31. * @param string $key
  32. * @param mixed $value
  33. * @param int $minutes
  34. * @return void
  35. */
  36. public function put_in_section($section, $key, $value, $minutes)
  37. {
  38. $this->put($this->section_item_key($section, $key), $value, $minutes);
  39. }
  40. /**
  41. * Write a sectioned item to the cache that lasts forever.
  42. *
  43. * @param string $section
  44. * @param string $key
  45. * @param mixed $value
  46. * @return void
  47. */
  48. public function forever_in_section($section, $key, $value)
  49. {
  50. return $this->forever($this->section_item_key($section, $key), $value);
  51. }
  52. /**
  53. * Get a sectioned item from the cache, or cache and return the default value.
  54. *
  55. * @param string $section
  56. * @param string $key
  57. * @param mixed $default
  58. * @param int $minutes
  59. * @return mixed
  60. */
  61. public function remember_in_section($section, $key, $default, $minutes, $function = 'put')
  62. {
  63. $key = $this->section_item_key($section, $key);
  64. return $this->remember($key, $default, $minutes, $function);
  65. }
  66. /**
  67. * Get a sectioned item from the cache, or cache the default value forever.
  68. *
  69. * @param string $section
  70. * @param string $key
  71. * @param mixed $default
  72. * @return mixed
  73. */
  74. public function sear_in_section($section, $key, $default)
  75. {
  76. return $this->sear($this->section_item_key($section, $key), $default);
  77. }
  78. /**
  79. * Delete a sectioned item from the cache.
  80. *
  81. * @param string $section
  82. * @param string $key
  83. * @return void
  84. */
  85. public function forget_in_section($section, $key)
  86. {
  87. return $this->forget($this->section_item_key($section, $key));
  88. }
  89. /**
  90. * Delete an entire section from the cache.
  91. *
  92. * @param string $section
  93. * @return int|bool
  94. */
  95. abstract public function forget_section($section);
  96. /**
  97. * Indicates if a key is sectionable.
  98. *
  99. * @param string $key
  100. * @return bool
  101. */
  102. protected function sectionable($key)
  103. {
  104. return $this->implicit and $this->sectioned($key);
  105. }
  106. /**
  107. * Determine if a key is sectioned.
  108. *
  109. * @param string $key
  110. * @return bool
  111. */
  112. protected function sectioned($key)
  113. {
  114. return str_contains($key, '::');
  115. }
  116. /**
  117. * Get the section and key from a sectioned key.
  118. *
  119. * @param string $key
  120. * @return array
  121. */
  122. protected function parse($key)
  123. {
  124. return explode('::', $key, 2);
  125. }
  126. }