alexreisner/geocoder

Adding Expiration date through cache_options results in a TTL of -1 in Redis Cache

nzraad opened this issue ยท 7 comments

Adding Expiration date through cache_options results in a TTL of -1, versus following the example below actually adds the correct TTL.

class AutoexpireCacheRedis
def initialize(store, ttl = 86400)
@store = store
@ttl = ttl
end
def [](url)
@store.get(url)
end
def []=(url, value)
@store.set(url, value)
@store.expire(url, @ttl)
end
def keys
@store.keys
end
def del(url)
@store.del(url)
end
end
Geocoder.configure(:cache => AutoexpireCacheRedis.new(Redis.new))

Expected behavior

According to the documentation, you could add the expiration to the cache options in the geocoder initializer.

# config/initializers/geocoder.rb
Geocoder.configure(
  cache: Redis.new,
  cache_options: {
    expiration: 2.days, # Redis ttl
  }
)

Actual behavior

Following the configuration above will result in a TTL in Redis as -1

Steps to reproduce

  1. In geocoder initializers, add expiration in cache_options.
# config/initializers/geocoder.rb
Geocoder.configure(
  cache: Redis.new,
  cache_options: {
    expiration: 2.days, # Redis ttl
  }
)
  1. Trigger geocoder on an API request.
  2. Check Redis for new cached API Request.
  3. TTL is showing -1.
    CleanShot 2022-01-10 at 09 48 48@2x

Environment info

  • Geocoder version: 1.6.3 & 1.7.2
  • Rails version: Ruby 2.7.4 & Rails 6.1.4.3
  • Database (if applicable): through Docker

CleanShot 2022-01-10 at 09 54 06@2x

  • Lookup (if applicable): ipinfo (Paid API Key)

Thanks for reporting this. I'll get back to you as soon as I have a chance to investigate. (@shqear93 any ideas?)

Also, our current implementation allows us to add TTL into Redis is as follows:

class AutoExpireCache
  def initialize(store, ttl = 1.day)
    @store = store
    @ttl = ttl
  end

  def [](url)
    @store.read(url)
  end

  def []=(url, value)
    @store.write(url, value, expires_in: @ttl)
  end

  def expire
    @store.expire
  end
end

Geocoder.configure(
  ip_lookup: :ipinfo_io,   
  api_key: ##############,
  cache: AutoExpireCache.new(Rails.cache)
)

Hmm, I think #1532 made the caching examples obsolete. We should probably remove those.

@nzraad actually I did the same steps you mentioned, and I have it was working fine, the newly created redis record had the correct TTL, and I tested and traced the the code from master branch and the TTL was actually arriving to Redis service

@nzraad I think you triggered the link first time without expiration config, then you added the expiration to the initializer, in that case it won't update the current record in redis so you have to remove it from redis first and try again, please try and let me know if if worked

Okay, looking into it more, it appears using Rails.cache is the reason it does not add the TTL key correctly. I will keep testing it out to see if that is the only configuration issue that is not allowing it to pass the TTL.

@nzraad I think we should close this if you figure it out