Code

Art | Bicycling | Food | Hacks | Making Stuff | Music | Wirehead
Home | Mail | Search
WireWorld > Hacks > Marginal > Simplecache.rb

Simplecache.rb

Simplecache.rb was dashed off in a rush.

But I still wanted it to be nice to use. So I spent a little bit of time on making it a handy little caching tool.

I also thought that people need a reminder about how to write graceful Ruby code. :) So I spent a little bit of thinking on how I would make a really ruby-centric API.

I ended up with a single function. If the value is in the cache, it'll return it. If the value isn't in the cache, it'll run the block and cache that value. And I assume that most of the time, you aren't going to want to, but it does let you not cache a value. Convention over configuration, guys. :)

example of usage:

$pagecache = Simplecache.new(200,60*60*4)

op = $pagecache.work(curpathstr) {
  node = Rm::BaseNode.frompath(curpath)

  val = node.http_send(state["command"],state,cgparams,meta) 
  if meta.nocache?
     SimpleCache::CacheControl(val,Simplecache::NOCACHE)
  else
     val
  end
}

$pagecache.expire

Download source / View source

Copyright 2007, Ken Wronkiewicz
Version 4.0
Last Updated: 2007-10-23 12:10AM
Posted: 2007-10-20 11:34PM