driver.php 2.4 KB

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