Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP查看缓存_Php_Caching_View_Include_Intercept - Fatal编程技术网

用PHP查看缓存

用PHP查看缓存,php,caching,view,include,intercept,Php,Caching,View,Include,Intercept,我有一个关于视图缓存的问题,假设我有以下代码块: <?php class View { public function render ( $template , $path = null ) { } // ... } 一个简单的选项: class CachingView extends View { protected $cacheStorage; public function

我有一个关于视图缓存的问题,假设我有以下代码块:

<?php
    class View {          
        public function render ( $template , $path = null ) { } 
            // ...  
        }
一个简单的选项:

class CachingView extends View {

    protected $cacheStorage;

    public function render($template, $path = null) {
        if (! $this->getCacheStorage()->has($template)) {
           $this->getCacheStorage()->store(parent::render($template, $path));
        }

        return $this->getCacheStorage()->get($template);
    }

    public function getCacheStorage() {
        if (empty($this->cacheStorage)) {
            $this->cacheStorage = new ViewCacheStorage();
        }
        return $this->cacheStorage;
    }
}

然后所有其他视图都从CachingView扩展。

您所说的“不必摇动主视图”是什么意思?不添加代码在主视图中创建视图缓存。为什么渲染方法作为视图时有参数?因为此方法是渲染模板,所以在这种情况下,其他视图会调用它,我需要缓存的类,是吗?我不太理解你的问题,但我已经更新了我的代码,包含了一个CacheStorage类——也许现在对你来说更清楚了。