Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:40:25+00:00 2026-06-11T16:40:25+00:00

I am using Rails.cache (I have tried both default store and memcached with the

  • 0

I am using Rails.cache (I have tried both default store and memcached with the dalli gem – but I get the same behaviour) it seems to be working, but isn’t (logs show that the query is not made, but actual results show otherwise)… I have a search page that caches the top ten queries for 1 day:

search.rb

  def self.top_ten
    Rails.cache.fetch("top_ten_searches_cache", :expires_in => 1.day) do
      Search.order("count DESC").limit(10)
    end    
  end 

However, when I go to the search page, and enter a new search term, repeatedly, it begins to show in the top ten results! (Whereas I am not expecting the top ten results to change until 1 day passes.) When the cache store is default, no query shows in the log (so you think cache is working) and when using dalli/memcached I get exactly the same behaviour, but with this in the log:

Cache read: top_ten_searches_cache ({:expires_in=>1 day})
Cache fetch_hit: top_ten_searches_cache ({:expires_in=>1 day})

What does Cache fetch_hit mean exactly? Could it mean that no cache was found and is having to do the query?

Not really sure what’s going on – it’s odd that I get the same behaviour with either the default store or memcached with Dalli.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:40:27+00:00Added an answer on June 11, 2026 at 4:40 pm

    You’re caching the scope, not the actual results of the query. Rails uses lazy loading, so to force the query to execute you need to put .to_a (or .all in Rails 3), for example:

    def self.top_ten
      Rails.cache.fetch("top_ten_searches_cache", :expires_in => 1.day) do
        Search.order("count DESC").limit(10).to_a
      end    
    end 
    

    See here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails 3.1 application thats using Paperclip. It's working perfectly locally but
I am caching several User objects using Rails.cache.write but when I retrieve it from
I am using memcached (gem memcache-client ) in my application, but I want to
I have been using mysql in a new rails application, but now I wanted
I'm using rails 3.2 and Ruby 1.9.3 I have a need to store data
I want to use redis cache store (using redis-store gem). It works fine locally
I am trying to interact with the cache store in a Rails app using
I am using Passenger and Rails' :cache => true to cache all my css
Using Rails 3.2. I have an individual Shop post, and each shop has reviews.
Using Rails 3.1.3 with Ruby 1.9.3p0. I've discovered that by default, Rails does not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.