Skip to content

Commit cd7ac54

Browse files
committed
Issue #2910796 by bartlangelaan: Cache should be optional
1 parent fd1fc0c commit cd7ac54

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

config/install/hn.settings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cache: true

config/schema/hn.schema.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
hn.settings:
2+
type: config_object
3+
label: 'HN Settings'
4+
mapping:
5+
cache:
6+
type: boolean
7+
label: 'Caching'

hn.install

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* This file provides updates for the HN module.
6+
*/
7+
8+
/**
9+
* Uninstall hn_extended_view_serializer.
10+
*/
11+
function hn_update_8001() {
12+
\Drupal::service('module_installer')->uninstall(['hn_extended_view_serializer']);
13+
}
14+
15+
/**
16+
* Add config file with cache enabled.
17+
*/
18+
function hn_update_8002() {
19+
\Drupal::configFactory()->getEditable('hn.settings')->set('cache', TRUE)->save();
20+
}

hn.module

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/HnResponseService.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Drupal\hn\Event\HnEntityEvent;
99
use Drupal\hn\Event\HnHandledEntityEvent;
1010
use Drupal\hn\Event\HnResponseEvent;
11-
use Drupal\node\Entity\Node;
1211
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1312
use Symfony\Component\HttpFoundation\Request;
1413
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -35,7 +34,7 @@ class HnResponseService {
3534
*/
3635
protected $currentUser;
3736
/**
38-
* Drupal\webprofiler\Config\ConfigFactoryWrapper definition.
37+
* Drupal\Core\Config\ConfigFactory definition.
3938
*
4039
* @var \Drupal\Core\Config\ConfigFactory
4140
*/
@@ -93,6 +92,8 @@ private function alterResponse($eventName) {
9392
*/
9493
public function getResponseData() {
9594

95+
$config = $this->config->get('hn.settings');
96+
9697
// First, get the current request.
9798
$r = \Drupal::request();
9899

@@ -135,7 +136,7 @@ public function getResponseData() {
135136
}
136137

137138
// Check if this page is cached.
138-
if (!$this->debugging && $cache = $this->cache->get('hn.response_cache.' . $path)) {
139+
if ($config->get('cache') && !$this->debugging && $cache = $this->cache->get('hn.response_cache.' . $path)) {
139140
$this->responseData = $cache->data;
140141
}
141142

@@ -150,7 +151,9 @@ public function getResponseData() {
150151
}
151152
}
152153

153-
\Drupal::cache()->set('hn.response_cache.' . $path, $this->responseData, Cache::PERMANENT, $cache_tags);
154+
if ($config->get('cache')) {
155+
\Drupal::cache()->set('hn.response_cache.' . $path, $this->responseData, Cache::PERMANENT, $cache_tags);
156+
}
154157
}
155158

156159
$this->alterResponse(HnResponseEvent::PRE_SEND);

0 commit comments

Comments
 (0)