After enabling the cache_lifetime variable on a site, it looks like all cache entries are getting a TTL set to that value (including permanent entries), so if I set it to 1 minute, the whole cache is cleared every minute (automatically deleted by Redis). This doesn't seem to be consistent with the wording of the setting or the behaviour of the db cache.

The 2.x version seems to have similar behaviour but only for temporary items, or permanent items with a specified expiry.

From what I can tell on a full cache bin flush, the db cache will wait for cache_lifetime to pass before deleting all temporary cache entries in that bin (in DrupalDatabaseCache->garbageCollection()).

Comments

pounard’s picture

Status: Active » Closed (won't fix)

The database backend works very differently from Redis' one, and reproducing its behavior gives us no benefits, this is a won't fix, you should not use this setting when using the Redis module.

If I remember correctly cache_lifetime will force invalid items to be kept during this amount of time, and I read correctly the database backend code, it will do it for all cache bins. Considering how hard it was to have a stable version of the Redis 3.x version and the fact that the Redis module relies on the Redis EXPIRE feature, it's not possible to reproduce this behavior without complexifying a lot the code.

pounard’s picture

Status: Closed (won't fix) » Postponed (maintainer needs more info)

Not switching this to closed status else I'll miss your replies. I will switch this to closed later.

pounard’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

Long time no answer, closing this.

das-peter’s picture

Version: 7.x-3.8 » 7.x-3.x-dev
Status: Closed (won't fix) » Needs review
StatusFileSize
new1.1 KB

Wow, holy cow!!!
I just stumbled over this. I was wondering why I saw some strange inconsistencies in parts of our caching and when debugging I came across Redis_Cache::refreshMaxTtl().
This looks freaking evil if you think how harmless and inviting the cache_lifetime setting looks.
Let's add a nice orange warning to the configuration page if this setting is enabled and extend the settings description at least.

@pounard I trust your statement in #1, nevertheless it doesn't feel right that something that says "minimum lifetime" is basically converted to maximum lifetime. While I might could somehow understand that this applies to temporary items it seems quite odd for permanent or items with a specific time-out. I simply can't think of any case in which the TTL could / should be smaller than an explicitly set time-out :|

pounard’s picture

You are absolutely right.

pounard’s picture

Status: Needs review » Closed (won't fix)

Hello again!

I am sorry, I didn't answered for a long time. For what it worth, changing behaviours is now out of scope for the 7.x-3.x branch. Nevertheless, if that matters, I fixed it into a new PHP library I am working on:

This library aims to be framework agnostic, provides cache backends for Doctrine, Drupal 7, Drupal 8 and Symfony, and many more components in the future. Since it shares code among all implementations, it also tests that the single piece of code actually does respect the specification of all the interfaces it implements.

It is more thoroughly unit tested, and much more feature-complete: https://github.com/makinacorpus/redis-bundle.

I am closing this issue, the future 7.x-4.x branch will mostly be glue code to make it work gracefully with this shared library.

nicksanta’s picture

I encountered a problem with this behavior. In this paricular project (Drupal 7) we only used Redis for the cache_form bin. This meant that

a) cache_lifetime had to be enabled for the default cache bins (set to 15 minutes in our case), and
b) our cache_form entries were being garbage cleaned after only 15 minutes - despite Drupal explicitly requesting a 6 hour expiration on cache_form entries.

This result of this was our users received 500 errors and lost their state in multi-step forms if they took more than 15 minutes to complete the form.

We solved this by creating a new cache backend class. If anyone else out there is running into this problem using 7.x-3.x you can solve with this:

settings.php

<?php
...
// The order of these two lines is important.
$conf['cache_backends'][] = 'sites/all/modules/contrib/redis/redis.autoload.inc';
$conf['cache_backends'][] = 'sites/default/CustomRedisCache.php';

// Configure cache_form bin to use custom class.
$conf['cache_class_cache_form'] = 'AcccRedisCacheFormPhpRedis';

// TTL can be tweaked with this variable.
$conf['cache_lifetime_redis'] = 21600;

sites/default/CustomRedisCache.php

<?php

/**
 * Redis cache backend with configurable TTL.
 */
class CustomRedisCache extends Redis_Cache {
  /**
   * Make redis maximum TTL configurable.
   *
   * Overrides parent class behavior that aligns this value with cache_lifetime.
   */
  public function refreshMaxTtl() {
    $this->maxTtl = variable_get('cache_lifetime_redis', 21600);
  }
}
les lim’s picture

StatusFileSize
new596 bytes

Part of the problem here is that having a cache_lifetime setting makes the Redis backend treat CACHE_TEMPORARY and CACHE_PERMANENT entries exactly the same - both will get explicit expirations set at `cache_lifetime` seconds in the future. I'm okay with Redis using the cache_lifetime value that way for CACHE_TEMPORARY items, but CACHE_PERMANENT should continue to be treated with the permTTL default setting of 1 year.

Here's a patch to restore that. I'll leave the status as-is since the branch is end-of-life, but it might be helpful to someone else.

cafuego’s picture

Status: Closed (won't fix) » Needs work

I just wasted a lot of time checking why cached Drupal 7 data was disappearing and finally came to this issue.

Can we get at least that warning patch into the 3.x branch of the module, if not a way to simply allow specific per-bin maximum TTLs via $conf?

torgospizza’s picture

Status: Needs work » Needs review

Since there's still no 4.x branch, and 3.x is the "recommended version" for Drupal 7, I vote to commit the patch from #8. It's a small patch and appears to be a much-needed fix.

urashima82’s picture

Hello,

I encountered the same problem and patch #8 solves it. I also vote to commit the patch from #8.

pounard’s picture

OK, fair enough, I'll commit this asap.

  • Pierre.R committed d4d00d2 on 7.x-3.x authored by Les Lim
    Issue #2538902 by Les Lim, das-peter: cache_lifetime variable causes all...

pounard credited Pierre.R.

pounard’s picture

I commited the fix. I think that I did a mistake originally, and you saw it: permTtl was never in use whereas it was exactly meant for this. Thanks a lot!

pounard’s picture

I'm sorry for not seeing this earlier.

pounard’s picture

I created the 7.x-3.18 release, you now just have to wait a few minutes for the packages to be built.

pounard’s picture

Status: Needs review » Fixed
cafuego’s picture

Thank you @pounard 🎉

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.