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

  • SEARCH
  • Home
  • 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 302475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:06:03+00:00 2026-05-12T07:06:03+00:00

I need to cache (and expire) all the models in a table. For example,

  • 0

I need to cache (and expire) all the models in a table.

For example, if i have a model named Currency, i only have less than 10 possible currencies. Therefore, it would be nice to have:

class Currency < ActiveRecord::Base
  cache_all(:expire_in => 10.minutes)
end

so that

Currency.all
Currency.find_by_name("USD")

should not hit the DB.

What do you think it could be a good approach?

Also, if you believe it would be better to use a model that is not backed up by a DB, please comment on this. Please notice that i would like to have a AR-style association.

  • 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-05-12T07:06:03+00:00Added an answer on May 12, 2026 at 7:06 am

    Since the data set is so small, probably the best thing is to cache it in local memory. There are a couple ways to do this, one is to use Memoization like I show here. However that isn’t the most efficient because it will store the all method and find_by_name method in separate caches even though they are the same object.

    An alternative is to redefine the methods to cache the objects manually. Something like this.

    class Currency < ActiveRecord::Base
      def self.all
        @all_cache ||= super.map(&:freeze) # freeze so you don't modify the cached objects
      end
    
      def self.find_by_name(name)
        all.detect { |c| c.name.to_s.downcase == name.to_s.downcase }
      end
    
      def self.flush_all_cache
        @all_cache = nil
      end
    end
    

    There may be a plugin to handle this for you, but I haven’t looked into that at all.

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

Sidebar

Ask A Question

Stats

  • Questions 188k
  • Answers 188k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You may have to go outside of DOMDocument - maybe… May 12, 2026 at 5:37 pm
  • Editorial Team
    Editorial Team added an answer There is a common technique to simulate additional out parameter… May 12, 2026 at 5:37 pm
  • Editorial Team
    Editorial Team added an answer Honestly, it looks pretty clear to me. I think that… May 12, 2026 at 5:37 pm

Related Questions

I've pretty much tried everything, but it seems impossible to use expire_fragment from models?
Quick background I have a concurrent map I used to cache some values that
I have a score i need to calculate for multiple items for multiple users.
Lets say you have a fragment of the page which displays the most recent

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.