ZF-如何缓存部分布局 [英] ZF - How to cache parts of the layout

查看:48
本文介绍了ZF-如何缓存部分布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Stacic页面缓存(带有缓存动作帮助器)来缓存我的应用程序的大部分页面.
这是极快的,但并不总是合适的.

I use Stacic Page Cache (with cache action helper) to cache most of the pages of my App.
This is extremly fast, but not always suitable.

  • 如何缓存包含动态数据的页面?例如.布局包含特定于用户的信息.

我考虑的一种解决方案是通过Ajax加载其他数据.但就我而言,最好缓存部分页面(例如条目列表或侧边栏的一部分).

One solution I considered is to load additional data via Ajax. But in my case it would be better to cache parts of the pages (eg. list of entries or sidebar partial).

  • 有ZF建议的方法吗?例如.仅缓存视图,不缓存布局,反之亦然.

缓存动作帮助器提供了不错的界面来缓存所有动作.有什么解决方案来缓存页面内容或部分内容或视图助手?

Cache action helper provides nice interface to cache all the actions. Any solution to cache the page content or partials or view helpers?

推荐答案

我最近所做的是创建一个服务,然后使用数据库连接和缓存对象配置该服务.检索数据使用一种延迟加载级联",首先在内存中查找,然后在高速缓存中查找,然后再查找db.

What I've been doing lately is creating a service and then configuring that service with both a db connection and a cache object. Retrieving the data uses a kind of "lazy-loading cascade", looking first in memory, then in cache, then to the db.

例如,我的应用程序之一是针对一家房地产代理公司的,该代理公司在我们国家的多个(但不是全部)省份中运作.我们有一个省的数据库表,其中一些是为前端启用的,我们需要在各个位置渲染它们(例如,作为select元素中的选项).我们做类似的事情(我正在使用的旧代码库使用DAO对象进行数据库访问,并使用PEAR的Cache_Lite进行缓存,因此该示例严格来说不是Zend Framework,但原理同样适用):

For example, one of my apps is for a real-estate agency that operates in several - but not all - of the provinces in our country. We have a db-table of provinces, some of which are enabled for the front-end, and we need to render them in various places (say, as options in a select element). We do something like this (the legacy code-base on which I am working uses DAO objects for db access and PEAR's Cache_Lite for caching, so the example is not strictly Zend Framework, but the principle applies equally):

/**
 * A service for fetching provinces
 */
class My_Service_Provinces
{
    protected $_daoProvinces;
    protected $_provinces = array();
    protected $_cache;


    public function __construct($daoProvinces)
    {
        $this->setDaoProvinces($daoProvinces);
    }

    public function setDaoProvinces($daoProvinces)
    {
        $this->_daoProvinces = $daoProvinces;
        return $this;
    }

    public function getDaoProvinces()
    {
        return $this->_daoProvinces;
    }

    public function setCache($cache)
    {
        $this->_cache = $cache;
        return $this;
    }

    public function getCache()
    {
        if (null == $this->_cache){
            $this->_cache = new My_Cache_Provinces();
        }
        return $this->_cache;
    }

    public function getProvinces()
    {
        if (null == $this->_provinces){
            $cache = $this->getCache();
            $data = $cache->get();
            if (!$data){
                $dao = $this->getDaoProvinces();
                $rows = $dao->frontend();
                $data = array();
                while ($row = $rows->get_row()){
                    $data[$row['provinceId']] = $row;
                }
                $cache->save(serialize($data));
            } else {
                $data = unserialize($data);
            }
            $this->_provinces = $data;
        }
        return $this->_provinces;
    }

    public function getProvince($provinceId)
    {
        $provinces = $this->getProvinces();
        return isset($provinces[$provinceId]) ? $provinces[$provinceId] : null;
    }
}

已使用适当的生存期对高速缓存对象进行了预配置.我为很少更改的数据提供了较长的生命周期,为频繁更改的数据提供了较短的生命周期.如果我真的需要更改数据后才能立即将其提供给应用程序-例如,管理员添加了一个新的省份-我实例化缓存对象并在更新时清除缓存.

The cache object is pre-configured with whatever lifetime is appropriate. I give a long lifetime to seldom-changing data, shorter lifetimes to frequently-changing data. If I really need the change to the data to be immediately available to the app - say, the admin adds a new province - I instantiate the cache object and clear the cache on update.

我什至还添加了一个工厂来帮助实例化服务,从而使调用代码不必消耗依赖关系.调用代码(可能在控制器中,甚至在视图或视图帮助器中)看起来像:

I've even added a factory to help instantiate the service so that calling code does not have to sweat the dependencies. Calling code - perhaps in a controller or even in a view or view-helper - looks something like:

$service = My_Service_Factory::getService('provinces');
$provinces = $service->getProvinces();

知道不知道吗?

这篇关于ZF-如何缓存部分布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆