driver.php 2.3 KB

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