memory.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php namespace Laravel\Cache\Drivers;
  2. class Memory extends Driver {
  3. /**
  4. * The in-memory array of cached items.
  5. *
  6. * @var string
  7. */
  8. protected $storage = array();
  9. /**
  10. * Determine if an item exists in the cache.
  11. *
  12. * @param string $key
  13. * @return bool
  14. */
  15. public function has($key)
  16. {
  17. return ( ! is_null($this->get($key)));
  18. }
  19. /**
  20. * Retrieve an item from the cache driver.
  21. *
  22. * @param string $key
  23. * @return mixed
  24. */
  25. protected function retrieve($key)
  26. {
  27. if (array_key_exists($key, $this->storage))
  28. {
  29. return $this->storage[$key];
  30. }
  31. }
  32. /**
  33. * Write an item to the cache for a given number of minutes.
  34. *
  35. * <code>
  36. * // Put an item in the cache for 15 minutes
  37. * Cache::put('name', 'Taylor', 15);
  38. * </code>
  39. *
  40. * @param string $key
  41. * @param mixed $value
  42. * @param int $minutes
  43. * @return void
  44. */
  45. public function put($key, $value, $minutes)
  46. {
  47. $this->storage[$key] = $value;
  48. }
  49. /**
  50. * Write an item to the cache that lasts forever.
  51. *
  52. * @param string $key
  53. * @param mixed $value
  54. * @return void
  55. */
  56. public function forever($key, $value)
  57. {
  58. $this->put($key, $value, 0);
  59. }
  60. /**
  61. * Delete an item from the cache.
  62. *
  63. * @param string $key
  64. * @return void
  65. */
  66. public function forget($key)
  67. {
  68. unset($this->storage[$key]);
  69. }
  70. /**
  71. * Flush the entire cache.
  72. *
  73. * @return void
  74. */
  75. public function flush()
  76. {
  77. $this->storage = array();
  78. }
  79. }