pantheon-systems / wp-redis

WordPress Object Cache using Redis.
https://wordpress.org/plugins/wp-redis/
GNU General Public License v2.0
225 stars 68 forks source link

Question about expiration of group keys from the readme #318

Closed johnolek closed 3 years ago

johnolek commented 3 years ago

The readme states this about using true redis cache groups via WP_REDIS_USE_CACHE_GROUPS:

However, when enabled, the expiration value is not respected because expiration on group keys isn't a feature supported by Redis.

I'm not 100% sure what this means. If I store something in the cache with a group and an expiration time, will that time not be respected for the individual piece of data? Or does this just mean that I can't set an expiration time for the group as a whole?

danielbachhuber commented 3 years ago

Hey @johnolek,

Thanks for the question!

However, when enabled, the expiration value is not respected because expiration on group keys isn't a feature supported by Redis.

If I store something in the cache with a group and an expiration time, will that time not be respected for the individual piece of data? Or does this just mean that I can't set an expiration time for the group as a whole?

The statement applies only if you enable native Redis cache groups using the WP_REDIS_USE_CACHE_GROUPS constant.

By default, WP Redis will hash your $key and $group into one longer key. Redis supports setting an expiry on individual cache values, so this longer key gets whatever expiry value you provide.

If you enable WP_REDIS_USE_CACHE_GROUPS, then WP Redis doesn't hash your $key and $group into one longer key. Instead, it passes the $key as the cache key and the $group as the Redis cache group. This lets you clear cache groups by group name if you need the feature. When using cache groups, Redis doesn't support setting an expiry value, however, hence the notice.

Does this help clarify? It's a bit of an obtuse subject, so I'm not sure if I'm helping or muddying the waters...

johnolek commented 3 years ago

This does make sense, thanks for the explanation.

A few clarifying questions though:

  1. If I have Redis cache groups enabled, will expiry values be respected for data I set where I don't specify a group name?
  2. Is there a way to set a default expiry time for groups as a whole, or will they always essentially have an expiry of 0?
  3. Can specific pieces of data inside a group still be cleared, or do the groups always need to be cleared as a whole?

At this point I'm sure that I don't want to use native Redis groups because it seems like it could lead to confusing behavior and I don't see any downsides to using the composite key approach for my use case. That doesn't stop my curiosity from getting the best of me and wanting to know the answers to these questions. And of course the answers might be helpful to other people who stumble across this in the future.

Thanks again for taking the time to respond in detail!

danielbachhuber commented 3 years ago
  1. If I have Redis cache groups enabled, will expiry values be respected for data I set where I don't specify a group name?

With the constant, it's all or nothing.

However... in looking at the codebase again, I realized we have a wp_cache_add_redis_hash_groups() function you can use to only opt specified group names into Redis cache groups.

  1. Is there a way to set a default expiry time for groups as a whole, or will they always essentially have an expiry of 0?

I'm not sure, to be honest. I believe the latter but there might be explicit mention somewhere in the docs: https://redis.io/documentation

  1. Can specific pieces of data inside a group still be cleared, or do the groups always need to be cleared as a whole?

They can be deleted individually.

Thanks again for taking the time to respond in detail!

You're welcome! Feel free to weigh in with any other questions, or discoveries you might make.