sectionable.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * @param string $function
  60. * @return mixed
  61. */
  62. public function remember_in_section($section, $key, $default, $minutes, $function = 'put')
  63. {
  64. $key = $this->section_item_key($section, $key);
  65. return $this->remember($key, $default, $minutes, $function);
  66. }
  67. /**
  68. * Get a sectioned item from the cache, or cache the default value forever.
  69. *
  70. * @param string $section
  71. * @param string $key
  72. * @param mixed $default
  73. * @return mixed
  74. */
  75. public function sear_in_section($section, $key, $default)
  76. {
  77. return $this->sear($this->section_item_key($section, $key), $default);
  78. }
  79. /**
  80. * Delete a sectioned item from the cache.
  81. *
  82. * @param string $section
  83. * @param string $key
  84. * @return void
  85. */
  86. public function forget_in_section($section, $key)
  87. {
  88. return $this->forget($this->section_item_key($section, $key));
  89. }
  90. /**
  91. * Delete an entire section from the cache.
  92. *
  93. * @param string $section
  94. * @return int|bool
  95. */
  96. abstract public function forget_section($section);
  97. /**
  98. * Indicates if a key is sectionable.
  99. *
  100. * @param string $key
  101. * @return bool
  102. */
  103. protected function sectionable($key)
  104. {
  105. return $this->implicit and $this->sectioned($key);
  106. }
  107. /**
  108. * Determine if a key is sectioned.
  109. *
  110. * @param string $key
  111. * @return bool
  112. */
  113. protected function sectioned($key)
  114. {
  115. return str_contains($key, '::');
  116. }
  117. /**
  118. * Get the section and key from a sectioned key.
  119. *
  120. * @param string $key
  121. * @return array
  122. */
  123. protected function parse($key)
  124. {
  125. return explode('::', $key, 2);
  126. }
  127. }