laravel cache get是怎么调用的?

发布时间 - 2021-05-08 00:00:00    点击率:

下面由laravel教程栏目给大家介绍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)

laravel 中 Facades的原理以及代码剖析。


# php  # php7  # laravel  # bootstrap  # public  # http  # 的是  # 给大家  # 来自于  # 如何做  # 实现了  # Events  # RequestHandled  # dispatch  # FatalThrowableError 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?  浅谈javascript alert和confirm的美化  百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏  如何在万网利用已有域名快速建站?  iOS发送验证码倒计时应用  家族网站制作贴纸教程视频,用豆子做粘帖画怎么制作?  Laravel如何构建RESTful API_Laravel标准化API接口开发指南  Win11怎么查看显卡温度 Win11任务管理器查看GPU温度【技巧】  JS碰撞运动实现方法详解  DeepSeek是免费使用的吗 DeepSeek收费模式与Pro版本功能详解  广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?  Python3.6正式版新特性预览  如何在IIS中配置站点IP、端口及主机头?  Claude怎样写约束型提示词_Claude约束提示词写法【教程】  长沙企业网站制作哪家好,长沙水业集团官方网站?  如何快速搭建FTP站点实现文件共享?  如何快速搭建安全的FTP站点?  Laravel怎么实现搜索功能_Laravel使用Eloquent实现模糊查询与多条件搜索【实例】  制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?  Angular 表单中正确绑定输入值以确保提交与验证正常工作  Android GridView 滑动条设置一直显示状态(推荐)  Python自然语言搜索引擎项目教程_倒排索引查询优化案例  如何在不使用负向后查找的情况下匹配特定条件前的换行符  Python结构化数据采集_字段抽取解析【教程】  电视网站制作tvbox接口,云海电视怎样自定义添加电视源?  在centOS 7安装mysql 5.7的详细教程  Laravel Blade组件怎么用_Laravel可复用视图组件的创建与使用  如何在万网ECS上快速搭建专属网站?  Laravel如何使用Facades(门面)及其工作原理_Laravel门面模式与底层机制  如何使用 jQuery 正确渲染 Instagram 风格的标签列表  Laravel怎么在Controller之外的地方验证数据  Laravel如何使用Vite进行前端资源打包?(配置示例)  Laravel怎么实现验证码功能_Laravel集成验证码库防止机器人注册  Laravel storage目录权限问题_Laravel文件写入权限设置  Laravel如何使用Spatie Media Library_Laravel图片上传管理与缩略图生成【步骤】  Laravel的.env文件有什么用_Laravel环境变量配置与管理详解  微信小程序 HTTPS报错整理常见问题及解决方案  Laravel如何实现API资源集合?(Resource Collection教程)  Laravel如何获取当前用户信息_Laravel Auth门面获取用户ID  Laravel怎么写单元测试_PHPUnit在Laravel项目中的基础测试入门  Laravel如何创建自定义中间件?(Middleware代码示例)  最好的网站制作公司,网购哪个网站口碑最好,推荐几个?谢谢?  edge浏览器无法安装扩展 edge浏览器插件安装失败【解决方法】  高端云建站费用究竟需要多少预算?  如何在腾讯云免费申请建站?  网页设计与网站制作内容,怎样注册网站?  独立制作一个网站多少钱,建立网站需要花多少钱?  Laravel的Blade指令怎么自定义_创建你自己的Laravel Blade Directives  Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】  什么是JavaScript解构赋值_解构赋值有哪些实用技巧