Pinpin
Pinpin

Reputation: 83

rails 3 and refreshed cached object

I'm new to caching, but I've got memcached working with rails and I'm using the Dalli gem.

In the rails console, i'm able to cache an object and then read it back out no problem

    Rails.cache.write("unique_posts",Post.new.get_uniques)
    posts=Rails.cache.fetch('unique_posts')

How do i set the refresh rate/expiring to the Rails.cache.write command?

Upvotes: 0

Views: 83

Answers (1)

Paritosh Piplewar
Paritosh Piplewar

Reputation: 8122

You just have to pass the :expires_in option. So, basically this will work

Rails.cache.write("unique_posts",Post.new.get_uniques, expires_in: 10.minutes)
posts=Rails.cache.fetch('unique_posts')

Pro tip, do this.

posts = Rails.cache.fetch('unique_post' , expires_in: 10.minutes) { Post.new.get_uniques }

Upvotes: 1

Related Questions