用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类——也许现在对你来说更清楚了。