我试图用Memcache中的函数扩展我的PDO类。特别是,我创建了一个函数cache_execute
,它允许我首先检查结果是否存在于缓存中,然后返回缓存中的信息,或者如果信息尚不存在,则返回数据库中的信息在缓存中(以及将信息中的缓存,如果不存在的话)模拟PDOStatement ::执行
这里是我当前的代码示例:
namespace trf;
class StatementWithCaching extends \PDOStatement {
public $db;
public $cache;
protected function __construct(&$db) {
$this->db =& $db;
}
function cache_execute($array = NULL , $cache = FALSE , $cacheTime = 1800) {
if(is_a($cache , 'Memcache')) {
$query = $this->queryString;
foreach ($array as $key => $value) {
$query = str_replace(':' . $key , "'" . addslashes($value) . "'", $query);
}
try {
$memResults = $this->mconn->get(md5($this->queryString));
}
catch (\Exception $e) {
$memResults = FALSE;
}
} else {
}
}
}
我的问题是 - 我怎么把信息从缓存中检索并存储在可由PDOStatement::fetch()
,PDOStatement::fetchAll()
等检索的地方
我的目标是能够运行StatementWithCaching::execute()
或StatementWithCaching::cache_execute()
并仍以相同方式检索结果(使用StatementWithCaching::fetch()
或StatementWithCaching::fetchAll()
)。
你不能,而不是与pdostatement对象。他们不会知道任何有关您的缓存的信息。你的主要的pdo包装应该包装“准备”/“执行”并返回你自己的缓存感知对象。 – 2014-12-04 14:53:33
问题不在于缓存,它知道如何将信息存储在可由PDOStatement :: fetch()或PDOStatement :: fetchAll()检索的位置,除非我误解了你。 – 2014-12-04 14:55:39
,就像我说过的,不可能使用pdostatement对象。它不知道你的缓存。所以你的pdo包装必须扩展“准备”来返回一个“MyCacheAwarePDOStatement”对象,而该对象扩展了pdostatement。 – 2014-12-04 14:56:43