Skip to content

Commit

Permalink
Merge pull request #30 from jefflogan303/master
Browse files Browse the repository at this point in the history
Merge code for Drupal 10, thanks @jefflogan303 
Feel free to join [golitespeed community on Slack](https://litespeedtech.com/slack), there's a drupal-cache channel, and everyone can discuss more there in the future.
  • Loading branch information
Code-Egg authored Mar 20, 2023
2 parents 57b5d9e + a2f4519 commit 59921e2
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 91 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
LiteSpeed Cache for Drupal 8+
LiteSpeed Cache for Drupal 9+
============================

Drupal 8/9 is significantly changed from Drupal 7. They have converted from procedural programming to object-oriented programming. Drupal 8 has a built-in page cache (for static content) and a dynamic page cache for logged in users. The latter basically works as a reverse proxy written in PHP. The built-in proxy is good if you have no other options, however, with the LiteSpeed Cache plugin you will see a great improvement in performance for your Drupal 8 site.
Drupal 9+ is significantly changed from Drupal 7. They have converted from procedural programming to object-oriented programming. Drupal 9+ has a built-in page cache (for static content) and a dynamic page cache for logged in users. The latter basically works as a reverse proxy written in PHP. The built-in proxy is good if you have no other options, however, with the LiteSpeed Cache plugin you will see a great improvement in performance for your Drupal 9+ site.

See https://www.litespeedtech.com/products/cache-plugins for more information.



Prerequisites
-------------
This version of LiteSpeed Cache requires Drupal 8.xx or later and LiteSpeed Web Server (LSWS) 5.2.3 or later.
This version of LiteSpeed Cache requires Drupal 9 or later and LiteSpeed Web Server (LSWS) 5.2.3 or later.



Expand All @@ -36,9 +36,11 @@ If you use Composer to manage dependencies, require the module in your project `
Enable LiteSpeedCache using rewrite rules
-------------

<IfModule LiteSpeed>
CacheLookup on
</IfModule>
```
<IfModule LiteSpeed>
CacheLookup on
</IfModule>
```

Install Plugin
-------------
Expand Down
9 changes: 1 addition & 8 deletions lite_speed_cache.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,4 @@ type: module
description: 'Caches pages for any user, handling dynamic content correctly.'
package: Core
configure: lite_speed_cache.form
core_version_requirement: ^8 || ^9
# version: VERSION

# Information added by Drupal.org packaging script on 2017-12-06
version: '8.4.3'
core: '8.x'
project: 'drupal'
datestamp: 1512600826
core_version_requirement: ^9 || ^10 || ^11
2 changes: 0 additions & 2 deletions lite_speed_cache.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* Installation functions for LiteSpeed Cache module.
*/

use Drupal\user\RoleInterface;

/**
* Implements hook_install().
*/
Expand Down
10 changes: 0 additions & 10 deletions lite_speed_cache.module
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,3 @@ function lite_speed_cache_help($route_name, RouteMatchInterface $route_match) {
return $output;
}
}


/**
* Implements hook_user_login($account).
*/


function lite_speed_cache_user_login($account) {
$reqTemp = \Drupal::request();
}
1 change: 0 additions & 1 deletion src/Cache/LSCacheTagsInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Drupal\lite_speed_cache\Cache;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Symfony\Component\HttpFoundation\RequestStack;


class LSCacheTagsInvalidator implements CacheTagsInvalidatorInterface {
Expand Down
31 changes: 11 additions & 20 deletions src/EventSubscriber/LiteSpeedCacheSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
namespace Drupal\lite_speed_cache\EventSubscriber;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableResponseInterface;
use Drupal\Core\PageCache\RequestPolicyInterface;
use Drupal\Core\PageCache\ResponsePolicyInterface;
use Drupal\Core\Render\RenderCacheInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use \Drupal\lite_speed_cache\Cache\LSCacheTagsInvalidator;
use \Drupal\lite_speed_cache\Form\LSCacheForm;
Expand Down Expand Up @@ -122,10 +121,10 @@ public function __construct(RequestPolicyInterface $request_policy, ResponsePoli
/**
* Sets a response in case of a Dynamic Page Cache hit.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
* The event to process.
*/
public function onRouteMatch(GetResponseEvent $event) {
public function onRouteMatch(RequestEvent $event) {

// Don't cache the response if the Dynamic Page Cache request policies are
// not met. Store the result in a static keyed by current request, so that
Expand Down Expand Up @@ -160,10 +159,10 @@ private function filterTags($tags){
/**
* Stores a response in case of a Dynamic Page Cache miss, if cacheable.
*
* @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
* The event to process.
*/
public function onResponse(FilterResponseEvent $event){
public function onResponse(ResponseEvent $event){
$response = $event->getResponse();
$request = $event->getRequest();

Expand Down Expand Up @@ -194,7 +193,7 @@ public function onResponse(FilterResponseEvent $event){
if($lsCacheDebug=='0' or $lsCacheDebug == 'On') {
$response->headers->set(self::STATUSHEADER, 'LS Cache Purged!');
}
$response->headers->set(LiteSpeedCacheSubscriber::PURGEHEADER, $commonTag);
$response->headers->set(self::PURGEHEADER, $commonTag);
} else {
if($lsCacheDebug=='0' or $lsCacheDebug == 'On') {
$response->headers->set(self::STATUSHEADER, 'No Purge!');
Expand All @@ -208,7 +207,7 @@ public function onResponse(FilterResponseEvent $event){
$response->headers->set(self::STATUSHEADER, 'LS Cache Purged!');
}
$tags = implode(",", LSCacheTagsInvalidator::$tags);
$response->headers->set(LiteSpeedCacheSubscriber::PURGEHEADER, $tags);
$response->headers->set(self::PURGEHEADER, $tags);
} else {
if($lsCacheDebug=='0' or $lsCacheDebug == 'On') {
$response->headers->set(self::STATUSHEADER, 'No Purge!');
Expand All @@ -221,7 +220,7 @@ public function onResponse(FilterResponseEvent $event){
if($lsCacheDebug=='0' or $lsCacheDebug == 'On') {
$response->headers->set(self::STATUSHEADER, 'LS Cache Purged!');
}
$response->headers->set(LiteSpeedCacheSubscriber::PURGEHEADER, "*");
$response->headers->set(self::PURGEHEADER, "*");
} else {
if($lsCacheDebug=='0' or $lsCacheDebug == 'On') {
$response->headers->set(self::STATUSHEADER, 'No Purge');
Expand All @@ -234,13 +233,13 @@ public function onResponse(FilterResponseEvent $event){
if($lsCacheDebug=='0' or $lsCacheDebug == 'On') {
$response->headers->set(self::STATUSHEADER, 'LS Cache Purged!');
}
$response->headers->set(LiteSpeedCacheSubscriber::PURGEHEADER, $commonTag);
$response->headers->set(self::PURGEHEADER, $commonTag);
} else {
if($lsCacheDebug=='0' or $lsCacheDebug == 'On') {
$response->headers->set(self::STATUSHEADER, 'No Purge');
}
}

// wade:start
// Check if it needs to crawler the sitemap
if (LSCacheForm::$crawlerTheSite) {
Expand All @@ -250,7 +249,6 @@ public function onResponse(FilterResponseEvent $event){
}
}
// wade:end


if ($request->cookies->has(session_name())) {

Expand All @@ -270,18 +268,15 @@ public function onResponse(FilterResponseEvent $event){
return;
}


$request = $event->getRequest();
if (!isset($this->requestPolicyResults[$request])) {
return;
}


if ($this->requestPolicyResults[$request] === RequestPolicyInterface::DENY || $this->responsePolicy->check($response, $request) === ResponsePolicyInterface::DENY) {
return;
}


if($cacheStatus=='0' or $cacheStatus == 'On') {
$response->headers->set('X-LiteSpeed-Cache-Control', 'private, max-age='.$maxAgePrivate);
$tags = $response->getCacheableMetadata()->getCacheTags();
Expand All @@ -291,7 +286,6 @@ public function onResponse(FilterResponseEvent $event){
$response->headers->set('X-LiteSpeed-Tag', $tags);
}


}else {
$cookies = $request->cookies;
if($cookies->has('_lscache_vary')) {
Expand All @@ -304,7 +298,6 @@ public function onResponse(FilterResponseEvent $event){

}


/**
* Whether the given response should be cached by LiteSpeed Page Cache.
*
Expand Down Expand Up @@ -339,8 +332,6 @@ protected function shouldCacheResponse(CacheableResponseInterface $response) {
return TRUE;
}


/**
/**
* {@inheritdoc}
*/
Expand Down
34 changes: 2 additions & 32 deletions src/Form/LSCacheForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ class LSCacheForm extends ConfigFormBase
/**
* Purge all status variable
*/

public static $purgeALL;

/**
* Purge this site status variable
*/

public static $purgeThisSite;

/**
* crawlear this site variable
*/

public static $crawlerTheSite;

/**
Expand All @@ -49,7 +46,6 @@ protected function getEditableConfigNames() {
];
}


/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -105,22 +101,8 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#description' => $this->t('Weather to send or not the debug headers!'),
);


$options = ['On','Off'];

/*
$form['cache_settings']['esi_on'] = array(
'#type' => 'select',
'#title' => $this->t('ESI'),
'#options' => $options,
'#default_value' => $config->get('lite_speed_cache.esi_on'),
'#description' => $this->t('Turn ESI On or Off for Hole Punching! Keep it disabled if you are on OpenLiteSpeed.'),
);
*/


// max_age field.
$form['cache_settings']['max_age'] = array(
'#type' => 'textfield',
Expand All @@ -129,18 +111,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#description' => $this->t('Amount of time for which page should be cached by LiteSpeed Webserver public cache (Seconds).'),
);

/*
// max_age field.
$form['cache_settings']['max_age_private'] = array(
'#type' => 'textfield',
'#title' => $this->t('Max age private'),
'#default_value' => $config->get('lite_speed_cache.max_age_private'),
'#description' => $this->t('Amount of time for which page should be cached by LiteSpeed Webserver private cache.'),
);
*/

return $form;
}

Expand All @@ -165,7 +135,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$config->set('lite_speed_cache.cache_status', $form_state->getValue('cache_status'));
$config->set('lite_speed_cache.debug', $form_state->getValue('debug'));
$config->save();

// Prevent gzip cause broken website layout
$config = $this->config('system.performance');
$config->set('css.preprocess', '0');
Expand Down
21 changes: 9 additions & 12 deletions src/StackMiddleware/LiteSpeedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

namespace Drupal\lite_speed_cache\StackMiddleware;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableResponseInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\PageCache\RequestPolicyInterface;
use Drupal\Core\PageCache\ResponsePolicyInterface;
use Drupal\Core\Site\Settings;
use Drupal\user\Authentication\Provider\Cookie;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -102,14 +99,14 @@ private function filterTags($tags){
* {@inheritdoc}
*/

public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE):Response {



if( (isset($_SERVER['X-LSCACHE']) && $_SERVER['X-LSCACHE']) || (isset($_SERVER['HTTP_X_LSCACHE']) && $_SERVER['HTTP_X_LSCACHE']) ){

// Only allow page caching on master request.
if ($type === static::MASTER_REQUEST && $this->requestPolicy->check($request) === RequestPolicyInterface::ALLOW) {
if ($type === static::MAIN_REQUEST && $this->requestPolicy->check($request) === RequestPolicyInterface::ALLOW) {
$response = $this->lookup($request, $type, $catch);
}
else {
Expand All @@ -132,15 +129,15 @@ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch =
* @param \Symfony\Component\HttpFoundation\Request $request
* A request object.
* @param int $type
* The type of the request (one of HttpKernelInterface::MASTER_REQUEST or
* The type of the request (one of HttpKernelInterface::MAIN_REQUEST or
* HttpKernelInterface::SUB_REQUEST)
* @param bool $catch
* Whether to catch exceptions or not
*
* @returns \Symfony\Component\HttpFoundation\Response $response
* A response object.
*/
protected function pass(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
protected function pass(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE) {
return $this->httpKernel->handle($request, $type, $catch);
}

Expand All @@ -150,15 +147,15 @@ protected function pass(Request $request, $type = self::MASTER_REQUEST, $catch =
* @param \Symfony\Component\HttpFoundation\Request $request
* A request object.
* @param int $type
* The type of the request (one of HttpKernelInterface::MASTER_REQUEST or
* The type of the request (one of HttpKernelInterface::MAIN_REQUEST or
* HttpKernelInterface::SUB_REQUEST)
* @param bool $catch
* Whether to catch exceptions or not
*
* @returns \Symfony\Component\HttpFoundation\Response $response
* A response object.
*/
protected function lookup(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
protected function lookup(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE) {


// fetch request from backend and set cache headers
Expand Down Expand Up @@ -218,15 +215,15 @@ protected function lookup(Request $request, $type = self::MASTER_REQUEST, $catch
* @param \Symfony\Component\HttpFoundation\Request $request
* A request object.
* @param int $type
* The type of the request (one of HttpKernelInterface::MASTER_REQUEST or
* The type of the request (one of HttpKernelInterface::MAIN_REQUEST or
* HttpKernelInterface::SUB_REQUEST)
* @param bool $catch
* Whether to catch exceptions or not
*
* @returns \Symfony\Component\HttpFoundation\Response $response
* A response object.
*/
protected function fetch(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
protected function fetch(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE) {
/** @var \Symfony\Component\HttpFoundation\Response $response */
$response = $this->httpKernel->handle($request, $type, $catch);

Expand Down Expand Up @@ -296,4 +293,4 @@ protected function checkCacheAbility(Request $request, Response $response) {
}


}
}

0 comments on commit 59921e2

Please sign in to comment.