laravel cache get 是如何调用的?
本文使用版本为laravel5.5
cache get
public function cache()
{
$c=\Cache::get('app');
if(!$c) {
\Cache::put('app', 'cache', 1);
}
dump($c);//cache
}
config/app.php
'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
'Artisan' => Illuminate\Support\Facades\Artisan::class,
'Auth' => Illuminate\Support\Facades\Auth::class,
'Blade' => Illuminate\Support\Facades\Blade::class,
'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
'Bus' => Illuminate\Support\Facades\Bus::class,
'Cache' => Illuminate\Support\Facades\Cache::class,
]
使用cache实际调用的是Illuminate\Support\Facades\Cache
,这个映射是如何做的?
public/index.php
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
bootstarp/app.php
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
app/http/kernel.php
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
}
Illuminate/Foundation/Http/Kernel.php
public function handle($request)
{
try {
$request->enableHttpMethodParameterOverride();
$response = $this->sendRequestThroughRouter($request);
} catch (Exception $e) {
$this->reportException($e);
$response = $this->renderException($request, $e);
} catch (Throwable $e) {
$this->reportException($e = new FatalThrowableError($e));
$response = $this->renderException($request, $e);
}
$this->app['events']->dispatch(
new Events\RequestHandled($request, $response)
);
return $response;
}
protected function sendRequestThroughRouter($request)
{
$this->app->instance('request', $request);
Facade::clearResolvedInstance('request');
$this->bootstrap();
return (new Pipeline($this->app))
->send($request)
->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
->then($this->dispatchToRouter());
}
public function bootstrap()
{
if (! $this->app->hasBeenBootstrapped()) {
$this->app->bootstrapWith($this->bootstrappers());
}
}
Illuminate/Foundation/Application.php
public function bootstrapWith(array $bootstrappers)
{
$this->hasBeenBootstrapped = true;
foreach ($bootstrappers as $bootstrapper) {
$this['events']->fire('bootstrapping: '.$bootstrapper, [$this]);
$this->make($bootstrapper)->bootstrap($this);
$this['events']->fire('bootstrapped: '.$bootstrapper, [$this]);
}
}
Illuminate/Foundation/Bootstrap/RegisterFacades.php
public function bootstrap(Application $app)
{
Facade::clearResolvedInstances();
Facade::setFacadeApplication($app);
//将config/app.php 里的aliases数组里面的Facades类设置别名
AliasLoader::getInstance(array_merge(
$app->make('config')->get('app.aliases', []),
$app->make(PackageManifest::class)->aliases()
))->register();
}
Illuminate/Foundation/AliasLoader.php
public function load($alias)
{
if (static::$facadeNamespace && strpos($alias, static::$facadeNamespace) === 0) {
$this->loadFacade($alias);
return true;
}
// $alias来自于config/app.php中aliases数组
if (isset($this->aliases[$alias])) {
//'Route' => Illuminate\Support\Facades\Route::class,
// class_alias 为一个类创建别名
return class_alias($this->aliases[$alias], $alias);
}
}
Illuminate/Support/Facades/Cache.php
class Cache extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'cache';
}
}
Illuminate/Support/Facades/Facade.php
这个文件没有get set ,只有__callStatic
public static function __callStatic($method, $args)
{
$instance = static::getFacadeRoot();
if (! $instance) {
throw new RuntimeException('A facade root has not been set.');
}
return $instance->$method(...$args);
}
public static function getFacadeRoot()
{
return static::resolveFacadeInstance(static::getFacadeAccessor());
}
protected static function resolveFacadeInstance($name)
{
//这里$name为cache
if (is_object($name)) {
return $name;
}
if (isset(static::$resolvedInstance[$name])) {
return static::$resolvedInstance[$name];
}
//$app是容器对象,实现了ArrayAccess接口,最终调用的还是容器的make方法
return static::$resolvedInstance[$name] = static::$app[$name];
}
IlluminateContainerContainer.php
public function make($abstract, array $parameters = [])
{
return $this->resolve($abstract, $parameters);
}
protected function resolve($abstract, $parameters = [])
{
$abstract = $this->getAlias($abstract);
$needsContextualBuild = ! empty($parameters) || ! is_null(
$this->getContextualConcrete($abstract)
);
// If an instance of the type is currently being managed as a singleton we'll
// just return an existing instance instead of instantiating new instances
// so the developer can keep using the same objects instance every time.
if (isset($this->instances[$abstract]) && ! $needsContextualBuild) {
return $this->instances[$abstract];
}
$this->with[] = $parameters;
$concrete = $this->getConcrete($abstract);
// We're ready to instantiate an instance of the concrete type registered for
// the binding. This will instantiate the types, as well as resolve any of
// its "nested" dependencies recursively until all have gotten resolved.
if ($this->isBuildable($concrete, $abstract)) {
$object = $this->build($concrete);
} else {
$object = $this->make($concrete);
}
// If we defined any extenders for this type, we'll need to spin through them
// and apply them to the object being built. This allows for the extension
// of services, such as changing configuration or decorating the object.
foreach ($this->getExtenders($abstract) as $extender) {
$object = $extender($object, $this);
}
// If the requested type is registered as a singleton we'll want to cache off
// the instances in "memory" so we can return it later without creating an
// entirely new instance of an object on each subsequent request for it.
if ($this->isShared($abstract) && ! $needsContextualBuild) {
$this->instances[$abstract] = $object;
}
$this->fireResolvingCallbacks($abstract, $object);
// Before returning, we will also set the resolved flag to "true" and pop off
// the parameter overrides for this build. After those two things are done
// we will be ready to return back the fully constructed class instance.
$this->resolved[$abstract] = true;
array_pop($this->with);
return $object;
}
Illuminate/Cache/CacheServiceProvider.php
public function register()
{
$this->app->singleton('cache', function ($app) {
return new CacheManager($app);
});
$this->app->singleton('cache.store', function ($app) {
return $app['cache']->driver();
});
$this->app->singleton('memcached.connector', function () {
return new MemcachedConnector;
});
}
get set
$instance->$method(...$args)
来源:https://segmentfault.com/a/1190000017805524
laravel cache get 是如何调用的?的更多相关文章
- Laravel Cache 缓存钉钉微应用的 Access Token
钉钉微应用的 Access token 如何获取? Access_Token 是企业访问钉钉开放平台全局接口的唯一凭证,即调用接口时需携带Access_Token.从接口列表看,所有接口都需要携带 a ...
- Laravel在不同的环境调用不同的配置文件
Laravel在不同的环境调用不同的配置文件 Laravel如何在不同的环境调用不同的配置文件?社区这个问题问的蛮多,如何优雅的方法实现呢,应该有好多方法吧,我一般习惯用两种方法,设置环境变量,或 ...
- php – Laravel 5查询关系导致“调用成员函数addEagerConstraints()on null”错误( 转载)
php – Laravel 5查询关系导致“调用成员函数addEagerConstraints()on null”错误 我一直在尝试创建一个简单的用户管理系统,但在查询关系时不断遇到障碍.例如,我 ...
- Laravel Cache 使用
在项目中使用 laravel 的 cache 时,使用下面形式方法: $value = Cache::remember('users', $minutes, function() { return D ...
- Laravel Cache 的缓存文件在到期后是否会自动删除
验证缓存文件是否会自动删除的目的是,防止产生大量的缓存文件,占满磁盘.因为,我最近越来越多的使用 cache 来缓存各类 token. 使用的是 file 作为 CACHE_DRIVER CACHE_ ...
- laravel Cache store [] is not defined
去这个网站学习一下也好 https://laravel-china.org/topics/2093/laravel-source-analysis-series-cache#0b2791 如果env ...
- 关于Laravel中使用response()方法调用json()返回数据unicode编码转换的问题解决
在网上找了好久没有找到,之后一步一步测试,发现了Laravel还是很强大的,解决方案如下: public function response(){ // 返回json数据 $data = [ 'err ...
- Laravel Cache 缓存使用
导入:use Cache; Cache::put('key', 'value', $minutes); 添加一个缓存 Cache 门面的 get 方法用于从缓存中获取缓存项,如果缓存项不存在,返回 n ...
- laravel(一):如何安装laravel
1.前提条件 本文针对想从零开始开发 Laravel 程序的初学者,不需要预先具备任何的 Laravel 使用经验.不过,为了能顺利阅读,还是需要事先安装好一些软件: PHP 5.4 及以上版本 包管 ...
随机推荐
- android 官网访问不了
网上搜到的解决方案,亲测有用.记下来,以备遗忘. 使用管理员权限,修改C:\Windows\System32\Drivers\etc\hosts文件,加入以下内容 173.194.127.7 deve ...
- 如何解读IL代码
如何解读IL代码 关于IL代码,我有将从三个方面去揭开它神秘的面纱.IL代码是什么?我们为什么要去读懂IL代码?我们如何去读懂IL代码?这三个问题的解答,将是我解读IL代码的整体思路. IL代码是什么 ...
- 自定义input文件上传 file的提示文字及样式
简单记录一下 效果图: 代码: <input class="aload" type='button' value='上传附件' onClick='javascript:$(& ...
- c# 的默认访问修饰符(转)
c# 的访问修饰符是private 还是 internal? 准确的说,不能一概而论. 类(class)或结构(struct)如果不是在其它类或结构中的话,它的访问类型要不就是internal, 要不 ...
- 合唱队(华为OJ)
描述 计算最少出列多少位同学,使得剩下的同学排成合唱队形 说明: N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左 ...
- C++中的虚函数表
(感谢http://blog.csdn.net/haoel/article/details/1948051/) C++中的虚函数的作用主要是实现了多态的机制. 多态,简而言之就是用父类型别的指针指向其 ...
- .net core 共享 .Net Forms Authentication cookie
Asp.net 项目迁移到 asp.net core 项目后需要 兼容以前老的项目的登录方式. Forms Authentication cookie 登录. 从网上搜集到关于这个问题的解决思路都没有 ...
- 《ArcGIS Runtime SDK for Android开发笔记》——翻译:ArcGIS Runtime SDK for Android 10.2.7发布
ArcGIS Runtime SDK for Android v10.2.7 released by Dan O'Neill on October 1, 2015(发布时间:2015年10月1日) W ...
- Mac下配置apach服务
有的时候,我们需要在内网工作组中分享一些文件或是后台接口没有及时给出,你又想要模拟真实数据,直接在项目里创建plist也可以做到这种需求,但难免让工程变得冗余且看起来比较Low.这个时候就看出配置本地 ...
- 如何让MVC和多层架构和谐并存(二)
上一节说了一些笼统的东西,这节说一些实际的操作. 1.取列表.这是一个新闻列表: 对应MVC的model是: public class NewsListModel { /// < ...