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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:54:15+00:00 2026-05-16T16:54:15+00:00

I have an expensive (time-consuming) external request to another web service I need to

  • 0

I have an expensive (time-consuming) external request to another web service I need to make, and I’d like to cache it. So I attempted to use this idiom, by putting the following in the application controller:

def get_listings
  cache(:get_listings!)
end

def get_listings!
  return Hpricot.XML(open(xml_feed))
end

When I call get_listings! in my controller everything is cool, but when I call get_listings Rails complains that no block was given. And when I look up that method I see that it does indeed expect a block, and additionally it looks like that method is only for use in views? So I’m guessing that although it wasn’t stated, that the example is just pseudocode.

So my question is, how do I cache something like this? I tried various other ways but couldn’t figure it out. Thanks!

  • 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-16T16:54:16+00:00Added an answer on May 16, 2026 at 4:54 pm

    As nruth suggests, Rails’ built-in cache store is probably what you want.

    Try:

    def get_listings
      Rails.cache.fetch(:listings) { get_listings! }
    end
    
    def get_listings!
      Hpricot.XML(open(xml_feed))
    end
    

    fetch() retrieves the cached value for the specified key, or writes the result of the block to the cache if it doesn’t exist.

    By default, the Rails cache uses file store, but in a production environment, memcached is the preferred option.

    See section 2 of http://guides.rubyonrails.org/caching_with_rails.html for more details.

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

Sidebar

Related Questions

I have a fairly expensive array calculation (SpectralResponse) which I like to keep to
I have a table that looks something like this: word big expensive smart fast
I have a dataset in MySQL where using limit is already an expensive query,
I have a class in which looking up an instance is expensive, so instances
Source RegexOptions.IgnoreCase is more expensive than I would have thought (eg, should be barely
I'm wondering, how expensive it is to have many threads in waiting state in
Generally the database server is the biggest, most expensive box we have to buy
In general, how expensive is locking in Java? Specifically in my case: I have
In postgis, is the ST_GeomFromText call very expensive? I ask mostly because I have
I have written an HttpModule that accepts the request, processes it (via a 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.