Search code examples
cachingsymfony-1.4apcdoctrine-1.2

Doctrine_Cache_Apc not works in symfony task


in my doctrine table i got this

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);
}

so if i use this from action its works fine, first request make sql query, second load Hits from cache

and what i want is to warm up cache from symfony task

i start task and for each object i call StatTable::getInstance()->countHitsFor($object_id) to create cache data for query, but its not works

after task first request to action makes sql query

UPD

ProjectConfiguration

public function configureDoctrine(Doctrine_Manager $manager)
{
    $manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, new Doctrine_Cache_Apc());
}

task

<?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();

    }
}

comand

php symfony my_tasks:warm_up_stat_cache

UPD2

as i understand the problem is in 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);
}

this not works too, may be APC makes prefixes for keys for different environments?


Solution

  • The problem is in Apc, its use separate memory spaces for Apache (mod_php) and for Cli.

    I made simple key-value storage on MyISAM table.

    Thanks.