Skip to content

Added support for expiry on the Redis backend.#206

Closed
roblinton wants to merge 2 commits intopsf:masterfrom
roblinton:redis-ttl
Closed

Added support for expiry on the Redis backend.#206
roblinton wants to merge 2 commits intopsf:masterfrom
roblinton:redis-ttl

Conversation

@roblinton
Copy link
Copy Markdown

The CacheController does not pass an expiry key to supported backends, aka. Redis. This commit adds support for deriving a TTL from the response headers and passing that to the cache backend allowing the cache to expire old caches automatically.

When using this library in an environment with lots of traffic and rapidly expiring caches we noticed that the Redis backend never received an expiry argument. We added this before noticing an existing pull request for the same functionality. That pull request appears to be fairly old and no longer merges so I've updated our changes to incorporate some of the notes on the old one.

Some notes:

  • I changed the argument to RedisCache.set() from a date to an int and put the logic for determining the expiry time in CacheController.get_cache_expiry()
  • I changed the Redis method from setex(name, expiry, value) to set(name, value, ex=expiry) to avoid the whole argument order affair.
  • I added CacheController.CACHE_TTL_MAX to avoid storing keys forever in Redis and allow users to enforce a maximum cache duration (defaults to 2 weeks). This is potentially outside the scope of this feature, if it creates problems let's drop it.

The CacheController does not pass an expiry key to supported backends,
aka. Redis. This commit adds support for deriving a TTL from the
response headers and passing that to the cache backend allowing the
cache to expire old caches automatically.
Redis complains when the `ex` value is set to 0 in set().  This turns 0
values into None.

If a truly incorrect value is present, it probably makes sense to raise
the exception.
@yurkovoznyak
Copy link
Copy Markdown

Hello @ionrock, any chance of this getting merged?

@roblinton
Copy link
Copy Markdown
Author

Implemented with #233

@roblinton roblinton closed this Dec 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants