The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Cache as a proxy object

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Florian Frank

Posts: 48
Nickname: ffrank
Registered: Dec, 2005

Florian Frank is a humanoid life-form, living on the third planet of the solar system.
Cache as a proxy object Posted: Jul 7, 2007 8:46 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Florian Frank.
Original Post: Cache as a proxy object
Feed Title: The Rubylution: Tag Ruby
Feed URL: http://rubylution.ping.de/xml/rss/tag/ruby/feed.xml
Feed Description: Starts… Now.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Florian Frank
Latest Posts From The Rubylution: Tag Ruby

Advertisement
# Small cache proxy object to cache the index.
class Cache
  instance_methods.each { |id| id =~ /\A__/ or undef_method id }

  # Caches data returned by block, for _duration_ seconds.
  def initialize(duration = 3600, &block)
    @duration = duration
    @block = block
    fetch_data
  end

  def fetch_data
    @data = @block.call
    @last_fetch = Time.now
  end
  private :fetch_data

  def method_missing(id, *a, &b)
    fetch_data if @last_fetch + @duration < Time.now
    @data.__send__(id, *a, &b)
  end
end

Read: Cache as a proxy object

Topic: Ayuda para Ruby y Rails en la línea de comando: FastRi Previous Topic   Next Topic Topic: Number One iPhone Annoyance

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use