driver.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php namespace Laravel\Cache\Drivers;
  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, $function = 'put')
  67. {
  68. if ( ! is_null($item = $this->get($key, null))) return $item;
  69. $this->$function($key, $default = value($default), $minutes);
  70. return $default;
  71. }
  72. /**
  73. * Get an item from the cache, or cache the default value forever.
  74. *
  75. * @param string $key
  76. * @param mixed $default
  77. * @return mixed
  78. */
  79. public function sear($key, $default)
  80. {
  81. return $this->remember($key, $default, null, 'forever');
  82. }
  83. /**
  84. * Delete an item from the cache.
  85. *
  86. * @param string $key
  87. * @return void
  88. */
  89. abstract public function forget($key);
  90. /**
  91. * Get the expiration time as a UNIX timestamp.
  92. *
  93. * @param int $minutes
  94. * @return int
  95. */
  96. protected function expiration($minutes)
  97. {
  98. return time() + ($minutes * 60);
  99. }
  100. }