Sha256: 59f5bb2e89d481736f9d82e99d204ee55c8ecf743f361a7fa6c2f6c2ba38146e

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

= Lawnchair

Very simple caching mechanism for arbitrary pieces of ruby code using Redis as the distributed (or local) cache

== Prerequisites
http://code.google.com/p/redis/
and
http://github.com/ezmobius/redis-rb/

== Usage Examples


All you really need to do is wrap some expensive piece of Ruby code in the Lawnchair::Cache.me method as a block and it will be evaluated and the return value will cached in the given cache key.

First, connect to the Redis database

  Lawnchair.connectdb

This will connect to a default database on localhost, if you want to connect to a particular database you can do:

  Lawnchair.connectdb(Redis.new(:database => 11, :host => "127.0.0.1", :port => 6379))

Obligatory example:
  
  Lawnchair::Cache.me(:key => "contrived_example") do
    # ideally this would be something a little more computationally expensive, but sleep will have to do
    (1..3).inject([]) do
      |set, i| set << Time.now.strftime("%H:%M:%S")
      sleep 1
      set
    end
  end
  
The return value is exactly what you think it should be

  ["12:26:08", "12:26:09", "12:26:10"]

Now, since it is cached, any time this block method is called (for the next 60 minute) it will return those values.  also, you will note it comes back instantly, instead of waiting on those sleeps.

If an hour is too long, or short for the cache key expiration you can set that to anything you want using the :expires_in hash key and entering a time in seconds

  Lawnchair::Cache.me(:key => "contrived_example", :expires_in => 1.day) do
    # expensive code to be cached for 24 hours
  end

If you need to manually expire a key just call:

  Lawnchair::Cache.expire("contrived_example")

If you need to flush all the values in the database

  Lawnchair.flushdb


== Note on Patches/Pull Requests
 
* Fork the project.
* Make your feature addition or bug fix.
* I will promply ignore anything that is not a refactor that does not have associated specs :)
* Have fun

== Copyright

Copyright (c) 2010 Shane Wolf. See LICENSE for details.

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lawnchair-0.3.1 README.rdoc