Commit 2c8224c2 authored by gbyte.co's avatar gbyte.co

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;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Cache\CacheableResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Symfony\Component\HttpFoundation\Request;
use Drupal\simple_sitemap\SimplesitemapManager;
......@@ -23,19 +21,12 @@ class SimplesitemapController extends ControllerBase {
*/
protected $generator;
/**
* @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
*/
protected $cacheKillSwitch;
/**
* SimplesitemapController constructor.
* @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->cacheKillSwitch = $cache_kill_switch;
}
/**
......@@ -43,8 +34,7 @@ class SimplesitemapController extends ControllerBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('simple_sitemap.generator'),
$container->get('page_cache_kill_switch')
$container->get('simple_sitemap.generator')
);
}
......@@ -73,20 +63,12 @@ class SimplesitemapController extends ControllerBase {
$output = $this->generator->getSitemap($variant, $request->query->getInt('page'));
if (!$output) {
$this->cacheKillSwitch->trigger();
throw new NotFoundHttpException();
}
$response = new CacheableResponse($output, Response::HTTP_OK, [
return new Response($output, Response::HTTP_OK, [
'content-type' => 'application/xml',
'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
}
public static function removeSitemapVariant($variant = NULL, $mode = 'all') {
$connection = \Drupal::database();
$delete_query = $connection->delete('simple_sitemap');
$delete_query = \Drupal::database()->delete('simple_sitemap');
switch($mode) {
case 'published':
$status = 1;
$delete_query->condition('status', 1);
break;
case 'unpublished':
$status = 0;
$delete_query->condition('status', 0);
break;
case 'all':
$status = NULL;
break;
default:
//todo: throw error
}
if (NULL !== $status) {
$delete_query->condition('status', $status);
}
if (NULL !== $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();
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