0

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?

Pinpin
  • 83
  • 1
  • 9

1 Answers1

1

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 }
Paritosh Piplewar
  • 7,982
  • 5
  • 26
  • 41