ApcCache symfony2 create cache from cli

317 views Asked by At

I have console command in symfony. This command must recalculate some reports, and save this report to cache. (command must be run in cron every 30 minutes) Next, when user look at te report on the website, must no waiting for generate report.

It works, when i recalculate, and set cache from browser request (if cache not exist, recalculate and ganarete cache). Recalculate and set cache from console command not work. I dont get any error, but cache wasn't save.

This is my test command in symfony:

<?php

namespace Test\ReportsBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


class RefreshSalesTxtReportCacheCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
                ->setName('crm:report:refreshSalesTxtCache')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {     
        $this->cache = new \Doctrine\Common\Cache\ApcCache();
        $this->cache->delete('test');
        $this->cache->save('test', 'content', 9999999999);
        echo phpinfo();
        var_dump($this->cache->fetch('trol'));

        die();
    }

}

When i run this command:

php app/console crm:report:refreshSalesTxtCache -e prod |grep apc

I get output:

/etc/php5/cli/conf.d/20-apcu.ini,
apc
apcu
apc.coredump_unmap => Off => Off
apc.enable_cli => On => On
apc.enabled => On => On
apc.entries_hint => 4096 => 4096
apc.gc_ttl => 3600 => 3600
apc.mmap_file_mask => no value => no value
apc.preload_path => no value => no value
apc.rfc1867 => Off => Off
apc.rfc1867_freq => 0 => 0
apc.rfc1867_name => APC_UPLOAD_PROGRESS => APC_UPLOAD_PROGRESS
apc.rfc1867_prefix => upload_ => upload_
apc.rfc1867_ttl => 3600 => 3600
apc.serializer => php => php
apc.shm_segments => 1 => 1
apc.shm_size => 32M => 32M
apc.slam_defense => On => On
apc.smart => 0 => 0
apc.ttl => 0 => 0
apc.use_request_time => On => On
apc.writable => /tmp => /tmp

In

1

There are 1 answers

0
Sachin I On

You have other option to use memcache container like below:

Add into cache when you run your script first time and Check into cache whether report id is set into cache or not:

 $key = 'reportCache_'.$reportId;
 $content = $this->getContainer()->get('service.memcache')->get($key);
 if ($content === false) {
    $this->getContainer()->get('service.memcache')->setAlways($key, $reportContaint, 3600*12);
 }