memcached.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php namespace Laravel\Cache\Drivers;
  2. class Memcached extends Sectionable {
  3. /**
  4. * The Memcache instance.
  5. *
  6. * @var Memcached
  7. */
  8. public $memcache;
  9. /**
  10. * The cache key from the cache configuration file.
  11. *
  12. * @var string
  13. */
  14. protected $key;
  15. /**
  16. * Create a new Memcached cache driver instance.
  17. *
  18. * @param Memcached $memcache
  19. * @return void
  20. */
  21. public function __construct(\Memcached $memcache, $key)
  22. {
  23. $this->key = $key;
  24. $this->memcache = $memcache;
  25. }
  26. /**
  27. * Determine if an item exists in the cache.
  28. *
  29. * @param string $key
  30. * @return bool
  31. */
  32. public function has($key)
  33. {
  34. return ( ! is_null($this->get($key)));
  35. }
  36. /**
  37. * Retrieve an item from the cache driver.
  38. *
  39. * @param string $key
  40. * @return mixed
  41. */
  42. protected function retrieve($key)
  43. {
  44. if ($this->sectionable($key))
  45. {
  46. list($section, $key) = $this->parse($key);
  47. return $this->get_from_section($section, $key);
  48. }
  49. elseif (($cache = $this->memcache->get($this->key.$key)) !== false)
  50. {
  51. return $cache;
  52. }
  53. }
  54. /**
  55. * Write an item to the cache for a given number of minutes.
  56. *
  57. * <code>
  58. * // Put an item in the cache for 15 minutes
  59. * Cache::put('name', 'Taylor', 15);
  60. * </code>
  61. *
  62. * @param string $key
  63. * @param mixed $value
  64. * @param int $minutes
  65. * @return void
  66. */
  67. public function put($key, $value, $minutes)
  68. {
  69. if ($this->sectionable($key))
  70. {
  71. list($section, $key) = $this->parse($key);
  72. return $this->put_in_section($section, $key, $value, $minutes);
  73. }
  74. else
  75. {
  76. $this->memcache->set($this->key.$key, $value, $minutes * 60);
  77. }
  78. }
  79. /**
  80. * Write an item to the cache that lasts forever.
  81. *
  82. * @param string $key
  83. * @param mixed $value
  84. * @return void
  85. */
  86. public function forever($key, $value)
  87. {
  88. if ($this->sectionable($key))
  89. {
  90. list($section, $key) = $this->parse($key);
  91. return $this->forever_in_section($section, $key, $value);
  92. }
  93. else
  94. {
  95. return $this->put($key, $value, 0);
  96. }
  97. }
  98. /**
  99. * Delete an item from the cache.
  100. *
  101. * @param string $key
  102. * @return void
  103. */
  104. public function forget($key)
  105. {
  106. if ($this->sectionable($key))
  107. {
  108. list($section, $key) = $this->parse($key);
  109. if ($key == '*')
  110. {
  111. $this->forget_section($section);
  112. }
  113. else
  114. {
  115. $this->forget_in_section($section, $key);
  116. }
  117. }
  118. else
  119. {
  120. $this->memcache->delete($this->key.$key);
  121. }
  122. }
  123. /**
  124. * Delete an entire section from the cache.
  125. *
  126. * @param string $section
  127. * @return int|bool
  128. */
  129. public function forget_section($section)
  130. {
  131. return $this->memcache->increment($this->key.$this->section_key($section));
  132. }
  133. /**
  134. * Get the current section ID for a given section.
  135. *
  136. * @param string $section
  137. * @return int
  138. */
  139. protected function section_id($section)
  140. {
  141. return $this->sear($this->section_key($section), function()
  142. {
  143. return rand(1, 10000);
  144. });
  145. }
  146. /**
  147. * Get a section key name for a given section.
  148. *
  149. * @param string $section
  150. * @return string
  151. */
  152. protected function section_key($section)
  153. {
  154. return $section.'_section_key';
  155. }
  156. /**
  157. * Get a section item key for a given section and key.
  158. *
  159. * @param string $section
  160. * @param string $key
  161. * @return string
  162. */
  163. protected function section_item_key($section, $key)
  164. {
  165. return $section.'#'.$this->section_id($section).'#'.$key;
  166. }
  167. }