GithubHelp home page GithubHelp logo

redis-model's Introduction

redis-model

Minimal model support for [redis-rb](github.com/ezmobius/redis-rb). Directly maps ruby properties to model_name:id:field_name keys in redis. Scalar, list and set properties are supported.

Values can be marshaled to/from Integer, Float, DateTime, JSON. See Redis::Model::Marshal for more info.

Define

require 'redis/model'

class User < Redis::Model
  field :name,      :string
  field :created,   :datetime
  field :profile,   :json

  list  :posts,     :json

  set   :followers, :int
end

Write

u = User.with_key(1)
u.name = 'Joe'                      # set user:1:name Joe
u.created = DateTime.now            # set user:1:created 2009-10-05T12:09:56+0400
u.profile = {                       # set user:1:profile {"sex":"M","about":"Lorem","age":23}
  :age => 23,                       
  :sex => 'M',                      
  :about => 'Lorem'                 
}                                   
u.posts << {                        # rpush user:1:posts {"title":"Hello world!","text":"lorem"}
    :title => "Hello world!",
    :text  => "lorem"
}           
u.followers << 2                    # sadd user:1:followers 2

Read

u = User.with_key(1)
p u.name                            # get user:1:name
p u.created.strftime('%m/%d/%Y')    # get user:1:created
p u.posts[0,20]                     # lrange user:1:posts 0 20
p u.posts[0]                        # lindex user:1:posts 0
p u.followers.has_key?(2)           # sismember user:1:followers 2

redis-model's People

Contributors

voloko avatar

Stargazers

 avatar

Watchers

 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.