Skip to content

Conversation

@takehidemori
Copy link

Summary

Fix fetch_with_batched_keys to properly set Redis TTL when expires_in option is provided.

Problem

Currently, fetch_with_cache_register writes cache entries directly using redis.set without passing the ex: parameter, even when expires_in is specified. This causes Redis keys to persist indefinitely (TTL = -1), leading to memory growth in self-hosted environments.

The existing test passes because ActiveSupport::Cache::Entry stores expires_at internally and expiration is checked on the Ruby side during deserialization. However, Redis itself never evicts these keys.

Changes

  • Pass ex: option to redis.set when expires_in is provided
  • Add tests to verify Redis TTL is actually set

Testing

redis-cli TTL "cache_key_created_with_expires_in"
# Before: -1 (no expiration)
# After: 3600 (or whatever expires_in was set to)

Affected Areas

All ~40 call sites of fetch_with_batched_keys will now properly respect expires_in, including:

  • user_learning_object_scopes.rb (already passes expires_in: but was ineffective)
  • User, Course, Account caches
  • Assignment overrides
  • Role permissions
  • And more

Also fall back to Rails.cache.options[:expires_in] when expires_in is not explicitly passed. This ensures all ~40 call sites respect the cache_store.yml configuration.
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.

2 participants