memcached.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. * Indicates that section caching is implicit based on keys.
  17. *
  18. * @var bool
  19. */
  20. public $implicit = true;
  21. /**
  22. * The implicit section key delimiter.
  23. *
  24. * @var string
  25. */
  26. public $delimiter = '::';
  27. /**
  28. * Create a new Memcached cache driver instance.
  29. *
  30. * @param Memcached $memcache
  31. * @return void
  32. */
  33. public function __construct(\Memcached $memcache, $key)
  34. {
  35. $this->key = $key;
  36. $this->memcache = $memcache;
  37. }
  38. /**
  39. * Determine if an item exists in the cache.
  40. *
  41. * @param string $key
  42. * @return bool
  43. */
  44. public function has($key)
  45. {
  46. return ( ! is_null($this->get($key)));
  47. }
  48. /**
  49. * Retrieve an item from the cache driver.
  50. *
  51. * @param string $key
  52. * @return mixed
  53. */
  54. protected function retrieve($key)
  55. {
  56. if ($this->sectionable($key))
  57. {
  58. list($section, $key) = $this->parse($key);
  59. return $this->get_from_section($section, $key);
  60. }
  61. elseif (($cache = $this->memcache->get($this->key.$key)) !== false)
  62. {
  63. return $cache;
  64. }
  65. }
  66. /**
  67. * Retrieve a sectioned item from the cache driver.
  68. *
  69. * @param string $section
  70. * @param string $key
  71. * @param mixed $default
  72. * @return mixed
  73. */
  74. public function get_from_section($section, $key, $default = null)
  75. {
  76. return $this->get($this->section_item_key($section, $key), $default);
  77. }
  78. /**
  79. * Write an item to the cache for a given number of minutes.
  80. *
  81. * <code>
  82. * // Put an item in the cache for 15 minutes
  83. * Cache::put('name', 'Taylor', 15);
  84. * </code>
  85. *
  86. * @param string $key
  87. * @param mixed $value
  88. * @param int $minutes
  89. * @return void
  90. */
  91. public function put($key, $value, $minutes)
  92. {
  93. if ($this->sectionable($key))
  94. {
  95. list($section, $key) = $this->parse($key);
  96. return $this->put_in_section($section, $key, $value, $minutes);
  97. }
  98. else
  99. {
  100. $this->memcache->set($this->key.$key, $value, $minutes * 60);
  101. }
  102. }
  103. /**
  104. * Write a sectioned item to the cache.
  105. *
  106. * @param string $section
  107. * @param string $key
  108. * @param mixed $value
  109. * @param int $minutes
  110. * @return void
  111. */
  112. public function put_in_section($section, $key, $value, $minutes)
  113. {
  114. $this->put($this->section_item_key($section, $key), $value, $minutes);
  115. }
  116. /**
  117. * Write an item to the cache that lasts forever.
  118. *
  119. * @param string $key
  120. * @param mixed $value
  121. * @return void
  122. */
  123. public function forever($key, $value)
  124. {
  125. if ($this->sectionable($key))
  126. {
  127. list($section, $key) = $this->parse($key);
  128. return $this->forever_in_section($section, $key, $value);
  129. }
  130. else
  131. {
  132. return $this->put($key, $value, 0);
  133. }
  134. }
  135. /**
  136. * Write a sectioned item to the cache that lasts forever.
  137. *
  138. * @param string $section
  139. * @param string $key
  140. * @param mixed $value
  141. * @return void
  142. */
  143. public function forever_in_section($section, $key, $value)
  144. {
  145. return $this->forever($this->section_item_key($section, $key), $value);
  146. }
  147. /**
  148. * Get a sectioned item from the cache, or cache and return the default value.
  149. *
  150. * @param string $section
  151. * @param string $key
  152. * @param mixed $default
  153. * @param int $minutes
  154. * @return mixed
  155. */
  156. public function remember_in_section($section, $key, $default, $minutes, $function = 'put')
  157. {
  158. $key = $this->section_item_key($section, $key);
  159. return $this->remember($key, $default, $minutes, $function);
  160. }
  161. /**
  162. * Get a sectioned item from the cache, or cache the default value forever.
  163. *
  164. * @param string $section
  165. * @param string $key
  166. * @param mixed $default
  167. * @return mixed
  168. */
  169. public function sear_in_section($section, $key, $default)
  170. {
  171. return $this->sear($this->section_item_key($section, $key), $default);
  172. }
  173. /**
  174. * Delete an item from the cache.
  175. *
  176. * @param string $key
  177. * @return void
  178. */
  179. public function forget($key)
  180. {
  181. if ($this->sectionable($key))
  182. {
  183. list($section, $key) = $this->parse($key);
  184. if ($key == '*')
  185. {
  186. $this->forget_section($section);
  187. }
  188. else
  189. {
  190. $this->forget_in_section($section, $key);
  191. }
  192. }
  193. else
  194. {
  195. $this->memcache->delete($this->key.$key);
  196. }
  197. }
  198. /**
  199. * Delete a sectioned item from the cache.
  200. *
  201. * @param string $section
  202. * @param string $key
  203. * @return void
  204. */
  205. public function forget_in_section($section, $key)
  206. {
  207. return $this->forget($this->section_item_key($section, $key));
  208. }
  209. /**
  210. * Delete an entire section from the cache.
  211. *
  212. * @param string $section
  213. * @return int|bool
  214. */
  215. public function forget_section($section)
  216. {
  217. return $this->memcache->increment($this->key.$this->section_key($section));
  218. }
  219. /**
  220. * Get the current section ID for a given section.
  221. *
  222. * @param string $section
  223. * @return int
  224. */
  225. protected function section_id($section)
  226. {
  227. return $this->sear($this->section_key($section), function()
  228. {
  229. return rand(1, 10000);
  230. });
  231. }
  232. /**
  233. * Get a section key name for a given section.
  234. *
  235. * @param string $section
  236. * @return string
  237. */
  238. protected function section_key($section)
  239. {
  240. return $section.'_section_key';
  241. }
  242. /**
  243. * Get a section item key for a given section and key.
  244. *
  245. * @param string $section
  246. * @param string $key
  247. * @return string
  248. */
  249. protected function section_item_key($section, $key)
  250. {
  251. return $section.'#'.$this->section_id($section).'#'.$key;
  252. }
  253. }