memcached.php 3.3 KB

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