Resolver

Last updated on
27 June 2024

Overview

Time's Up utilizes cache-tags resolvers to periodically clear caches.

A resolver essentially does two things:

  1. Defines a cache-tag name to be invalidated.
  2. Defines a periodicity to invalidate that cache tag.

You may want to extend Time's Up with your own resolvers to implement custom periodicity.

Creating resolvers that operate in very short intervals (e.g., every minute) can put heavy stress on the database.

Resolver Declaration

In order to decalre a new Resolver, you only needs 2 things

  • Declaring your resolver in your very own module.services.yml and tag your service with timesup.periodicity_resolver.
  • Create a service associated with the previous declaration on module.services.yml
    • Define a cache-tag name (will be prefixed with timesup:)
    • Define a periodicity logic to run the resolver

Example

To declare a new resolver that defines a cache-tag to clear content every day at midday, you can follow these steps:

1. Declaring your resolver in a services.yml file

my_module.services.yml

services:
  my_module.timesup.periodicity_resolver.daily_midday:
    class: Drupal\my_module\Timesup\Periodicity\DailyMiddayResolver
    parent: timesup.periodicity_resolver.base
    tags:
      - { name: timesup.periodicity_resolver, priority: 0 }

2. Create a new Resolver Class

my_module/src/Timesup/Periodicity/DailyMiddayResolver.php

<?php

namespace Drupal\my_module\Timesup\Periodicity;

/**
 * Will clear the cache-tags timesup_midday every day at midday.
 *
 * @internal
 */
final class DailyMiddayResolver extends PeriodicityBaseResolver {

  /**
   * The Cache tag name to invalidate.
   *
   * @string
   */
  const CACHE_TAG = 'midday';

  /**
   * {@inheritdoc}
   */
  public function shouldApply(): bool {
    // Code here your very own logic to run or not run the cache chear.
     
    // You can get the last run timestamp using.
    $last_run = $this->state->get($this->getLastRunKey());

    // From this last run, you can calculate the next desired occurence
    // and define the difference between now to run or not the invalidation.
  }
}

3. Use the defined cache-tag

You may now use the declared cache-tag timesup:midday anywhere you need it.

Help improve this page

Page status: No known problems

You can: