Skip to content

Allow configuring the cache with a cache store instance - #1711

Merged
alexreisner merged 1 commit into
alexreisner:masterfrom
pioz:cache-store-instance
Aug 9, 2026
Merged

Allow configuring the cache with a cache store instance#1711
alexreisner merged 1 commit into
alexreisner:masterfrom
pioz:cache-store-instance

Conversation

@pioz

@pioz pioz commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Problem

Geocoder::Cache picks its cache store adapter from the class name of the configured cache: object:

@class = (Geocoder::CacheStore.const_get("#{store.class}", false) rescue Geocoder::CacheStore::Generic)

Any object whose class is not literally named Redis silently falls back to Geocoder::CacheStore::Generic, which does not apply cache_options[:expiration]. In our app the Redis client is wrapped in a small namespacing class, so the configured expiration: 2.days was silently ignored and keys accumulated in Redis with no TTL — we only noticed when Redis memory kept growing. The same pitfall is described in #1670 (comment).

Passing an already-built cache store instance — as the README suggests for Rails (cache: Geocoder::CacheStore::Generic.new(Rails.cache, {})) — only works by accident today: Geocoder::Cache wraps the instance in a second Generic adapter, whose duck-typing dispatch happens to delegate write/read to the inner adapter. expire is actually broken in that setup, because Generic#remove calls store.delete, which cache store adapters don't implement:

NoMethodError: undefined method 'delete' for an instance of Geocoder::CacheStore::Generic

Change

When the configured cache: is already a Geocoder::CacheStore::Base instance, use it directly instead of wrapping it. This gives an explicit way to choose the adapter for stores whose class name is not recognized:

Geocoder.configure(
  cache: Geocoder::CacheStore::Redis.new(MyRedisWrapper.new, {prefix: "geocoder:", expiration: 2.days})
)

Expiration support is still never guessed for unrecognized stores (in line with the intent expressed in #1670): the user opts in explicitly by picking the adapter.

Backward compatible: the class name lookup and the Generic fallback are unchanged for every other value of cache:. The README example for Rails.cache now also works fully (no double wrapping, expire included).

Related to #1670.

@alexreisner

Copy link
Copy Markdown
Owner

This is excellent. Thank you for the great code and clear explanation. Merging!

@alexreisner
alexreisner merged commit c10bd70 into alexreisner:master Aug 9, 2026
7 checks passed
@pioz

pioz commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

Yeah! Any change to get a new geocoder release (1.8.7) with this update?

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