memcached.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php namespace Laravel\Cache;
  2. use Laravel\Config;
  3. class Memcached extends Driver {
  4. /**
  5. * The Memcache instance.
  6. *
  7. * @var Memcache
  8. */
  9. private $memcache;
  10. /**
  11. * The cache key from the cache configuration file.
  12. *
  13. * @var string
  14. */
  15. private $key;
  16. /**
  17. * Create a new Memcached cache driver instance.
  18. *
  19. * @param Memcache $memcache
  20. * @return void
  21. */
  22. public function __construct(\Memcache $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. return (($cache = $this->memcache->get($this->key.$key)) !== false) ? $cache : null;
  46. }
  47. /**
  48. * Write an item to the cache for a given number of minutes.
  49. *
  50. * @param string $key
  51. * @param mixed $value
  52. * @param int $minutes
  53. * @return void
  54. */
  55. public function put($key, $value, $minutes)
  56. {
  57. $this->memcache->set($this->key.$key, $value, 0, $minutes * 60);
  58. }
  59. /**
  60. * Delete an item from the cache.
  61. *
  62. * @param string $key
  63. * @return void
  64. */
  65. public function forget($key)
  66. {
  67. $this->memcache->delete($this->key.$key);
  68. }
  69. }