file.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php namespace Laravel\Cache\Drivers;
  2. class File extends Driver {
  3. /**
  4. * The path to which the cache files should be written.
  5. *
  6. * @var string
  7. */
  8. protected $path;
  9. /**
  10. * Create a new File cache driver instance.
  11. *
  12. * @param string $path
  13. * @return void
  14. */
  15. public function __construct($path)
  16. {
  17. $this->path = $path;
  18. }
  19. /**
  20. * Determine if an item exists in the cache.
  21. *
  22. * @param string $key
  23. * @return bool
  24. */
  25. public function has($key)
  26. {
  27. return ( ! is_null($this->get($key)));
  28. }
  29. /**
  30. * Retrieve an item from the cache driver.
  31. *
  32. * @param string $key
  33. * @return mixed
  34. */
  35. protected function retrieve($key)
  36. {
  37. if ( ! file_exists($this->path.$key)) return null;
  38. // File based caches store have the expiration timestamp stored in
  39. // UNIX format prepended to their contents. This timestamp is then
  40. // extracted and removed when the cache is read to determine if
  41. // the file is still valid.
  42. if (time() >= substr($cache = file_get_contents($this->path.$key), 0, 10))
  43. {
  44. return $this->forget($key);
  45. }
  46. return unserialize(substr($cache, 10));
  47. }
  48. /**
  49. * Write an item to the cache for a given number of minutes.
  50. *
  51. * <code>
  52. * // Put an item in the cache for 15 minutes
  53. * Cache::put('name', 'Taylor', 15);
  54. * </code>
  55. *
  56. * @param string $key
  57. * @param mixed $value
  58. * @param int $minutes
  59. * @return void
  60. */
  61. public function put($key, $value, $minutes)
  62. {
  63. $value = (time() + ($minutes * 60)).serialize($value);
  64. file_put_contents($this->path.$key, $value, LOCK_EX);
  65. }
  66. /**
  67. * Delete an item from the cache.
  68. *
  69. * @param string $key
  70. * @return void
  71. */
  72. public function forget($key)
  73. {
  74. if (file_exists($this->path.$key))
  75. {
  76. @unlink($this->path.$key);
  77. }
  78. }
  79. }