私の教義表で私はこれを手に入れました
public function countHitsFor($object_id) {
return $this->createQuery('s')
->select('COUNT(*) as count')
->where('s.target_id = ?', $object_id);
->useResultCache(true, 3600, 'hits_for_'.$object_id)
->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
}
したがって、アクションからこれを使用すると、正常に機能します。最初のリクエストはSQLクエリを作成し、2番目はキャッシュからヒットをロードします
そして私が欲しいのは、symfonyタスクからキャッシュをウォームアップすることです
タスクを開始し、オブジェクトごとStatTable::getInstance()->countHitsFor($object_id)
にクエリ用のキャッシュデータを作成するために呼び出しますが、機能しません
タスクの最初のアクション要求の後、SQLクエリが作成されます
UPD
プロジェクト構成
public function configureDoctrine(Doctrine_Manager $manager)
{
$manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, new Doctrine_Cache_Apc());
}
仕事
<?php
class warm_up_stat_cacheTask extends sfProgressTask
{
protected function configure()
{
$this->addOptions(array(
new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'prod'),
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
// add your own options here
));
$this->namespace = 'my_tasks';
$this->name = 'warm_up_stat_cache';
$this->briefDescription = '';
$this->detailedDescription = '';
}
protected function execute($arguments = array(), $options = array())
{
// initialize the database connection
$databaseManager = new sfDatabaseManager($this->configuration);
$connection = $databaseManager->getDatabase($options['connection'])->getConnection();
$contextInstance = sfContext::createInstance($this->configuration);
$Objects = ObjectTable::getInstance()->findAll();
foreach ($Objects as $obj) {
StatTable::getInstance()->countHitsFor($obj->id);
}
$obj->free();
}
}
コマンド
php symfony my_tasks:warm_up_stat_cache
UPD2
私が理解しているように、問題はAPCにあります
public function countHitsFor($object_id) {
$ckey = 'hits_for_'.$object_id;
if (!apc_exists($ckey))
apc_store($ckey,$this->createQuery('s')
->select('COUNT(*) as count')
->where('s.target_id = ?', $object_id);
->useResultCache(true, 3600, 'hits_for_'.$object_id)
->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR));
return (int) apc_fetch($ckey);
}
これも機能しません。APC がさまざまな環境のキーのプレフィックスを作成する可能性がありますか?