redis.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php namespace Laravel\Cache\Drivers;
  2. class Redis extends Driver {
  3. /**
  4. * The Redis database instance.
  5. *
  6. * @var Laravel\Redis
  7. */
  8. protected $redis;
  9. /**
  10. * Create a new Redis cache driver instance.
  11. *
  12. * @param Laravel\Redis $redis
  13. * @return void
  14. */
  15. public function __construct(\Laravel\Redis $redis)
  16. {
  17. $this->redis = $redis;
  18. }
  19. /**
  20. * Determine if an item exists in the cache.
  21. *
  22. * @param string $key
  23. * @return bool
  24. */
  25. public function has($key)
  26. {
  27. return ( ! is_null($this->redis->get($key)));
  28. }
  29. /**
  30. * Retrieve an item from the cache driver.
  31. *
  32. * @param string $key
  33. * @return mixed
  34. */
  35. protected function retrieve($key)
  36. {
  37. if ( ! is_null($cache = $this->redis->get($key)))
  38. {
  39. return unserialize($cache);
  40. }
  41. }
  42. /**
  43. * Write an item to the cache for a given number of minutes.
  44. *
  45. * <code>
  46. * // Put an item in the cache for 15 minutes
  47. * Cache::put('name', 'Taylor', 15);
  48. * </code>
  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->forever($key, $value);
  58. $this->redis->expire($key, $minutes * 60);
  59. }
  60. /**
  61. * Write an item to the cache that lasts forever.
  62. *
  63. * @param string $key
  64. * @param mixed $value
  65. * @return void
  66. */
  67. public function forever($key, $value)
  68. {
  69. $this->redis->set($key, serialize($value));
  70. }
  71. /**
  72. * Delete an item from the cache.
  73. *
  74. * @param string $key
  75. * @return void
  76. */
  77. public function forget($key)
  78. {
  79. $this->redis->del($key);
  80. }
  81. /**
  82. * Flush the entire cache.
  83. *
  84. * @return void
  85. */
  86. public function flush()
  87. {
  88. $this->redis->flushdb();
  89. }
  90. }