driver.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php namespace Laravel\Cache\Drivers; use Closure;
  2. abstract class Driver {
  3. /**
  4. * Determine if an item exists in the cache.
  5. *
  6. * @param string $key
  7. * @return bool
  8. */
  9. abstract public function has($key);
  10. /**
  11. * Get an item from the cache.
  12. *
  13. * <code>
  14. * // Get an item from the cache driver
  15. * $name = Cache::driver('name');
  16. *
  17. * // Return a default value if the requested item isn't cached
  18. * $name = Cache::get('name', 'Taylor');
  19. * </code>
  20. *
  21. * @param string $key
  22. * @param mixed $default
  23. * @param string $driver
  24. * @return mixed
  25. */
  26. public function get($key, $default = null)
  27. {
  28. return ( ! is_null($item = $this->retrieve($key))) ? $item : value($default);
  29. }
  30. /**
  31. * Retrieve an item from the cache driver.
  32. *
  33. * @param string $key
  34. * @return mixed
  35. */
  36. abstract protected function retrieve($key);
  37. /**
  38. * Write an item to the cache for a given number of minutes.
  39. *
  40. * <code>
  41. * // Put an item in the cache for 15 minutes
  42. * Cache::put('name', 'Taylor', 15);
  43. * </code>
  44. *
  45. * @param string $key
  46. * @param mixed $value
  47. * @param int $minutes
  48. * @return void
  49. */
  50. abstract public function put($key, $value, $minutes);
  51. /**
  52. * Get an item from the cache, or cache and return the default value.
  53. *
  54. * <code>
  55. * // Get an item from the cache, or cache a value for 15 minutes
  56. * $name = Cache::remember('name', 'Taylor', 15);
  57. *
  58. * // Use a closure for deferred execution
  59. * $count = Cache::remember('count', function() { return User::count(); }, 15);
  60. * </code>
  61. *
  62. * @param string $key
  63. * @param mixed $default
  64. * @param int $minutes
  65. * @return mixed
  66. */
  67. public function remember($key, $default, $minutes)
  68. {
  69. if ( ! is_null($item = $this->get($key, null))) return $item;
  70. $this->put($key, value($default), $minutes);
  71. return $default;
  72. }
  73. /**
  74. * Delete an item from the cache.
  75. *
  76. * @param string $key
  77. * @return void
  78. */
  79. abstract public function forget($key);
  80. /**
  81. * Get the expiration time as a UNIX timestamp.
  82. *
  83. * @param int $minutes
  84. * @return int
  85. */
  86. protected function expiration($minutes)
  87. {
  88. return time() + ($minutes * 60);
  89. }
  90. }