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 7665349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:32:31+00:00 2026-05-31T14:32:31+00:00

I have a model with about 70 rows that is static(and likely will be

  • 0

I have a model with about 70 rows that is static(and likely will be forever). The information in this table is used in hundreds of places throughout my site and in my API. It is also associated with several other models that are not so small.

I’m trying to figure out the best way to go about caching this so I can avoid having to go to the DB every time to access it. I’ve looked at Redis and Memcached and understand how I could use these tools. However, I’m hoping there is a way to do this without having to change all the references to this data (ie – The ActiveRecord calls and associations). What would be my best bet?

  • 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-31T14:32:33+00:00Added an answer on May 31, 2026 at 2:32 pm

    A simple way to perform this is to simply cache the results somewhere in your class:

    class MyThing < ActiveRecord::Base
      def self.cached_all
        @cached_all ||= self.all
      end
    end
    

    The caveat here is that if you do change something you have no way of force-expiring it, so you’ll have to restart your server process. For some things this isn’t an issue, like lists of countries, time-zones, and so forth, where the rate of change is very limited.

    It’s probably best if you cache the data in a format suitable for retrieval, such as a Hash indexed by ID, making it easy to use:

    class MyThing < ActiveRecord::Base
      def self.cached_all
        @cached_all ||= Hash[self.all.collect { |m| [ m.id, m ] }]
      end
    end
    

    For those situations where you need more control over caching, you can go the Memcached route using the Rails.cache facility. It’s also worth noting that caching models with a large amount of data you never use is a bad idea, so be sure to keep them slim. Don’t load in giant binary attachments, for instance, unless they’re required.

    Keep in mind there’s no magical way to make other models use this cache, so you’ll have to refer to it deliberately in each case where you would otherwise bang against the database table. For instance:

    def cached_my_thing
      MyThing.cached_all[self.my_thing_id]
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Concept table of about 1000 rows that I would like to
so basicly I have a table that consist about 700million rows, and constantly updating
I have the model Member that contains all informations about the registered member on
I have a data structure that looks like this: Model Place primary key id
I have two tables: events_archive and reports. The events table shows all information about
I have a question about Android Market/Google Play license model for paid applications. First
I have a question about the proper, best way to manage the model. I
I have two models: Person and Relation. The second one stores information about relations
I have model Article it has field title with some text that may contain
How is it possible that this function below inserts two rows in my database?

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.