Generate nested namespaced keys for key-value databases.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Object Oriented Keys for Redis.


If you are familiar with databases like Redis and libraries like Ohm you already know how important it is to craft the keys that will hold the data.

>> redis =
>>"SADD", "event:3:attendees", "Albert")
>>"SMEMBERS", "event:3:attendees")
=> ["Albert"]

It is a design pattern in key-value databases to use the key to simulate structure, and you can read more about this in the case study for a Twitter clone.

Nest helps you generate those keys by providing chainable namespaces that are already connected to Redis:

>> event ="event")
>> event[3][:attendees].sadd("Albert")
>> event[3][:attendees].smembers
=> ["Albert"]


To create a new namespace:

>> ns ="foo")
=> "foo"

>> ns["bar"]
=> "foo:bar"

>> ns["bar"]["baz"]["qux"]
=> "foo:bar:baz:qux"

And you can use any object as a key, not only strings:

>> ns[:bar][42]
=> "foo:bar:42"

In a more realistic tone, lets assume you are working with Redis and dealing with events:

>> events ="events")
=> "events"

>> id = events[:id].incr
=> 1

>> events[id][:attendees].sadd("Albert")
=> "OK"

>> meetup = events[id]
=> "events:1"

>> meetup[:attendees].smembers
=> ["Albert"]

Supplying your existing Redis instance

You can supply a Redic instance as a second parameter. If you don't, a default instance is created for you:

>> redis ="redis://localhost:6379")
=> #<Redic:0x007fa640845f10 ...>

>> users ="users", redis)
=> "users"

>> id = users[:id].incr
=> 1

>> users[id].hset(:name, "Albert")
=> "OK"

Nest objects respond to redis and return a Redic instance. It is automatically reused when you create a new namespace, and you can reuse it when creating a new instance of Nest:

>> events ="events", meetup.redis)
=> "events"

>> events.sadd(meetup)
=> true

>> events.sismember(meetup)
=> true

>> events.smembers
=> ["events:1"]

>> events.del
>> true

Nest allows you to execute all the Redis commands that expect a key as the first parameter.

Differences with redis-namespace

redis-namespace wraps Redis and translates the keys back and forth transparently.

Use redis-namespace when you want all your application keys to live in a different scope.

Use Nest when you want to use the keys to represent structure.

Tip: instead of using redis-namespace, it is recommended that you run a different instance of redis-server. Translating keys back and forth is not only delicate, but unnecessary and counterproductive.

Differences with Ohm

Ohm lets you map Ruby objects to Redis with little effort. It not only alleviates you from the pain of generating keys for each object, but also helps you when dealing with references between objects.

Use Ohm when you want to use Redis as your database.

Use Nest when mapping objects with Ohm is not possible or overkill.

Tip: Ohm uses Nest internally to deal with keys. Having a good knowledge of Nest will let you extend Ohm to suit your needs.


$ gem install nest