RedisAssist - Easy Redis Backed Object Modeling

Documentation: http://docs.redisassist.com

RedisAssist is a Persistant Object Model backed by Redis for Ruby.

Store and Fetch data of any type in Redis with as little friction as possible. RedisAssist lets you back simple or complex Object Models with Redis while giving you a convenient interface interact with it.

Getting Started

class Person < RedisAssist::Base
  attr_persist :name
  attr_persist :birthday,     as: :time
  attr_persist :meta_info,    as: :json
  attr_persist :created_at,   as: :time # Magic date fields just like ActiveRecord.

  def validate
    add_error(:name, "Albert Einstein is dead.") if name.eql?('Albert Eintein')
  end 
end

Saving

person = Person.new(name: 'Albert Hoffman', birthday: Time.parse('1/11/1906'), meta_info: { profession: 'Scientist' })
person.new_record?  # => true
person.name         # => "Albert Hoffman"
person.save         # => #<Person:0x007f88341662a0 @attributes={:name=>"Albert Hoffman", :birthday=>-1993402800.0, :meta_info=>"{\"profession\":\"Scientist\"}"}
person.new_record?  # => false

Creating

person = Person.new(name: 'Albert Hoffman', birthday: Time.parse('1/11/1906'), meta_info: { profession: 'Scientist' })

Validating

person = Person.new(name: 'Albert Einstein', birthday: Time.parse('1/11/1906'), meta_info: { profession: 'Scientist' })
persin.valid?       # => false
persin.errors       # => [{name: "Albert Einstein is dead."}]

Fetching

person = Person.Find(1)

# When passing an array of ids, RedisAssist will load everything using a single pipeline
people = Person.find([1, 2])

Deleting

Deletes all the persisted attributes from redis.

person = Person.find(1)
person.delete       # => true

"Soft delete" is built into redis-assist. Simply add a deleted_at property to your model.

attr_persist :deleted_at, as: :time 

You can fetch soft deleted records by setting a deleted property when calling #find

Person.find(1, deleted: true)

Transforms

Since Redis only supports string values RedisAssist provides an interface for serialization in and out of Redis.

Currently RedisAssist natively supports the following types.

  • String default
  • Boolean
  • Float
  • Integer
  • JSON
  • Time
  • list native redis list data type
  • hash native redis hash data type

RedisAssist also provides an elegant API for defining custom transforms.

# Serialize/Deserialize objects with the MessagePack gem
class MessagePackTransform < RedisAssist::Transform
  def self.to(val)
    val.to_msgpack
  end

  def self.from(val)
    MessagePack.unpack(val) 
  end
end

To use the MessagePackTransform we just defined

attr_persist :really_long_biography, as: :message_pack      

Requirements

redis-rb

Configuration

You can configure RedisAssist with your own redis client

RedisAssist::Config.redis = Redis.new([...])