sorentwo / readthis

:newspaper: Pooled active support compliant caching with redis
MIT License
504 stars 40 forks source link

Overriding expiration period #27

Closed tobinibot closed 8 years ago

tobinibot commented 8 years ago

I have the following configuration in my development.rb configuration file:

config.cache_store = :readthis_store, {
    expires_in: 1.day.to_i,
    namespace: 'xxx',
    redis: { host: config.redis_host, port: config.redis_port, db: config.redis_db_for_caching, driver: :hiredis }
}

In my controller, there are pieces of data I want to cache for different time periods.

# example 1
data = Rails.cache.fetch(:data_object_one, expires_in: 15.minutes) do
    # build expensive data structure
end

# example 2
data = Rails.cache.fetch(:data_object_two, expires_in: 4.hours) do
    # build expensive data structure
end

The data is getting stored in Redis, but the TTL starts counting down from 86400 (1 day), not the specific time period I'm trying to use. Is the expires_in option not supported?

sorentwo commented 8 years ago

The expires_in option is certainly supported. You'll want to convert the period into seconds by using to_i. Change your first example from 15.minutes to 15.minutes.to_i.

tobinibot commented 8 years ago

I did that. Also tried just a straight number. Still getting the 86400 TTL.

Rails.cache.fetch(:test_data, expires_in: 60) do
    'hello world'
end

Rails.cache.fetch(:test_data, expires_in: 1.minute.to_i) do
    'hello world'
end
tobinibot commented 8 years ago

Sorry if that last comment came off a bit brusque, I didn’t mean it to.

I'm currently trying to see if I get RubyMine to debug through the gem code as well to see if I can see where it’s going wrong, but I haven't been able to figure that out yet. I’ll let you know if I'm able to come up with any additional data.

I'm using readthis v 1.0.0, by the way.

tobinibot commented 8 years ago

Fixed by PR https://github.com/sorentwo/readthis/pull/28