laravel Cache 阅读心得

粗略阅读完  大概 知道几个东西

1.Store 接口  实际的操作缓存的接口

2.Repository 接口  个人感觉是Sotre代理接口   

3 CacheManager 是代理类

 

举例  

Cache::get()  

1 门面类 实际就是找到Cache门面对应的实际名称    可以查询到 是  cache     相当于 mak(‘cache’)

2 然后 调用get方法   CacheManager里面get是私有的  所以只能看有没有魔术方法 发现有  那就执行呗  

这个里面 有个store 方法

    /**
     * Get a cache store instance by name, wrapped in a repository.
     *
     * @param  string|null  $name
     * @return \Illuminate\Contracts\Cache\Repository
     */
    public function store($name = null)
    {
        $name = $name ?: $this->getDefaultDriver();

        return $this->stores[$name] = $this->get($name);
    }

  发现返回的是一个Repository类   这个类 

    protected function createFileDriver(array $config)
    {
        return $this->repository(new FileStore($this->app['files'], $config['path'], $config['permission'] ?? null));
    }

  但是这个Repository类 依赖 一个实际的Store接口的实现

 所以  调用get 方法  是通过调用Repository类的get方法   

    /**
     * Retrieve an item from the cache by key.
     *
     * @param  string  $key
     * @param  mixed  $default
     * @return mixed
     */
    public function get($key, $default = null)
    {
        if (is_array($key)) {
            return $this->many($key);
        }

        $value = $this->store->get($this->itemKey($key));

        // If we could not find the cache value, we will fire the missed event and get
        // the default value for this cache value. This default could be a callback
        // so we will execute the value function which will resolve it if needed.
        if (is_null($value)) {
            $this->event(new CacheMissed($key));

            $value = value($default);
        } else {
            $this->event(new CacheHit($key, $value));
        }

        return $value;
    }

  但是查看源码    Repository类的get方法  实际上还是调用的store里面的get方法  也就是说  Repository是store的代理   真正操作的还是store类

 

posted @ 2020-08-08 10:38  天梯小蔡  阅读(315)  评论(0编辑  收藏  举报