Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update documentation with instructions for Rails.cache #1560

Merged
merged 1 commit into from
Jan 17, 2022

Conversation

xtr3me
Copy link
Contributor

@xtr3me xtr3me commented Jan 17, 2022

After upgrading we noticed that the configuration we had set originally resulted in a crash (which worked on version 1.6.3 but not after updating to 1.7.2):

Geocoder.configure(cache: Rails.cache)
ArgumentError: wrong number of arguments (given 1, expected 0)
from <abbreviated>/gems/activesupport-5.2.6/lib/active_support/cache/redis_cache_store.rb:166:in `initialize'

Initially I wrote a simple adapter around Rails.cache but when I was preparing a PR I noticed that we could use the Generic cache store wrapper to work with Rails.cache.

This PR helps the developer who uses Rails.cache to setup Geocoder's caching functionality.
It also instructs the developer to use the non deprecated way of setting the cache prefix as it was moved.

This was the adapter I initially wrote:

module Geocoder::CacheStore
  class RailsCache < Base
    def initialize(store = nil, options = {})
      super
      @store = ::Rails.cache
      @expiration = options[:expiration]
    end

    def write(url, value, expire = @expiration)
      if expire.present?
        store.write(key_for(url), value, expires_in: expire)
      else
        store.write(key_for(url), value)
      end
    end

    def read(url)
      store.read key_for(url)
    end

    def remove(key)
      store.delete(key)
    end

    private

    def expire
      @expiration
    end
  end
end

@alexreisner alexreisner merged commit f480e76 into alexreisner:master Jan 17, 2022
@alexreisner
Copy link
Owner

This is excellent. Thanks very much!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants