8000 [WIP] [2.2] [HttpKernel] added commands to clear, cleanup and introspect the HttpCache by lsmith77 · Pull Request #6213 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WIP] [2.2] [HttpKernel] added commands to clear, cleanup and introspect the HttpCache #6213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;

abstract class AbstractHttpCacheCommand extends ContainerAwareCommand
{
/**
* @return array
*/
protected function getDefinitionArray()
{
return array(
new InputArgument('uri', InputArgument::OPTIONAL, 'A full uri, including the scheme, host, path and query string'),
new InputOption('kernel_class_name', '', InputOption::VALUE_OPTIONAL, 'Name of the HttpCache kernel', 'AppCache'),
);
}

/**
* @return string cache dir
* @throws \RuntimeException
*/
protected function getCacheDir()
{
$cacheDir = $this->getContainer()->getParameter('kernel.cache_dir').'/http_cache';
if (!is_readable($cacheDir)) {
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $cacheDir));
}

return $cacheDir;
}

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\HttpKernel\KernelInterface $kernel
* @return \Symfony\Component\HttpKernel\HttpCache\HttpCache
* @throws \RuntimeException
*/
protected function getCacheKernel(InputInterface $input, KernelInterface $kernel)
{
$kernelClassName = $input->getOption('kernel_class_name');
if (!class_exists($kernelClassName)) {
throw new \RuntimeException(sprintf('Kernel class "%s" not loadable', $kernelClassName));
}

$cacheKernel = new $kernelClassName($kernel);
if (! $cacheKernel instanceof HttpCache) {
throw new \RuntimeException(sprintf('Kernel ("%s") is not an instance of HttpCache', get_class($kernel)));
}

return $cacheKernel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* TODO: this command is a bit all over the place
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this command needs more work

* imho there should be a command for releasing locks and purging stale data
* and it should be able to do this for all cache entries or just a specific uri
* note sure if this should be two separate or just one command
* also this will require further changes to Store/StoreInterface
*/
class HttpCacheCleanupCommand extends AbstractHttpCacheCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('http:cache:cleanup')
->setDefinition($this->getDefinitionArray())
->setDescription('Cleans up locked files in the http cache')
->setHelp(<<<EOF
The <info>%command.name%</info> command cleans up all locked entries in the http cache
for a given environment and debug mode:

<info>php %command.full_name% --env=dev</info>
<info>php %command.full_name% --env=prod --no-debug</info>
EOF
)
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$kernel = $this->getContainer()->get('kernel');
$cacheKernel = $this->getCacheKernel($input, $kernel);

$uri = $input->getArgument('uri');
if (empty($uri)) {
$output->writeln(sprintf('Cleaning up the http cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
$cacheKernel->getStore()->cleanup();
return 0;
}

$output->writeln(sprintf('Cleaning up the http cache for the uri <info>%s</info> and the <info>%s</info> environment with debug <info>%s</info>', $uri, $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));

$store = $cacheKernel->getStore();
$request = Request::create($uri);
if ($store->isLocked($request)) {
$output->writeln(sprintf('Removed lock for the uri <info>%s</info>', $uri));
$store->unlock($request);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class HttpCacheClearCommand extends AbstractHttpCacheCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('http:cache:clear')
->setDefinition($this->getDefinitionArray())
->setDescription('Clears the http cache')
->setHelp(<<<EOF
The <info>%command.name%</info> command clears the application http cache for a given environment
and debug mode:

<info>php %command.full_name% --env=dev</info>
<info>php %command.full_name% --env=prod --no-debug</info>
EOF
)
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$kernel = $this->getContainer()->get('kernel');
$cacheKernel = $this->getCacheKernel($input, $kernel);

$uri = $input->getArgument('uri');
if (empty($uri)) {
$output->writeln(sprintf('Clearing the http cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
$filesystem = $this->getContainer()->get('filesystem');
$cacheDir = $this->getCacheDir();
$filesystem->remove($cacheDir);
return 0;
}

$output->writeln(sprintf('Clearing the http cache for the uri <info>%s</info> and the <info>%s</info> environment with debug <info>%s</info>', $uri, $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));

$store = $cacheKernel->getStore();
$store->purge($uri);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Request;

class HttpCacheInfoCommand extends AbstractHttpCacheCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('http:cache:info')
->setDefinition($this->getDefinitionArray())
->setDescription('Provides information about entries in the http cache')
->setHelp(<<<EOF
The <info>%command.name%</info> command provides information about entries in the http cache
for a given environment and debug mode:

<info>php %command.full_name% --env=dev</info>
<info>php %command.full_name% --env=prod --no-debug</info>
EOF
)
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$kernel = $this->getContainer()->get('kernel');
$cacheKernel = $this->getCacheKernel($input, $kernel);

$uri = $input->getArgument('uri');

$output->writeln(sprintf('Reading information from the http cache for the uri <info>%s</info> and the <info>%s</info> environment with debug <info>%s</info>', $uri, $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));

$store = $cacheKernel->getStore();
$request = Request::create($uri);
$txt = $store->isCached($request) ? ('cached'.($store->isLocked($request) ? ' and locked' : ' and unlocked')) : 'not cached';
$output->writeln(sprintf('Location (is %s): <info>%s</info>', $txt, $store->getLocation($request)));
}
}
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpKernel/HttpCache/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public function isLocked(Request $request)
return is_file($this->getPath($this->getCacheKey($request).'.lck'));
}

public function isCached(Request $request)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should maybe be moved to a different interface?

{
return is_file($this->getLocation($request));
}

/**
* Locates a cached Response for the Request provided.
*
Expand Down Expand Up @@ -361,6 +366,11 @@ private function save($key, $data)
@chmod($path, 0666 & ~umask());
}

public function getLocation(Request $request)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should maybe be moved to a different interface?

{
return $this->getPath($this->getCacheKey($request));
}

public function getPath($key)
{
return $this->root.DIRECTORY_SEPARATOR.substr($key, 0, 2).DIRECTORY_SEPARATOR.substr($key, 2, 2).DIRECTORY_SEPARATOR.substr($key, 4, 2).DIRECTORY_SEPARATOR.substr($key, 6);
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/HttpKernel/HttpCache/StoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ public function unlock(Request $request);
*/
public function isLocked(Request $request);

/**
* Returns whether or not a cache entry exists.
*
* @param Request $request A Request instance
*
* @return Boolean true if cache entry exists, false otherwise
*/
public function isCached(Request $request);

/**
* Returns the location where the cache entry is stored
*
* @param Request $request A Request instance
*
* @return string Location of the cache entry for the given Request
*/
public function getLocation(Request $request);

/**
* Purges data for the given URL.
*
Expand Down
0