所以,我有一个问题,一个引擎上的五个域,引擎使用Doctrine 1.2 ORM,所有查询缓存与memcache(Doctrine_Cache_Memcache)。如何使前缀键为每个域和获取由domainprefix_key缓存?谢谢。
您可以创建一个派生的扩展或组成Doctrine_Cache_Memcache。在派生中,您只需在将执行传递给Doctrine_Cache_Memcache之前,通过添加键的域部分来修改id。
下面是一个考虑使用继承的例子,重写_doSave方法;其他公共成员可以用类似的方式重写。<?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。