memory.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php namespace Laravel\Cache\Drivers;
  2. class Memory extends Sectionable {
  3. /**
  4. * The in-memory array of cached items.
  5. *
  6. * @var string
  7. */
  8. public $storage = array();
  9. /**
  10. * Determine if an item exists in the cache.
  11. *
  12. * @param string $key
  13. * @return bool
  14. */
  15. public function has($key)
  16. {
  17. return ( ! is_null($this->get($key)));
  18. }
  19. /**
  20. * Retrieve an item from the cache driver.
  21. *
  22. * @param string $key
  23. * @return mixed
  24. */
  25. protected function retrieve($key)
  26. {
  27. if ($this->sectionable($key))
  28. {
  29. list($section, $key) = $this->parse($key);
  30. return $this->get_from_section($section, $key);
  31. }
  32. else
  33. {
  34. return array_get($this->storage, $key);
  35. }
  36. }
  37. /**
  38. * Write an item to the cache for a given number of minutes.
  39. *
  40. * <code>
  41. * // Put an item in the cache for 15 minutes
  42. * Cache::put('name', 'Taylor', 15);
  43. * </code>
  44. *
  45. * @param string $key
  46. * @param mixed $value
  47. * @param int $minutes
  48. * @return void
  49. */
  50. public function put($key, $value, $minutes)
  51. {
  52. if ($this->sectionable($key))
  53. {
  54. list($section, $key) = $this->parse($key);
  55. return $this->put_in_section($section, $key, $value, $minutes);
  56. }
  57. else
  58. {
  59. array_set($this->storage, $key, $value);
  60. }
  61. }
  62. /**
  63. * Write an item to the cache that lasts forever.
  64. *
  65. * @param string $key
  66. * @param mixed $value
  67. * @return void
  68. */
  69. public function forever($key, $value)
  70. {
  71. if ($this->sectionable($key))
  72. {
  73. list($section, $key) = $this->parse($key);
  74. return $this->forever_in_section($section, $key, $value);
  75. }
  76. else
  77. {
  78. $this->put($key, $value, 0);
  79. }
  80. }
  81. /**
  82. * Delete an item from the cache.
  83. *
  84. * @param string $key
  85. * @return void
  86. */
  87. public function forget($key)
  88. {
  89. if ($this->sectionable($key))
  90. {
  91. list($section, $key) = $this->parse($key);
  92. if ($key == '*')
  93. {
  94. $this->forget_section($section);
  95. }
  96. else
  97. {
  98. $this->forget_in_section($section, $key);
  99. }
  100. }
  101. else
  102. {
  103. array_forget($this->storage, $key);
  104. }
  105. }
  106. /**
  107. * Delete an entire section from the cache.
  108. *
  109. * @param string $section
  110. * @return int|bool
  111. */
  112. public function forget_section($section)
  113. {
  114. array_forget($this->storage, 'section#'.$section);
  115. }
  116. /**
  117. * Flush the entire cache.
  118. *
  119. * @return void
  120. */
  121. public function flush()
  122. {
  123. $this->storage = array();
  124. }
  125. /**
  126. * Get a section item key for a given section and key.
  127. *
  128. * @param string $section
  129. * @param string $key
  130. * @return string
  131. */
  132. protected function section_item_key($section, $key)
  133. {
  134. return "section#{$section}.{$key}";
  135. }
  136. }