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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:51:00+00:00 2026-05-26T03:51:00+00:00

How would you cache an ActiveResource model? Preferably in memcached. Right now it’s pulling

  • 0

How would you cache an ActiveResource model? Preferably in memcached. Right now it’s pulling a model from my REST API fine but it pulls dozens of records each time. Would be best to cache them.

  • 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-26T03:51:00+00:00Added an answer on May 26, 2026 at 3:51 am

    I’ve been playing around with the same thing and I think I’ve found a pretty simple way to check redis for the cached object first. This will only work when you use the find method, but for my needs, I think this is sufficient.

    By overriding find, I can check the checksum of the arguments to see if I already have the response saved in redis. If I do, I can pull the JSON response out of redis and create a new object right there. If I don’t, I’ll pass the find through to ActiveResource::Base’s find and the normal action will happen.

    I haven’t implemented the saving of the responses into redis with ActiveResource yet, but my plan is to populate those caches elsewhere. This way, normally I can rely on my caches being there, but if they aren’t, I can fall back to the API.

    class MyResource < ActiveResource::Base
      class << self
        def find(*arguments)
          checksum = Digest::MD5.hexdigest(arguments.md5key)
          cached = $redis.get "cache:#{self.element_name}:#{checksum}"
          if cached
            return self.new JSON.parse(cached)
          end
    
          scope   = arguments.slice!(0)
          options = arguments.slice!(0) || {}
          super scope, options
        end
      end
    end
    

    and a little patch so we can get an md5key for our array:

    require 'digest/md5'
    
    class Object
      def md5key
        to_s
      end
    end
    
    class Array
      def md5key
        map(&:md5key).join
      end
    end
    
    class Hash
      def md5key
        sort.map(&:md5key).join
      end
    end
    

    Does that help?

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

Sidebar

Related Questions

I have a REST API where I would like to cache the JSON response
How would you access the cache from a jQuery ajax call? I'm using jquery
I'm trying to write a script, which would cache images, but am stuck with
I would like to cache the result of the twitter api result and display
I'm writing a simple C++ class in which I would like cache picture thumbnails
I would like to cache my most database heavy actions in my asp.net-mvc site.
I would like to cache my website with memcache as much as possible. There
I am reading a CSV file and I would like to cache the results
Would there be any point in adding resx files to a distributed cache? What
we have a cache which I would like to put some transaction scopes around

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.