driver.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * @return mixed
  24. */
  25. public function get($key, $default = null)
  26. {
  27. return ( ! is_null($item = $this->retrieve($key))) ? $item : value($default);
  28. }
  29. /**
  30. * Retrieve an item from the cache driver.
  31. *
  32. * @param string $key
  33. * @return mixed
  34. */
  35. abstract protected function retrieve($key);
  36. /**
  37. * Write an item to the cache for a given number of minutes.
  38. *
  39. * <code>
  40. * // Put an item in the cache for 15 minutes
  41. * Cache::put('name', 'Taylor', 15);
  42. * </code>
  43. *
  44. * @param string $key
  45. * @param mixed $value
  46. * @param int $minutes
  47. * @return void
  48. */
  49. abstract public function put($key, $value, $minutes);
  50. /**
  51. * Get an item from the cache, or cache and return the default value.
  52. *
  53. * <code>
  54. * // Get an item from the cache, or cache a value for 15 minutes
  55. * $name = Cache::remember('name', 'Taylor', 15);
  56. *
  57. * // Use a closure for deferred execution
  58. * $count = Cache::remember('count', function() { return User::count(); }, 15);
  59. * </code>
  60. *
  61. * @param string $key
  62. * @param mixed $default
  63. * @param int $minutes
  64. * @return mixed
  65. */
  66. public function remember($key, $default, $minutes)
  67. {
  68. if ( ! is_null($item = $this->get($key, null))) return $item;
  69. $this->put($key, $default = value($default), $minutes);
  70. return $default;
  71. }
  72. /**
  73. * Delete an item from the cache.
  74. *
  75. * @param string $key
  76. * @return void
  77. */
  78. abstract public function forget($key);
  79. /**
  80. * Get the expiration time as a UNIX timestamp.
  81. *
  82. * @param int $minutes
  83. * @return int
  84. */
  85. protected function expiration($minutes)
  86. {
  87. return time() + ($minutes * 60);
  88. }
  89. }