2014-06-22 22 views
11

Tôi đang sử dụng máy chủ Memcached (cùng với phần mở rộng memcache PHP) để lưu trữ siêu dữ liệu validator và trình điều khiển bộ nhớ cache của Doctrine metatada/result/query.Bộ nhớ cache của ứng dụng Symfony/console: clear --env = prod và cache (APC, Memcached, vv)?

Mọi thứ hoạt động như mong đợi và là nhanh so với bộ đệm hệ thống tệp.

Câu hỏi của tôi là, hiện các lệnh:

php app/console cache:clear --env=prod --no-debug 

tự động xóa tất cả các loại bộ nhớ cache (bao gồm memcache)?

Sau khi chạy lệnh này và kiểm tra số liệu thống kê máy chủ, các mục đếm luôn giống nhau cũng như bộ nhớ cache nghề nghiệp:

enter image description here

cấu hình của tôi, nơi% prod_cache% tham số thực sự là chuỗi memcache:

# Framework Configuration 
framework: 
    validation: 
     cache: %prod_cache% # Matches validator cache service 

# Doctrine Configuration 
doctrine: 
    orm: 
     metadata_cache_driver: 
      type: service 
      id: cache_%prod_cache% 
     result_cache_driver: 
      type: service 
      id: cache_%prod_cache% 
     query_cache_driver: 
      type: service 
      id: cache_%prod_cache% 

# DoctrineCacheBundle Configuration 
doctrine_cache: 
    providers: 
     memcache: 
      type: memcache 
      alias: cache_memcache 

# Services 
services: 
    validator.mapping.cache.memcache: # Validator cache service 
     class: Symfony\Component\Validator\Mapping\Cache\DoctrineCache 
     arguments: [@cache_memcache] 

Trả lời

5

Bạn phải xóa bộ nhớ cache bên ngoài chính mình, từ doc (SF 2.5):

Có thể có rất nhiều điều khác mà bạn cần phải làm, tùy thuộc vào thiết lập của bạn : [...] Xoá bộ nhớ cache APC

Xem: http://symfony.com/doc/current/cookbook/deployment/tools.html#e-other-things

+0

Symfony/Học thuyết thực sự có lệnh cho điều này, xem [câu trả lời của tôi ở đây] (http://stackoverflow.com/a/24652434/1237411) –

1

Nếu bạn kiểm tra vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php bạn sẽ thấy rằng Symfony xóa cache bằng dịch vụ hệ thống tập tin. Nó xóa bộ nhớ cache bằng cách loại bỏ thư mục cũ.

Dưới đây là chức năng chính xác (Symfony 2,4)

protected function execute(InputInterface $input, OutputInterface $output) 
{ 
    $realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir'); 
    $oldCacheDir = $realCacheDir.'_old'; 
    $filesystem = $this->getContainer()->get('filesystem'); 

    if (!is_writable($realCacheDir)) { 
     throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir)); 
    } 

    if ($filesystem->exists($oldCacheDir)) { 
     $filesystem->remove($oldCacheDir); 
    } 

    $kernel = $this->getContainer()->get('kernel'); 
    $output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true))); 
    $this->getContainer()->get('cache_clearer')->clear($realCacheDir); 

    if ($input->getOption('no-warmup')) { 
     $filesystem->rename($realCacheDir, $oldCacheDir); 
    } else { 
     // the warmup cache dir name must have the same length than the real one 
     // to avoid the many problems in serialized resources files 
     $warmupDir = substr($realCacheDir, 0, -1).'_'; 

     if ($filesystem->exists($warmupDir)) { 
      $filesystem->remove($warmupDir); 
     } 

     $this->warmup($warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers')); 

     $filesystem->rename($realCacheDir, $oldCacheDir); 
     if (defined('PHP_WINDOWS_VERSION_BUILD')) { 
      sleep(1); // workaround for Windows PHP rename bug 
     } 
     $filesystem->rename($warmupDir, $realCacheDir); 
    } 

    $filesystem->remove($oldCacheDir); 
} 
2

Lệnh này không xóa Memcache vì nó là một độc lập phân phối bộ nhớ cacher. Chỉ xóa bộ nhớ cache của Symfony. Để xóa memcache, bạn phải chạy thủ công:

echo 'flush_all' | nc localhost 11211 

Nhưng tôi nghĩ bạn đã biết điều đó rồi.

13

cache: rõ ràng

Lệnh cache:clear không xóa cache Doctrine. Nó chỉ xóa một số bộ đệm cụ thể của khung công tác Symfony 2, chủ yếu là các thư mục app/cache (hoặc var/cache).

thuyết lệnh

Để xóa cache Doctrine, sử dụng:

  • doctrine:cache:clear-metadata --env=prod
  • doctrine:cache:clear-query --env=prod
  • doctrine:cache:clear-result --env=prod

Bộ nhớ cache bổ sung

Nếu bạn sử dụng các cửa hàng bộ nhớ cache bổ sung, bạn sẽ phải tự xóa chúng. Bạn có thể xem các lệnh doctrine:cache:clear-* để lấy cảm hứng về cách tạo lệnh của riêng bạn.

Nhưng nhìn vào cấu hình của bạn, bạn sử dụng một kho lưu trữ bộ nhớ cache duy nhất cho 3 bộ đệm Doctrine và bộ đệm cache của Symfony Validator. Vì vậy, chỉ cần gọi một trong số doctrine:cache:clear-* sẽ xóa mọi thứ.

Tắt chủ đề

Khi sử dụng một hệ thống bộ nhớ cache như APC, OPcache, và có lẽ những người khác, bạn sẽ phải hiểu rằng khi bạn chạy một kịch bản PHP từ dòng lệnh, một không gian bộ nhớ khác nhau được sử dụng sau đó khi chạy một tập lệnh PHP từ một máy chủ web.

Nói cách khác: khi bạn xóa bộ đệm đó khỏi dòng lệnh, bộ nhớ cache được sử dụng bởi máy chủ web là không bị ảnh hưởng. Để xóa bộ nhớ cache máy chủ web, bạn sẽ cần phải chạy (các) tập lệnh xóa bộ nhớ cache từ chính máy chủ web.

Đây không phải là vấn đề cho memcached, bởi vì nó là một chương trình riêng biệt quản lý không gian bộ nhớ của riêng nó.

+0

đừng quên thêm --env = prod vào cuối các lệnh của học thuyết! – 10us

-1

thử điều này:

php app/console memcached:clear --clearAll default 
1

Nếu bạn sử dụng một bộ nhớ cache như APC sử dụng khác nhau không gian bộ nhớ cho bàn điều khiển và máy chủ web, bạn không có lý do nào khác để có và giao diện nơi bạn xóa bộ nhớ cache theo cách thủ công.

Đối với memcache, memcached, v.v. bạn có thể móc vào bộ nhớ cache: lệnh rõ ràng bằng cách tạo trình lắng nghe qua sự kiện kernel.cache_clearer chạy cả lệnh bộ nhớ cache của lệnh doctrine.

Bất kể phương pháp nào bạn sử dụng để xóa bộ nhớ cache, một số loại bộ nhớ cache như memcache, sẽ không giải phóng bộ nhớ được sử dụng. Thay vào đó, họ sẽ bỏ khóa vào lần sau khi lệnh nhận được cấp trên khóa đó hoặc khi bộ nhớ đầy và một số phím cần được loại bỏ.

class CacheClearDoctrineListener implements CacheClearerInterface 
{ 
/** 
* @var ContainerInterface 
*/ 
protected $container; 

protected $environment; 

public function setContainer(ContainerInterface $container = null) 
{ 
    $this->container = $container; 
    $this->environment = $container->get('kernel')->getEnvironment(); 
} 

public function clear($cacheDir) 
{ 
    $this->clearCacheMetadata(); 
    $this->clearCacheQuery(); 
} 

protected function clearCacheMetadata() 
{ 
    // clear cache metadata 
    $application = new Application($this->container->get('kernel')); 
    $application->setAutoExit(false); 

    $output = new ConsoleOutput(); 
    $arguments = array(
     "command" => "doctrine:cache:clear-metadata", 
     "--env" => $this->environment 
    ); 
    $input = new ArrayInput($arguments); 
    $returnCode = $application->run($input, $output); 

    if ($returnCode != 0) { 
     $output->writeln(json_decode(rtrim($output))); 
    } 
} 

protected function clearCacheQuery() 
{ 
    // clear cache query 
    $application = new Application($this->container->get('kernel')); 
    $application->setAutoExit(false); 

    $output = new ConsoleOutput(); 
    $arguments = array(
     "command" => "doctrine:cache:clear-query", 
     "--env" => $this->environment 
    ); 
    $input = new ArrayInput($arguments); 
    $returnCode = $application->run($input, $output); 

    if ($returnCode != 0) { 
     $output->writeln(json_decode(rtrim($output))); 
    } 
} 
} 

Khai báo một cái gì đó dịch vụ của bạn như thế này:

<service id="cache_clear_doctrine_clearer" class="%namespace_of_your_listener%"> 
    <call method="setContainer"> 
     <argument type="service" id="service_container"/> 
    </call> 
    <tag name="kernel.cache_clearer" priority="254" /> 
</service> 
Các vấn đề liên quan