soveran / ohm

Object-Hash Mapping for Redis

Home Page:http://ohm.keyvalue.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for Redis set, hash, list in Ohm

phuongnd08 opened this issue · comments

Redis support set, hash, list at its nature. Would be nice if we have something like that in Ohm.
Perhaps a field that return a Nest where user can use smembers, hset, hget...
I'm using this kind of stuff:

def field_name
   Nest.new("MyModel")[id][:field_name]
end

but it would be cool if it's built in to Ohm, just to have the key name consistent with other field

You mean aside from the set and list attribute types provided by Ohm, right? Something low level?

Right, the low level thing.

Something I use is the track directive. It let's you create keys in the same namespace of the object, and tie its life cycle to that of the object itself, so if the object is removed, so is the key. The track directive is used internally by Ohm, but it's a public method and the usage is very simple. You can check the code and the changelog entry.

Once you are tracking a key, you can use it like this:

require "ohm"

class Stack < Ohm::Model
  track :stack

  def push(item)
    redis.call("RPUSH", key[:stack], item)
  end

  def pop
    redis.call("RPOP", key[:stack])
  end
end

stack = Stack.create
stack.push("foo")
stack.push("bar")
stack.pop #=> "bar"
stack.pop #=> "foo"
stack.pop #=> nil

Awesome! Exactly what's I'm looking for.