【Laravel3.0.0源码阅读分析】缓存类cache.php
作者:互联网
<?php namespace Laravel; defined('DS') or die('No direct script access.');
// 缓存类
class Cache {
/**
* All of the active cache drivers.
* 所有可用的缓存驱动
* @var array
*/
public static $drivers = array();
/**
* Get a cache driver instance.
* 获取一个缓存驱动实例
* If no driver name is specified, the default will be returned.
* 如果没有指定驱动名称,默认的驱动将会被返回。
* <code>
* // Get the default cache driver instance
* // 获取默认的缓存驱动实例
* $driver = Cache::driver();
*
* // Get a specific cache driver instance by name
* // 通过名称获取指定的缓存驱动
* $driver = Cache::driver('memcached');
* </code>
*
* @param string $driver
* @return Cache\Drivers\Driver
*/
public static function driver($driver = null)
{
if (is_null($driver)) $driver = Config::get('cache.driver');
if ( ! isset(static::$drivers[$driver]))
{
static::$drivers[$driver] = static::factory($driver);
}
return static::$drivers[$driver];
}
/**
* Create a new cache driver instance.
* 创建一个新的缓存驱动实例
* @param string $driver
* @return Cache\Drivers\Driver
*/
protected static function factory($driver)
{
switch ($driver)
{
case 'apc':
return new Cache\Drivers\APC(Config::get('cache.key'));
case 'file':
return new Cache\Drivers\File(path('storage').'cache'.DS);
case 'memcached':
return new Cache\Drivers\Memcached(Memcached::connection(), Config::get('cache.key'));
case 'redis':
return new Cache\Drivers\Redis(Redis::db());
case 'database':
return new Cache\Drivers\Database(Config::get('cache.key'));
default:
throw new \Exception("Cache driver {$driver} is not supported.");
}
}
/**
* Magic Method for calling the methods on the default cache driver.
* 调用默认缓存驱动的魔术方法
* <code>
* // Call the "get" method on the default cache driver
* // 在默认缓存驱动上调用get方法
* $name = Cache::get('name');
*
* // Call the "put" method on the default cache driver
* // 在默认缓存驱动调用put方法
* Cache::put('name', 'Taylor', 15);
* </code>
*/
// __callStatic 当调用的静态方法不存在,会自动调用__callStatic方法
public static function __callStatic($method, $parameters)
{
// 调用回调函数,并把一个数组参数作为回调函数参数
return call_user_func_array(array(static::driver(), $method), $parameters);
}
}
github地址: https://github.com/liu-shilong/laravel3-scr
标签:cache,return,get,Cache,driver,Laravel3.0,源码,static 来源: https://blog.csdn.net/qq2942713658/article/details/117374864