Ozymandias

Ozy is a simple Hash subclass that persists itself to Redis, if you'll let it. This provides a clean interface to storing data in Redis without calling all those crazy Redis commands. Of course, Ozy also supports those commands, so go nuts.

Install

Install using Rubygems:

gem install ozy

Configuration

Start by requiring Ozy (either in your Gemfile or manually, like so):

require 'rubygems'
require 'ozy'

Connect to Redis

Ozy will automatically try to connect to a locally running Redis, unless you tell it otherwise:

Ozy.connection = Redis.new(YAML.load_file(File.join(Rails.root, 'config', 'redis.yml'))[Rails.env].symbolize_keys)

Usage

Ozy's are just Hashes, but accept a hash for its initialization:

@ozy = Ozy.new(:foo => :bar)

Persistence

An Ozy won't persist until you tell it to by calling "save" and passing it a key:

@ozy.save(:key)

You can get an Ozy using that key:

Ozy.get(:key) #=> key: {:foo => :bar}

Expiration

You can pass an options hash into an Ozy initializer as well. Currently, the only supported option is expire:

@ozy = Ozy.new({:foo => :bar}, :expire => 60)
@ozy.expires_in #=> 60

Ozy's expiration times won't start ticking down until you save them. Cause they're not in Redis yet. Are you getting it?

@ozy.save(:key)
sleep 1
@ozy.expires_in #=> 59

You can extend an Ozy's expiration like so:

@ozy.expire_in! 120
@ozy.expires_in #=> 120

Automatic Persistence

Changes made to a saved Ozy will automatically persist to Redis without calling save again:

@ozy = Ozy.new(:foo => :bar)
@ozy.save(:key)
@ozy.merge!(:foo => :baz)
@ozy[:your] = :mom
Ozy.get(:key) #=> foo: {:foo => :baz, :your => :mom}

If you want to skip the whole "save" thing, just call Ozy.create(key, attributes):

@ozy = Ozy.create(:key, :i_will => :hunt_ed)
Ozy.get(:key) #=> key: {:i_will => :hunt_ed}

Redis Commands

Ozy's accept any key-based Redis command, as well. Example:

@ozy.del
Ozy.get(:key) #=> nil

And... that's it!