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.
geocoder/examples/autoexpire_cache_redis.rb
Lines 6 to 30 in 934a5a9
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
- In geocoder initializers, add expiration in
cache_options
.
# config/initializers/geocoder.rb
Geocoder.configure(
cache: Redis.new,
cache_options: {
expiration: 2.days, # Redis ttl
}
)
Environment info
- Geocoder version:
1.6.3
&1.7.2
- Rails version: Ruby
2.7.4
& Rails6.1.4.3
- Database (if applicable): through Docker
- 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.