2

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

<?php
    class View {          
        public function render ( $template , $path = null ) { } 
            // ...  
        }

这是我的“MainView”,其中类在所有其他视图中扩展,例如“ClientsView”..等。

但是,我想实现一种方法来拦截投降的请求,通过缓存,当我将此参数传递给渲染方法时,我对缓存说,或者什么..我只是想保持控制..所以我有一个“ViewCacheStorage”,您将在其中存储缓存的文件,以及每个缓存到期的剩余时间,在我不必动摇主视图的情况下,最好的方法是什么?

4

1 回答 1

1

一个简单的选择:

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 扩展而来。

于 2011-12-16T22:36:15.430 回答