cache.php 1.94 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel; defined('DS') or die('No direct script access.');
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

	/**
	 * Get a cache driver instance.
	 *
15
	 * If no driver name is specified, the default will be returned.
Taylor Otwell committed
16
	 *
17 18 19 20 21 22 23 24
	 * <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
25
	 * @param  string        $driver
Phill Sparks committed
26
	 * @return Cache\Drivers\Driver
Taylor Otwell committed
27
	 */
28
	public static function driver($driver = null)
Taylor Otwell committed
29
	{
30
		if (is_null($driver)) $driver = Config::get('cache.driver');
Taylor Otwell committed
31

32
		if ( ! isset(static::$drivers[$driver]))
Taylor Otwell committed
33
		{
34
			static::$drivers[$driver] = static::factory($driver);
Taylor Otwell committed
35 36
		}

37
		return static::$drivers[$driver];
Taylor Otwell committed
38 39 40
	}

	/**
Taylor Otwell committed
41 42 43
	 * Create a new cache driver instance.
	 *
	 * @param  string  $driver
Phill Sparks committed
44
	 * @return Cache\Drivers\Driver
Taylor Otwell committed
45 46 47 48 49 50
	 */
	protected static function factory($driver)
	{
		switch ($driver)
		{
			case 'apc':
51
				return new Cache\Drivers\APC(Config::get('cache.key'));
Taylor Otwell committed
52 53

			case 'file':
Taylor Otwell committed
54
				return new Cache\Drivers\File(path('storage').'cache'.DS);
Taylor Otwell committed
55 56

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

			case 'redis':
60 61 62 63
				return new Cache\Drivers\Redis(Redis::db());

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

			default:
66
				throw new \Exception("Cache driver {$driver} is not supported.");
Taylor Otwell committed
67 68 69 70
		}
	}

	/**
71
	 * Magic Method for calling the methods on the default cache driver.
72 73
	 *
	 * <code>
74
	 *		// Call the "get" method on the default cache driver
75 76
	 *		$name = Cache::get('name');
	 *
77
	 *		// Call the "put" method on the default cache driver
78 79
	 *		Cache::put('name', 'Taylor', 15);
	 * </code>
Taylor Otwell committed
80
	 */
81
	public static function __callStatic($method, $parameters)
Taylor Otwell committed
82
	{
83
		return call_user_func_array(array(static::driver(), $method), $parameters);
Taylor Otwell committed
84 85
	}

86
}