driver.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php namespace Laravel\Cache;
  2. abstract class Driver {
  3. /**
  4. * Determine if an item exists in the cache.
  5. *
  6. * <code>
  7. * // Determine if the "name" item exists in the cache
  8. * $exists = Cache::driver()->has('name');
  9. * </code>
  10. *
  11. * @param string $key
  12. * @return bool
  13. */
  14. abstract public function has($key);
  15. /**
  16. * Get an item from the cache.
  17. *
  18. * A default value may also be specified, and will be returned in the requested
  19. * item does not exist in the cache.
  20. *
  21. * <code>
  22. * // Get the "name" item from the cache
  23. * $name = Cache::driver()->get('name');
  24. *
  25. * // Get the "name" item from the cache or return "Fred"
  26. * $name = Cache::driver()->get('name', 'Fred');
  27. * </code>
  28. *
  29. * @param string $key
  30. * @param mixed $default
  31. * @param string $driver
  32. * @return mixed
  33. */
  34. public function get($key, $default = null)
  35. {
  36. if ( ! is_null($item = $this->retrieve($key))) return $item;
  37. return (is_callable($default)) ? call_user_func($default) : $default;
  38. }
  39. /**
  40. * Retrieve an item from the cache driver.
  41. *
  42. * @param string $key
  43. * @return mixed
  44. */
  45. abstract protected function retrieve($key);
  46. /**
  47. * Write an item to the cache for a given number of minutes.
  48. *
  49. * <code>
  50. * // Write the "name" item to the cache for 30 minutes
  51. * Cache::driver()->put('name', 'Fred', 30);
  52. * </code>
  53. *
  54. * @param string $key
  55. * @param mixed $value
  56. * @param int $minutes
  57. * @return void
  58. */
  59. abstract public function put($key, $value, $minutes);
  60. /**
  61. * Get an item from the cache. If the item doesn't exist in the cache, store
  62. * the default value in the cache and return it.
  63. *
  64. * <code>
  65. * // Get the "name" item from the cache or store "Fred" for 30 minutes
  66. * $name = Cache::driver()->remember('name', 'Fred', 30);
  67. * </code>
  68. *
  69. * @param string $key
  70. * @param mixed $default
  71. * @param int $minutes
  72. * @return mixed
  73. */
  74. public function remember($key, $value, $minutes)
  75. {
  76. if ( ! is_null($item = $this->get($key, null))) return $item;
  77. $default = is_callable($default) ? call_user_func($default) : $default;
  78. $this->put($key, $default, $minutes);
  79. return $default;
  80. }
  81. /**
  82. * Delete an item from the cache.
  83. *
  84. * @param string $key
  85. * @return void
  86. */
  87. abstract public function forget($key);
  88. }