Commit 2c8224c2 authored by Pawel G's avatar Pawel G

Issue #3001470 by gbyte.co: Remove caching as now all sitemap content is stored in database

parent 54765021
...@@ -5,10 +5,8 @@ namespace Drupal\simple_sitemap\Controller; ...@@ -5,10 +5,8 @@ namespace Drupal\simple_sitemap\Controller;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Cache\CacheableResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\simple_sitemap\Simplesitemap; use Drupal\simple_sitemap\Simplesitemap;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Drupal\simple_sitemap\SimplesitemapManager; use Drupal\simple_sitemap\SimplesitemapManager;
...@@ -23,19 +21,12 @@ class SimplesitemapController extends ControllerBase { ...@@ -23,19 +21,12 @@ class SimplesitemapController extends ControllerBase {
*/ */
protected $generator; protected $generator;
/**
* @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
*/
protected $cacheKillSwitch;
/** /**
* SimplesitemapController constructor. * SimplesitemapController constructor.
* @param \Drupal\simple_sitemap\Simplesitemap $generator * @param \Drupal\simple_sitemap\Simplesitemap $generator
* @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $cache_kill_switch
*/ */
public function __construct(Simplesitemap $generator, KillSwitch $cache_kill_switch) { public function __construct(Simplesitemap $generator) {
$this->generator = $generator; $this->generator = $generator;
$this->cacheKillSwitch = $cache_kill_switch;
} }
/** /**
...@@ -43,8 +34,7 @@ class SimplesitemapController extends ControllerBase { ...@@ -43,8 +34,7 @@ class SimplesitemapController extends ControllerBase {
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('simple_sitemap.generator'), $container->get('simple_sitemap.generator')
$container->get('page_cache_kill_switch')
); );
} }
...@@ -73,20 +63,12 @@ class SimplesitemapController extends ControllerBase { ...@@ -73,20 +63,12 @@ class SimplesitemapController extends ControllerBase {
$output = $this->generator->getSitemap($variant, $request->query->getInt('page')); $output = $this->generator->getSitemap($variant, $request->query->getInt('page'));
if (!$output) { if (!$output) {
$this->cacheKillSwitch->trigger();
throw new NotFoundHttpException(); throw new NotFoundHttpException();
} }
$response = new CacheableResponse($output, Response::HTTP_OK, [ return new Response($output, Response::HTTP_OK, [
'content-type' => 'application/xml', 'content-type' => 'application/xml',
'X-Robots-Tag' => 'noindex', // Tell search engines not to index the sitemap itself. 'X-Robots-Tag' => 'noindex', // Tell search engines not to index the sitemap itself.
]); ]);
// Cache output.
$response->getCacheableMetadata()
->addCacheTags(["simple_sitemap:$variant"])
->addCacheContexts(['url.query_args']);
return $response;
} }
} }
...@@ -191,57 +191,29 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S ...@@ -191,57 +191,29 @@ abstract class SitemapGeneratorBase extends SimplesitemapPluginBase implements S
} }
public static function removeSitemapVariant($variant = NULL, $mode = 'all') { public static function removeSitemapVariant($variant = NULL, $mode = 'all') {
$connection = \Drupal::database(); $delete_query = \Drupal::database()->delete('simple_sitemap');
$delete_query = $connection->delete('simple_sitemap');
switch($mode) { switch($mode) {
case 'published': case 'published':
$status = 1; $delete_query->condition('status', 1);
break; break;
case 'unpublished': case 'unpublished':
$status = 0; $delete_query->condition('status', 0);
break; break;
case 'all': case 'all':
$status = NULL;
break; break;
default: default:
//todo: throw error //todo: throw error
} }
if (NULL !== $status) {
$delete_query->condition('status', $status);
}
if (NULL !== $variant) { if (NULL !== $variant) {
$delete_query->condition('type', $variant); $delete_query->condition('type', $variant);
} }
elseif ($status !== 0) {
$variant = $connection->query('SELECT DISTINCT type from {simple_sitemap} WHERE status = :status', [':status' => 1])->fetchCol();
}
$delete_query->execute(); $delete_query->execute();
if (NULL !== $variant) {
self::invalidateCache($variant);
}
}
/**
* @param string|array $variants
*/
protected static function invalidateCache($variants) {
$variants = is_array($variants) ? $variants : [$variants];
$tags = array_map(function($variant) {
return 'simple_sitemap:' . $variant;
}, $variants);
Cache::invalidateTags($tags);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment