driver.php 2.38 KB
Newer Older
1
<?php namespace Laravel\Cache\Drivers;
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
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return mixed
	 */
Taylor Otwell committed
28 29
	public function get($key, $default = null)
	{
30
		return ( ! is_null($item = $this->retrieve($key))) ? $item : value($default);
Taylor Otwell committed
31
	}
Taylor Otwell committed
32 33

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

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

	/**
57
	 * Get an item from the cache, or cache and return the default value.
Taylor Otwell committed
58
	 *
59
	 * <code>
60
	 *		// Get an item from the cache, or cache a value for 15 minutes
61 62 63 64 65 66
	 *		$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
67 68 69 70 71
	 * @param  string  $key
	 * @param  mixed   $default
	 * @param  int     $minutes
	 * @return mixed
	 */
Taylor Otwell committed
72
	public function remember($key, $default, $minutes, $function = 'put')
Taylor Otwell committed
73
	{
Taylor Otwell committed
74
		if ( ! is_null($item = $this->get($key, null))) return $item;
Taylor Otwell committed
75

Taylor Otwell committed
76
		$this->$function($key, $default = value($default), $minutes);
Taylor Otwell committed
77 78 79 80 81

		return $default;
	}

	/**
Taylor Otwell committed
82 83 84 85 86 87 88 89 90 91 92 93
	 * Get an item from the cache, or cache the default value forever.
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return mixed
	 */
	public function sear($key, $default)
	{
		return $this->remember($key, $default, null, 'forever');
	}

	/**
Taylor Otwell committed
94 95 96 97 98 99 100
	 * Delete an item from the cache.
	 *
	 * @param  string  $key
	 * @return void
	 */
	abstract public function forget($key);

101 102 103 104 105 106 107 108 109 110 111
	/**
	 * Get the expiration time as a UNIX timestamp.
	 *
	 * @param  int  $minutes
	 * @return int
	 */
	protected function expiration($minutes)
	{
		return time() + ($minutes * 60);
	}

112
}