Drupal cache clear also clearing Redis

Created on 2 November 2023, about 1 year ago
Updated 21 August 2024, 3 months ago

Problem/Motivation

We want to empty the Redis cache at the same time as we do a drupal Cache Clear via drush.

There are 3 issues with activity on this issue in the last year.
We are using the MR from one of these closed issues.
I opened this new issue to see if we can restart the discussion, or perhaps reopen the issue with the most comments on this.

Proposed resolution

Use the code from the MR in this issue: https://www.drupal.org/project/redis/issues/2765895 πŸ› Currently Drush Cr or Cache Clear UI does not flush Redis cache Fixed

✨ Feature request
Status

RTBC

Version

1.0

Component

Code

Created by

πŸ‡³πŸ‡±Netherlands Johan den Hollander

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @Johan den Hollander
  • First commit to issue fork.
  • πŸ‡«πŸ‡·France pbonnefoi

    @johan-den-hollander I opened an MR with the patch from #2765895 (with little improvements) I have other ideas, but need to find time to implement it.

    I made this feature as an option with the following settings :

    You can set flush_redis_on_drupal_flush_cache to TRUE to flush Redis cache when clearing all Drupal cache.

    $settings['flush_redis_on_drupal_flush_cache'] = TRUE;

    You can set flushdb and flushdb_async to TRUE to use the Redis native flushDB when clearing Drupal cache.

    $settings['redis_flushdb'] = TRUE;
    $settings['redis_flushdb_async'] = TRUE;

    You need to set a redis base properly to use this feature. By default, Redis gets cleared getting all keys using cache_prefix. This is not recommended on production as it could be an expensive task to clear all the keys.

  • πŸ‡³πŸ‡±Netherlands Johan den Hollander

    Thanks. Will give this a try.
    We use the clearing of the Redis on drush cr on two identical sites.
    However one of the sites is giving zend_memory_heap error when we do so.
    When we first do a redis-flush and then a drush cr without the patch from this issue we have no errors.

    The other site has no problem clearing Redis via a drush cr.

    I'm not sure if this error is only because of using this patch or that maybe we have some other issues like hosting configuration.
    Althoug both of these sites are configured in the same way...

  • First commit to issue fork.
  • πŸ‡©πŸ‡ͺGermany Harlor Berlin

    The redis client does not necessarily have databases. For instance the redis cluster cleint ✨ Implement initial RedisCluster client integration Needs review does not have databases.

    How ever with redis_flushdb=FALSE one actually does not need the DB index anyway - So I would suggest to move the redis index/DB safety checks and the ->select into the flushdb block.

  • πŸ‡«πŸ‡·France saadaouiyos

    saadaouiyos β†’ changed the visibility of the branch 3398797-drupal-cache-clear to hidden.

  • πŸ‡ΈπŸ‡°Slovakia hideaway

    There is a problem with the patch when prefix is an array and results into an error when logging messages. Correct version in the last else statement should be '@prefix' => $p:

    else {
      foreach ($prefix as $p) {
        $keys = $redis->keys('*' . $p . '*');
        $redis->del($keys);
        Drupal::logger('redis')->info(t('Redis Keys deleted for prefix @prefix', ['@prefix' => $p]));
        Drupal::messenger()->addMessage(t('Redis Keys deleted for prefix @prefix', ['@prefix' => $p]));
      }
    }
    

    As we don't use MR diff or patch files in our builds as they are dangerous, attaching a patch file to have working static patch.

  • πŸ‡ΈπŸ‡°Slovakia hideaway

    Forgotten patch file :)

  • Status changed to RTBC 4 months ago
  • πŸ‡©πŸ‡ͺGermany AntonΓ­n SlejΕ‘ka Hannover

    We use the patch on multiple sites (in production). It works as expected.

  • πŸ‡³πŸ‡±Netherlands renΓ© bakx Netherlands

    This is a much needed feature and the patched got the job done!

    It bit us in the back on a production env. Wondering why we where still seeing stale cache for objects set to never expire and no cache-tags to help them being evicted in other way.

    And yes you are right, it's wrong and somewhat foolish to rely on cache clear to solve this problem, yet is worked perfectly till we moved that project to use redis for cache.

    So yeah, please merge this and cut a new release :D

  • πŸ‡¬πŸ‡§United Kingdom adam clarey

    As mentioned above, this patch does not work with RedisCluser

    PHP Fatal error: Uncaught Error: Call to undefined method RedisCluster::select() in /var/www/chat-dev/web/modules/contrib/redis/redis.module:61
    Stack trace:
    #0 [internal function]: redis_cache_flush()

Production build 0.71.5 2024