Coder Social home page Coder Social logo

liqingrikeiikyeong / remodel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tlossen/remodel

0.0 3.0 0.0 294 KB

remodel -- a minimal ORM (object-redis-mapper) in a few hundred lines of ruby

Home Page: http://github.com/tlossen/remodel

License: MIT License

Ruby 100.00%

remodel's Introduction

remodel your storage layer

use redis instead of mysql to store your application data.

remodel (= redis model) is an ActiveRecord-like mapping layer which offers familiar syntax like has_many, has_one etc. to build your domain model in ruby.

entities are serialized to json and stored as fields in a redis hash. using different hashes (called 'contexts' in remodel), you can easily separate data belonging to multiple users, for example.

why redis?

redis offers in-memory read and write performance — on the order of 10K to 100K operations per second, comparable to memcached — plus asynchronous persistence to disk. for example, on my macbook (2 ghz):

$ redis-benchmark -d 100 -r 10000 -q
SET: 13864.27 requests per second
GET: 18152.17 requests per second
INCR: 17006.80 requests per second
LPUSH: 17243.99 requests per second
LPOP: 18706.54 requests per second

how to get started

  1. install redis and the redis-rb ruby client:

     $ brew install redis
     $ gem install redis
    
  2. start redis:

     $ ./redis-server
    
  3. now the tests should run successfully:

     $ rake
     Started
     .......................................................................................
     Finished in 0.072304 seconds.
     87 tests, 138 assertions, 0 failures, 0 errors
    

example

define your domain model like this:

class Book < Remodel::Entity
  has_many :chapters, :class => 'Chapter'
  property :title, :short => 't', :class => 'String'
  property :year, :class => 'Integer'
  property :author, :class => 'String', :default => '(anonymous)'
end

class Chapter < Remodel::Entity
  property :title, :class => String
end

now you can do:

>> require './example/book'
=> true
>> context = Remodel.create_context('shelf')
=> #<Remodel::Context(shelf)> 
>> book = Book.create context, :title => 'Moby Dick', :year => 1851
=> #<Book(shelf, 1) title: "Moby Dick", year: 1851, author: "(anonymous)"> 
>> chapter = book.chapters.create :title => 'Ishmael'
=> #<Chapter(shelf, 1) title: "Ishmael"> 

all entities have been created in the redis hash 'shelf' we have used as context:

>> Remodel.redis.hgetall 'shelf'
=> {"b"=>"1", "b1"=>"{\"t\":\"Moby Dick\",\"year\":1851}", "c"=>"1", 
   "c1"=>"{\"title\":\"Ishmael\"}", "c1_book"=>"b1", "b1_chapters"=>"[\"c1\"]"}

inspired by

  • how to redis — good overview of different mapping options by mattmatt.
  • hurl — basically defunkts Hurl::Model is what i started with.
  • ohm — object-hash mapping for redis. somewhat similar, but instead of serializing to json, stores each attribute under a separate key.

todo

  • better docs
  • make serializer (json, messagepack, marshal ...) configurable

status

it has some rough edges, but i have successfully been using remodel in production since summer 2010.

license

MIT

remodel's People

Contributors

tlossen avatar phuesler avatar

Watchers

James Cloos avatar EpicOneNine avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.