所以,我有一个问题,一个引擎上有五个域,引擎使用 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/