driver.php 2.13 KB
Newer Older
1
<?php namespace Laravel\Cache\Drivers; use Closure;
2

Taylor Otwell committed
3 4 5 6 7 8 9 10 11 12 13 14 15
abstract class Driver {

	/**
	 * Determine if an item exists in the cache.
	 *
	 * @param  string  $key
	 * @return bool
	 */
	abstract public function has($key);

	/**
	 * Get an item from the cache.
	 *
16 17 18 19 20 21 22
	 * <code>
	 *		// Get an item from the cache driver
	 *		$name = Cache::driver('name');
	 *
	 *		// Return a default value if the requested item isn't cached
	 *		$name = Cache::get('name', 'Taylor');
	 * </code>
Taylor Otwell committed
23 24 25 26 27 28
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @param  string  $driver
	 * @return mixed
	 */
Taylor Otwell committed
29 30 31 32
	public function get($key, $default = null)
	{
		if ( ! is_null($item = $this->retrieve($key))) return $item;

33
		return ($default instanceof Closure) ? call_user_func($default) : $default;
Taylor Otwell committed
34
	}
Taylor Otwell committed
35 36

	/**
Taylor Otwell committed
37
	 * Retrieve an item from the cache driver.
Taylor Otwell committed
38
	 *
Taylor Otwell committed
39
	 * @param  string  $key
Taylor Otwell committed
40 41
	 * @return mixed
	 */
Taylor Otwell committed
42
	abstract protected function retrieve($key);
Taylor Otwell committed
43 44

	/**
Taylor Otwell committed
45 46
	 * Write an item to the cache for a given number of minutes.
	 *
47 48 49 50 51
	 * <code>
	 *		// Put an item in the cache for 15 minutes
	 *		Cache::put('name', 'Taylor', 15);
	 * </code>
	 *
Taylor Otwell committed
52 53 54 55 56 57 58 59
	 * @param  string  $key
	 * @param  mixed   $value
	 * @param  int     $minutes
	 * @return void
	 */
	abstract public function put($key, $value, $minutes);

	/**
60 61
	 * Get an item from the cache. If the item doesn't exist in the
	 * cache, store the default value in the cache and return it.
Taylor Otwell committed
62
	 *
63
	 * <code>
64
	 *		// Get an item from the cache, or cache a value for 15 minutes
65 66 67 68 69 70
	 *		$name = Cache::remember('name', 'Taylor', 15);
	 *
	 *		// Use a closure for deferred execution
	 *		$count = Cache::remember('count', function() { return User::count(); }, 15);
	 * </code>
	 *
Taylor Otwell committed
71 72 73 74 75 76 77
	 * @param  string  $key
	 * @param  mixed   $default
	 * @param  int     $minutes
	 * @return mixed
	 */
	public function remember($key, $value, $minutes)
	{
Taylor Otwell committed
78
		if ( ! is_null($item = $this->get($key, null))) return $item;
Taylor Otwell committed
79

80
		$default = ($default instanceof Closure) ? call_user_func($default) : $default;
Taylor Otwell committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

		$this->put($key, $default, $minutes);

		return $default;
	}

	/**
	 * Delete an item from the cache.
	 *
	 * @param  string  $key
	 * @return void
	 */
	abstract public function forget($key);

}