MemcachedStore.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Illuminate\Contracts\Cache\LockProvider;
  4. use Illuminate\Support\InteractsWithTime;
  5. use Memcached;
  6. use ReflectionMethod;
  7. class MemcachedStore extends TaggableStore implements LockProvider
  8. {
  9. use InteractsWithTime;
  10. /**
  11. * The Memcached instance.
  12. *
  13. * @var \Memcached
  14. */
  15. protected $memcached;
  16. /**
  17. * A string that should be prepended to keys.
  18. *
  19. * @var string
  20. */
  21. protected $prefix;
  22. /**
  23. * Indicates whether we are using Memcached version >= 3.0.0.
  24. *
  25. * @var bool
  26. */
  27. protected $onVersionThree;
  28. /**
  29. * Create a new Memcached store.
  30. *
  31. * @param \Memcached $memcached
  32. * @param string $prefix
  33. * @return void
  34. */
  35. public function __construct($memcached, $prefix = '')
  36. {
  37. $this->setPrefix($prefix);
  38. $this->memcached = $memcached;
  39. $this->onVersionThree = (new ReflectionMethod('Memcached', 'getMulti'))
  40. ->getNumberOfParameters() == 2;
  41. }
  42. /**
  43. * Retrieve an item from the cache by key.
  44. *
  45. * @param string $key
  46. * @return mixed
  47. */
  48. public function get($key)
  49. {
  50. $value = $this->memcached->get($this->prefix.$key);
  51. if ($this->memcached->getResultCode() == 0) {
  52. return $value;
  53. }
  54. }
  55. /**
  56. * Retrieve multiple items from the cache by key.
  57. *
  58. * Items not found in the cache will have a null value.
  59. *
  60. * @param array $keys
  61. * @return array
  62. */
  63. public function many(array $keys)
  64. {
  65. $prefixedKeys = array_map(function ($key) {
  66. return $this->prefix.$key;
  67. }, $keys);
  68. if ($this->onVersionThree) {
  69. $values = $this->memcached->getMulti($prefixedKeys, Memcached::GET_PRESERVE_ORDER);
  70. } else {
  71. $null = null;
  72. $values = $this->memcached->getMulti($prefixedKeys, $null, Memcached::GET_PRESERVE_ORDER);
  73. }
  74. if ($this->memcached->getResultCode() != 0) {
  75. return array_fill_keys($keys, null);
  76. }
  77. return array_combine($keys, $values);
  78. }
  79. /**
  80. * Store an item in the cache for a given number of seconds.
  81. *
  82. * @param string $key
  83. * @param mixed $value
  84. * @param int $seconds
  85. * @return bool
  86. */
  87. public function put($key, $value, $seconds)
  88. {
  89. return $this->memcached->set(
  90. $this->prefix.$key, $value, $this->calculateExpiration($seconds)
  91. );
  92. }
  93. /**
  94. * Store multiple items in the cache for a given number of seconds.
  95. *
  96. * @param array $values
  97. * @param int $seconds
  98. * @return bool
  99. */
  100. public function putMany(array $values, $seconds)
  101. {
  102. $prefixedValues = [];
  103. foreach ($values as $key => $value) {
  104. $prefixedValues[$this->prefix.$key] = $value;
  105. }
  106. return $this->memcached->setMulti(
  107. $prefixedValues, $this->calculateExpiration($seconds)
  108. );
  109. }
  110. /**
  111. * Store an item in the cache if the key doesn't exist.
  112. *
  113. * @param string $key
  114. * @param mixed $value
  115. * @param int $seconds
  116. * @return bool
  117. */
  118. public function add($key, $value, $seconds)
  119. {
  120. return $this->memcached->add(
  121. $this->prefix.$key, $value, $this->calculateExpiration($seconds)
  122. );
  123. }
  124. /**
  125. * Increment the value of an item in the cache.
  126. *
  127. * @param string $key
  128. * @param mixed $value
  129. * @return int|bool
  130. */
  131. public function increment($key, $value = 1)
  132. {
  133. return $this->memcached->increment($this->prefix.$key, $value);
  134. }
  135. /**
  136. * Decrement the value of an item in the cache.
  137. *
  138. * @param string $key
  139. * @param mixed $value
  140. * @return int|bool
  141. */
  142. public function decrement($key, $value = 1)
  143. {
  144. return $this->memcached->decrement($this->prefix.$key, $value);
  145. }
  146. /**
  147. * Store an item in the cache indefinitely.
  148. *
  149. * @param string $key
  150. * @param mixed $value
  151. * @return bool
  152. */
  153. public function forever($key, $value)
  154. {
  155. return $this->put($key, $value, 0);
  156. }
  157. /**
  158. * Get a lock instance.
  159. *
  160. * @param string $name
  161. * @param int $seconds
  162. * @param string|null $owner
  163. * @return \Illuminate\Contracts\Cache\Lock
  164. */
  165. public function lock($name, $seconds = 0, $owner = null)
  166. {
  167. return new MemcachedLock($this->memcached, $this->prefix.$name, $seconds, $owner);
  168. }
  169. /**
  170. * Restore a lock instance using the owner identifier.
  171. *
  172. * @param string $name
  173. * @param string $owner
  174. * @return \Illuminate\Contracts\Cache\Lock
  175. */
  176. public function restoreLock($name, $owner)
  177. {
  178. return $this->lock($name, 0, $owner);
  179. }
  180. /**
  181. * Remove an item from the cache.
  182. *
  183. * @param string $key
  184. * @return bool
  185. */
  186. public function forget($key)
  187. {
  188. return $this->memcached->delete($this->prefix.$key);
  189. }
  190. /**
  191. * Remove all items from the cache.
  192. *
  193. * @return bool
  194. */
  195. public function flush()
  196. {
  197. return $this->memcached->flush();
  198. }
  199. /**
  200. * Get the expiration time of the key.
  201. *
  202. * @param int $seconds
  203. * @return int
  204. */
  205. protected function calculateExpiration($seconds)
  206. {
  207. return $this->toTimestamp($seconds);
  208. }
  209. /**
  210. * Get the UNIX timestamp for the given number of seconds.
  211. *
  212. * @param int $seconds
  213. * @return int
  214. */
  215. protected function toTimestamp($seconds)
  216. {
  217. return $seconds > 0 ? $this->availableAt($seconds) : 0;
  218. }
  219. /**
  220. * Get the underlying Memcached connection.
  221. *
  222. * @return \Memcached
  223. */
  224. public function getMemcached()
  225. {
  226. return $this->memcached;
  227. }
  228. /**
  229. * Get the cache key prefix.
  230. *
  231. * @return string
  232. */
  233. public function getPrefix()
  234. {
  235. return $this->prefix;
  236. }
  237. /**
  238. * Set the cache key prefix.
  239. *
  240. * @param string $prefix
  241. * @return void
  242. */
  243. public function setPrefix($prefix)
  244. {
  245. $this->prefix = ! empty($prefix) ? $prefix.':' : '';
  246. }
  247. }