php - 使用 Memcache 和 Doctrine 1.2 进行多域缓存

标签 php mysql doctrine memcached

所以,我有一个问题,一个引擎上有五个域,引擎使用 Doctrine 1.2 ORM,所有查询都使用 memcache(Doctrine_Cache_Memcache) 进行缓存。如何为每个域创建 key 前缀并通过domainprefix_key从缓存中获取?谢谢。

最佳答案

您可以创建一个派生类来扩展或组合 Doctrine_Cache_Memcache。在派生中,您只需在将执行传递给 Doctrine_Cache_Memcache 之前通过在 key 的域部分前面添加来修改 id

这是一个考虑使用继承、重写_doSave方法的示例;其他公共(public)成员可以以类似的方式覆盖。

<?php
class DomainCache extends Doctrine_Cache_Memcache
{
    private function _getDomain()
    {
        // this could pull from config, a database, it
        // could even be hardcoded on a per-project basis - YMMV!
    }

    /**
     * Given the normal id the application would use, prefix
     * it with the appropriate domain.
     */
    private function _getDomainId($id)
    {
        return $this->_getDomain() . '_' . $id;
    }

    /**
     * Save a cache record directly. This method is implemented by the cache
     * drivers and used in Doctrine_Cache_Driver::save().
     * Overridden such that a domain-specific key is used.
     *
     * @param string $id        cache id
     * @param string $data      data to cache
     * @param int $lifeTime     if != false, set a specific lifetime for this
     *                          cache record (null => infinite lifeTime)
     * @return boolean true if no problem
     */
    protected function _doSave($id, $data, $lifeTime = false)
    {
        return parent::_doSave($this->_getDomainId($id), $data, $lifeTime);
    }
}

如果您对编写Doctrine_Cache_Memcache感兴趣,例如假设您想扩展为_getDomain提供实际工作的任何内容,您可以实现Doctrine_Class_Interface 相反。

关于php - 使用 Memcache 和 Doctrine 1.2 进行多域缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8443970/

相关文章:

php - 如何创建日志来跟踪 Excel 刷新

php - 确保来自 Paypal 的 paypal 返回页面

php - 以下场景的流畅查询

mysql - 类似 IDE 的系统 - 用于存储的数据库或文件系统

php - 用于验证员工 ID 的正则表达式

MySQL 奇怪的分组结果集

symfony - 在 Symfony2 中使用 Controller 外部的存储库

Mysql 搜索高行数

php - 在sql中按数组中的值获取行

php - 如何strtotime直到另一个strtotime?