memcached.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php namespace Laravel\Cache\Drivers; use Memcache;
  2. class Memcached extends Driver {
  3. /**
  4. * The Memcache instance.
  5. *
  6. * @var Memcache
  7. */
  8. protected $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 Memcache $memcache
  19. * @return void
  20. */
  21. public function __construct(Memcache $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 (($cache = $this->memcache->get($this->key.$key)) !== false)
  45. {
  46. return unserialize($cache);
  47. }
  48. }
  49. /**
  50. * Write an item to the cache for a given number of minutes.
  51. *
  52. * <code>
  53. * // Put an item in the cache for 15 minutes
  54. * Cache::put('name', 'Taylor', 15);
  55. * </code>
  56. *
  57. * @param string $key
  58. * @param mixed $value
  59. * @param int $minutes
  60. * @return void
  61. */
  62. public function put($key, $value, $minutes)
  63. {
  64. $this->memcache->set($this->key.$key, serialize($value), 0, $minutes * 60);
  65. }
  66. /**
  67. * Write an item to the cache that lasts forever.
  68. *
  69. * @param string $key
  70. * @param mixed $value
  71. * @return void
  72. */
  73. public function forever($key, $value)
  74. {
  75. return $this->put($key, $value, 0);
  76. }
  77. /**
  78. * Delete an item from the cache.
  79. *
  80. * @param string $key
  81. * @return void
  82. */
  83. public function forget($key)
  84. {
  85. $this->memcache->delete($this->key.$key);
  86. }
  87. }