And you thought all the clever caching names were taken.

What is it

ActsAsCachola is a plugin that lets you cache any class method by simply prepending ‘cachola_’ to the method name when calling it. Here’s how it works:

Given the following model:

class Internet < ActiveRecord::Base
  acts_as_cachola
 
  def self.get_a_million_numbers
    1.upto(1_000_000).inject([]){ |numbers, x| numbers << x }
  end
end

Now you can call the method, ‘cachola_get_a_million_numbers,’ and the return value of ‘get_a_million_numbers’ will be cached automatically.

Note that if the method accepts arguments, each unique call will have its own key in the cache. For example:

class Internet < ActiveRecord::Base
  acts_as_cachola
 
  def self.get_numbers(to_number)
    1.upto(to_number).inject([]){ |numbers, x| numbers << x }
  end
end

Calling Internet.cachola_get_numbers(100) and Internet.cachola_get_numbers(500) will result in two keys (with different values) stored in the cache.

The cached method is then expired automatically when the class in which the plugin has been included is saved or destroyed. It’s restored to the cache the next time it’s called.

Now, what if your Internet class method ‘get_a_million_numbers’ depends on other objects getting saved or destroyed? That’s the other thing I wanted to make easier. Rather than setting up observers or sweepers, you can add the following to the other model:

class WhereAmI < ActiveRecord::Base
  acts_as_cachola_notifier => [:internet]
end

Now when your WhereAmI model is ether saved or destroyed, the cached methods in the Internet model will be deleted.

Installation

script/plugin install git://github.com/rbrant/acts_as_cachola.git

Where is this going from here?

Not sure. It does what I need it to do right now. It’s something I’ve found myself doing on two different projects that I thought would just make my life easier.

Project Info

ActsAsCachola is hosted on Github: http://github.com/rbrant/acts_as_cachola, where your contributions, forkings, comments and feedback are greatly appreciated. Please do add tests if you want me to pull in any changes.

{ 0 comments }

I want to show you some SAX.

by Rich Brant on March 12, 2010

I had to process some pretty big xml docs recently from the USPTO. Each doc is about 60mb and (oddly enough) contains several thousand individual documents all concatenated. So the document isn’t valid xml..but that’s a different story.

The reason for writing this was to show a quick demo of how to use SAX to process a large XML file. You can read about SAX here, but basically, SAX (Simple API for XML) is an event-driven model that solves the problem of having to read an entire tree structure into memory which can be realllly sloooow, and instead reads the stream of data and raises events along the way.

The code below uses the Nokogiri library (which as a side note has this odd, albeit entertaining tagline: “XML is like violence – if it doesn’t solve your problems, you are not using enough of it.”). Most other XML parsing libraries also have SAX implementations.

What the code does below is looks for the root node of each doc and builds a string for each individual document. After the doc has been assembled, the doc can be processed via the more pleasant:

doc = Nokogiri::HTML(xml)
serial = doc.css("application-reference document-id doc-number").inner_text

So this ends up being sort of a hybrid and much, much faster than loading the entire doc at once. It would be faster not parsing the doc again at all but the docs have too much nested complexity that requires the ability to use xpath to get at what I need.

{ 0 comments }

Good stuff from Bob Martin on software. Like the stuff at 11:20 inre tech/biz disconnect. Not sure about the shirt, however.

February 17, 2010

Posted via email from Rich’s posterous

Read the full article →

Seth Godin’s ebook, ‘What matters now’ Free download. Tons of great ideas/things to think about.

December 15, 2009

Download now or preview on posterous what-matters-now-2.pdf (3073 KB) Posted via email from Rich’s posterous

Read the full article →

Project review: ifwinsight.com

December 6, 2009

It’s easy to forget what you’ve learned and what tools you used from project to project. I thought it might be worthwhile to sort of sum up these things either on a weekly basis or project basis. I had a lot of fun on a recent project and thought it would be a good place [...]

Read the full article →