ApcCache symfony2从cli

时间:2015-06-21 20:56:44

标签: php symfony caching command-line-interface apc

我在symfony中有控制台命令。此命令必须重新计算某些报告,并将此报告保存到缓存中。 (命令必须每30分钟在cron中运行) 接下来,当用户在网站上查看te报告时,一定不能等待生成报告。

当我重新计算时,它可以工作,并从浏览器请求设置缓存(如果缓存不存在,重新计算和ganarete缓存)。 从控制台命令重新计算和设置缓存不起作用。我没有收到任何错误,但缓存没有保存。

这是我在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();
    }

}

当我运行此命令时:

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

我得到输出:

/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

1 个答案:

答案 0 :(得分:0)

您可以使用其他选项来使用如下所示的memcache容器:

第一次运行脚本时添加到缓存中 检查缓存是否将报告ID设置为缓存:

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