driver.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php namespace Laravel\Cache;
  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. * A default value may also be specified, and will be returned in the requested
  14. * item does not exist in the cache.
  15. *
  16. * @param string $key
  17. * @param mixed $default
  18. * @param string $driver
  19. * @return mixed
  20. */
  21. public function get($key, $default = null)
  22. {
  23. if ( ! is_null($item = $this->retrieve($key))) return $item;
  24. return ($default instanceof \Closure) ? call_user_func($default) : $default;
  25. }
  26. /**
  27. * Retrieve an item from the cache driver.
  28. *
  29. * @param string $key
  30. * @return mixed
  31. */
  32. abstract protected function retrieve($key);
  33. /**
  34. * Write an item to the cache for a given number of minutes.
  35. *
  36. * @param string $key
  37. * @param mixed $value
  38. * @param int $minutes
  39. * @return void
  40. */
  41. abstract public function put($key, $value, $minutes);
  42. /**
  43. * Get an item from the cache. If the item doesn't exist in the cache, store
  44. * the default value in the cache and return it.
  45. *
  46. * @param string $key
  47. * @param mixed $default
  48. * @param int $minutes
  49. * @return mixed
  50. */
  51. public function remember($key, $value, $minutes)
  52. {
  53. if ( ! is_null($item = $this->get($key, null))) return $item;
  54. $default = ($default instanceof \Closure) ? call_user_func($default) : $default;
  55. $this->put($key, $default, $minutes);
  56. return $default;
  57. }
  58. /**
  59. * Delete an item from the cache.
  60. *
  61. * @param string $key
  62. * @return void
  63. */
  64. abstract public function forget($key);
  65. }