wieni/wmpage_cache

Caches pages for anonymous users, with more customisability than the default page cache module

Installs: 1 807

Dependents: 2

Suggesters: 0

Security: 0

Stars: 0

Watchers: 4

Forks: 1

Open Issues: 4

Type:drupal-module

1.0.0 2024-01-07 10:16 UTC

README

Latest Stable Version Total Downloads License

Caches pages for anonymous users, with more customisability than the default page cache module.

Why?

  • Provides more configurable options than the default page cache module
  • Allows you to choose between multiple cache storages (database, Redis, Flysystem) and cache invalidators/purgers (default, Cloudfront)

Installation

This package requires PHP 7.1 and Drupal 8 or higher. It can be installed using Composer:

 composer require wieni/wmpage_cache

To prevent unnecessary early rendering issues when creating custom controllers, a patch from the following issue should be included:

#2638686: Exception in EarlyRenderingControllerWrapperSubscriber is a DX nightmare, remove it

How does it work?

Configuring the module

Settings can be changed through container parameters. Check wmpage_cache.services.yml for a list of settings, what they do and their default values.

About cacheability metadata

Just like the Internal Page Cache & Dynamic Page Cache core modules, this module uses cacheability metadata (cache tags, cache contexts and max-age) to determine the cacheability of a request and to make sure cached pages are invalidated when necessary.

Adding cacheable metadata from a controller

There are a bunch of different things you can return in a controller and cacheable metadata can be included in pretty much every one of them.

Render arrays

Cacheability metadata can be included in a render array under the #cache key. For more information, please refer to the official documentation.

Response objects

Cacheable Response objects have to implement CacheableResponseInterface and can use the CacheableResponseTrait. For more information, please refer to the official documentation.

wmcontroller ViewBuilder

When rendering a Twig template, eg. by using the wmcontroller ViewBuilder, all cacheable metadata of parameters that are passed to the template are automatically included.

Adding cacheable metadata from a Twig extension

If a Twig extension is returning information that will be used in a Twig template, without going through the Drupal render system, any cacheability metadata will be lost. That's why it's better to dispatch cacheability metadata in the logic of the Twig extension. You can do this by attaching the metadata to an empty render array and rendering it:

$build = [];
(new CacheableMetadata())
    ->addCacheableDependency($entity)
    ->applyTo($build);

$this->renderer->render($build);

In case this Twig extension is called often, this can impact performance. Another option is to collect all metadata until the end of page rendering, and attach it once in a hook_page_attachments implementation.

Changelog

All notable changes to this project will be documented in the CHANGELOG file.

Security

If you discover any security-related issues, please email security@wieni.be instead of using the issue tracker.

License

Distributed under the MIT License. See the LICENSE file for more information.