1: <?php namespace Laravel\Cache\Drivers;
2:
3: abstract class Driver {
4:
5: /**
6: * Determine if an item exists in the cache.
7: *
8: * @param string $key
9: * @return bool
10: */
11: abstract public function has($key);
12:
13: /**
14: * Get an item from the cache.
15: *
16: * <code>
17: * // Get an item from the cache driver
18: * $name = Cache::driver('name');
19: *
20: * // Return a default value if the requested item isn't cached
21: * $name = Cache::get('name', 'Taylor');
22: * </code>
23: *
24: * @param string $key
25: * @param mixed $default
26: * @return mixed
27: */
28: public function get($key, $default = null)
29: {
30: return ( ! is_null($item = $this->retrieve($key))) ? $item : value($default);
31: }
32:
33: /**
34: * Retrieve an item from the cache driver.
35: *
36: * @param string $key
37: * @return mixed
38: */
39: abstract protected function retrieve($key);
40:
41: /**
42: * Write an item to the cache for a given number of minutes.
43: *
44: * <code>
45: * // Put an item in the cache for 15 minutes
46: * Cache::put('name', 'Taylor', 15);
47: * </code>
48: *
49: * @param string $key
50: * @param mixed $value
51: * @param int $minutes
52: * @return void
53: */
54: abstract public function put($key, $value, $minutes);
55:
56: /**
57: * Get an item from the cache, or cache and return the default value.
58: *
59: * <code>
60: * // Get an item from the cache, or cache a value for 15 minutes
61: * $name = Cache::remember('name', 'Taylor', 15);
62: *
63: * // Use a closure for deferred execution
64: * $count = Cache::remember('count', function() { return User::count(); }, 15);
65: * </code>
66: *
67: * @param string $key
68: * @param mixed $default
69: * @param int $minutes
70: * @param string $function
71: * @return mixed
72: */
73: public function remember($key, $default, $minutes, $function = 'put')
74: {
75: if ( ! is_null($item = $this->get($key, null))) return $item;
76:
77: $this->$function($key, $default = value($default), $minutes);
78:
79: return $default;
80: }
81:
82: /**
83: * Get an item from the cache, or cache the default value forever.
84: *
85: * @param string $key
86: * @param mixed $default
87: * @return mixed
88: */
89: public function sear($key, $default)
90: {
91: return $this->remember($key, $default, null, 'forever');
92: }
93:
94: /**
95: * Delete an item from the cache.
96: *
97: * @param string $key
98: * @return void
99: */
100: abstract public function forget($key);
101:
102: /**
103: * Get the expiration time as a UNIX timestamp.
104: *
105: * @param int $minutes
106: * @return int
107: */
108: protected function expiration($minutes)
109: {
110: return time() + ($minutes * 60);
111: }
112:
113: }
114: