cache.php 2.41 KB
Newer Older
1
<?php namespace Laravel; use Closure;
2

3
class Cache {
Taylor Otwell committed
4 5 6 7

	/**
	 * All of the active cache drivers.
	 *
8
	 * @var array
Taylor Otwell committed
9
	 */
10
	public static $drivers = array();
Taylor Otwell committed
11 12

	/**
13 14 15 16 17 18 19
	 * The third-party driver registrar.
	 *
	 * @var array
	 */
	public static $registrar = array();

	/**
Taylor Otwell committed
20 21
	 * Get a cache driver instance.
	 *
22
	 * If no driver name is specified, the default will be returned.
Taylor Otwell committed
23
	 *
24 25 26 27 28 29 30 31
	 * <code>
	 *		// Get the default cache driver instance
	 *		$driver = Cache::driver();
	 *
	 *		// Get a specific cache driver instance by name
	 *		$driver = Cache::driver('memcached');
	 * </code>
	 *
Taylor Otwell committed
32
	 * @param  string        $driver
Phill Sparks committed
33
	 * @return Cache\Drivers\Driver
Taylor Otwell committed
34
	 */
35
	public static function driver($driver = null)
Taylor Otwell committed
36
	{
37
		if (is_null($driver)) $driver = Config::get('cache.driver');
Taylor Otwell committed
38

39
		if ( ! isset(static::$drivers[$driver]))
Taylor Otwell committed
40
		{
41
			static::$drivers[$driver] = static::factory($driver);
Taylor Otwell committed
42 43
		}

44
		return static::$drivers[$driver];
Taylor Otwell committed
45 46 47
	}

	/**
Taylor Otwell committed
48 49 50
	 * Create a new cache driver instance.
	 *
	 * @param  string  $driver
Phill Sparks committed
51
	 * @return Cache\Drivers\Driver
Taylor Otwell committed
52 53 54
	 */
	protected static function factory($driver)
	{
55 56
		if (isset(static::$registrar[$driver]))
		{
57 58 59
			$resolver = static::$registrar[$driver];

			return $resolver();
60 61
		}

Taylor Otwell committed
62 63 64
		switch ($driver)
		{
			case 'apc':
65
				return new Cache\Drivers\APC(Config::get('cache.key'));
Taylor Otwell committed
66 67

			case 'file':
Taylor Otwell committed
68
				return new Cache\Drivers\File(path('storage').'cache'.DS);
Taylor Otwell committed
69 70

			case 'memcached':
71
				return new Cache\Drivers\Memcached(Memcached::connection(), Config::get('cache.key'));
Taylor Otwell committed
72

73 74 75
			case 'memory':
				return new Cache\Drivers\Memory;

Taylor Otwell committed
76
			case 'redis':
77 78 79 80
				return new Cache\Drivers\Redis(Redis::db());

			case 'database':
				return new Cache\Drivers\Database(Config::get('cache.key'));
Taylor Otwell committed
81 82

			default:
83
				throw new \Exception("Cache driver {$driver} is not supported.");
Taylor Otwell committed
84 85 86 87
		}
	}

	/**
88 89 90 91 92 93
	 * Register a third-party cache driver.
	 *
	 * @param  string   $driver
	 * @param  Closure  $resolver
	 * @return void
	 */
94
	public static function extend($driver, Closure $resolver)
95 96 97 98 99
	{
		static::$registrar[$driver] = $resolver;
	}

	/**
100
	 * Magic Method for calling the methods on the default cache driver.
101 102
	 *
	 * <code>
103
	 *		// Call the "get" method on the default cache driver
104 105
	 *		$name = Cache::get('name');
	 *
106
	 *		// Call the "put" method on the default cache driver
107 108
	 *		Cache::put('name', 'Taylor', 15);
	 * </code>
Taylor Otwell committed
109
	 */
110
	public static function __callStatic($method, $parameters)
Taylor Otwell committed
111
	{
112
		return call_user_func_array(array(static::driver(), $method), $parameters);
Taylor Otwell committed
113 114
	}

115
}