1: <?php namespace Laravel\Cache\Drivers;
2:
3: class File extends Driver {
4:
5: /**
6: * The path to which the cache files should be written.
7: *
8: * @var string
9: */
10: protected $path;
11:
12: /**
13: * Create a new File cache driver instance.
14: *
15: * @param string $path
16: * @return void
17: */
18: public function __construct($path)
19: {
20: $this->path = $path;
21: }
22:
23: /**
24: * Determine if an item exists in the cache.
25: *
26: * @param string $key
27: * @return bool
28: */
29: public function has($key)
30: {
31: return ( ! is_null($this->get($key)));
32: }
33:
34: /**
35: * Retrieve an item from the cache driver.
36: *
37: * @param string $key
38: * @return mixed
39: */
40: protected function retrieve($key)
41: {
42: if ( ! file_exists($this->path.$key)) return null;
43:
44: // File based caches store have the expiration timestamp stored in
45: // UNIX format prepended to their contents. We'll compare the
46: // timestamp to the current time when we read the file.
47: if (time() >= substr($cache = file_get_contents($this->path.$key), 0, 10))
48: {
49: return $this->forget($key);
50: }
51:
52: return unserialize(substr($cache, 10));
53: }
54:
55: /**
56: * Write an item to the cache for a given number of minutes.
57: *
58: * <code>
59: * // Put an item in the cache for 15 minutes
60: * Cache::put('name', 'Taylor', 15);
61: * </code>
62: *
63: * @param string $key
64: * @param mixed $value
65: * @param int $minutes
66: * @return void
67: */
68: public function put($key, $value, $minutes)
69: {
70: if ($minutes <= 0) return;
71:
72: $value = $this->expiration($minutes).serialize($value);
73:
74: file_put_contents($this->path.$key, $value, LOCK_EX);
75: }
76:
77: /**
78: * Write an item to the cache for five years.
79: *
80: * @param string $key
81: * @param mixed $value
82: * @return void
83: */
84: public function forever($key, $value)
85: {
86: return $this->put($key, $value, 2628000);
87: }
88:
89: /**
90: * Delete an item from the cache.
91: *
92: * @param string $key
93: * @return void
94: */
95: public function forget($key)
96: {
97: if (file_exists($this->path.$key)) @unlink($this->path.$key);
98: }
99:
100: /**
101: * Flush the entire cache.
102: *
103: * @return void
104: */
105: public function flush()
106: {
107: array_map('unlink', glob($this->path.'*'));
108: }
109:
110: }
111: