0

在我的教义表中,我得到了这个

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 查询,第二个从缓存加载命中

我想要的是从 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 为不同环境的键添加前缀?

4

1 回答 1

1

问题出在 Apc 中,它为 Apache (mod_php) 和 Cli 使用单独的内存空间。

我在 MyISAM 表上做了简单的键值存储。

谢谢。

于 2012-09-17T21:29:18.883 回答