h1. dm-redis-adapter
This is a DataMapper adapter for the Redis key-value store.
Redis is a very fast key-value store with some interesting data structures added, and oh so much more. You can have a key that is a SET, LIST, STRING or HASH that is binary safe. Data structures like SET and LIST allow for even more interesting things. Redis is a fabulous and fast engine for data structures, and you can read more about it here: redis. Redis is also a persistent data store, and can be used in large-scale environments with master-slave replication and consistent hashing on the client side. Redis makes everyone happy and has been known to cause sunshine to spontaneously break out in clouded areas.
DataMapper is a brilliant ORM that is based on the IdentityMap pattern. Usage of DataMapper resembles that of ActiveRecord, the popular ORM bundled with Ruby on Rails, but with some very important differences. A quote from the DM wiki: "One row in the database should equal one object reference. Pretty simple idea. Pretty profound impact." Having an identity map allows for very efficient queries to the database, as well as interesting forms of lazy loading of attributes or associations.
Marrying DataMapper to Redis allows for schema-less models, you can add fields at any time without having to create a migration. DataMapper also allows us to store non-native Redis types in the db, like Date fields.
h1. Upgrading
Please note that as of version 0.5.3 of the gem, the key names that are used for storage have changed and break compatibility with previous versions!
h1. Changelog
- v0.10.1 Bugfixes and reduced runtime dependency (thanks @JonMidhir and @rabbit!)
- v0.9.0 Fixes for composite keys being indexed (thanks @fringley)
- v0.8.4 Fixes for composite keys (thanks @krider2010)
- v0.8.2 Updates to RedisAdapter#initialize to support URI. (thanks @thentenaar)
- v0.8.1 Dependency updates by @jof to support newer versions of the redis gem.
- v0.8.0 Updates by @sfeu to support composite natural keys.
- v0.6.4 Adding the Gemfile so that rake commands work (thanks @kellydunn!)
- v0.6.3 Fixes a problem with non-index, non-key queries (thanks @thecurator!)
- v0.6.2 Fixes a problem with 'destroyed' objects (thanks @snovotny!)
- v0.6.1 Fixes a problem with deleting an object and not having it deleted from an assosciation (thanks @sheuer!)
- v0.6.0 Refactor and change to the way that model names are stored in redis
- v0.5.3 Support for inheritance via sfeu and ujifgc, this version breaks compatibility with previous versions of the gem
- v0.4.0 Support for dm-core v1.1.0
- v0.3.0 Updates to support ruby 1.9.2 (thanks arbarlow!)
- v0.2.1 Fixes to sorting
- v0.1.1 Update to redis-rb v2.0.0
- v0.1 Update to store records as redis hash values
- v0.0.11 Updates to support newer versions of the redis client, support for JSON datatypes
h1. Install
Prerequisites:
Install the dm-redis-adapter:
> gem install dm-redis-adapter
h1. Usage
Setup your adapter, define your models and properties:
require 'rubygems'
require 'dm-core'
require 'dm-redis-adapter'
DataMapper.setup(:default, {:adapter => "redis"})
class Cafe
include DataMapper::Resource
property :id, Serial
property :name, Text
end
Cafe.finalize
Cafe.create(:name => "Whoahbot's Caffienitorium")
Now you can use redis in a ORM style, and take advantage of all of the amazing things that DataMapper offers.
If you want to do finds on specific String fields, add an index:
class Coffee
include DataMapper::Resource
property :id, Serial
property :description, String, :index => true
end
Coffee.create(:description => "Notes of crude oil and sulphur")
Coffee.first(:description => "Notes of crude oil and sulphur") # will now work
Validations on unique fields are now supported through indices and dm-validations:
class Crumblecake
include DataMapper::Resource
validates_is_unique :flavor
property :id, Serial
property :flavor, String, :index => true
end
Crumblecake.create(:flavor => "snozzbler")
Crumblecake.new(:flavor => "snozzbler").valid? # false (of course! Who ever heard of a snozzbler crumblecake?)
h1. Badass contributors