Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Branch: master
Failed to load latest commit information.
lib Redis::Marshal -> Redis::Marshaller (avoid NS collisions)
spec Updated gems and dependencies
.document Initial commit.
.gitignore Updated gems and dependencies
Gemfile Updated gems and dependencies
Gemfile.lock Updated gems and dependencies
LICENSE.txt Version bump to 0.1.0
README.mkd Massive updates to README so its actually useful
Rakefile Bump to 0.2.3
TODO.mkd Added TODO file.
redis-native_hash.gemspec Bump to 0.2.3



Tools to help expose Redis' powerful Hash type through a familiar Ruby Hash interface. NativeHash provides a general solution for exposing reasonably sized Redis hashes as Ruby hashes, including sane and transparent transactions, nested hash support, and automatic serialization of complex data types. BigHash is provided to efficiently handle big or even enormous Redis hashes. LazyHash is a convenient proxy for NativeHash useful when you aren't sure the hash will be read (useful for sessions).

Also included is Rack middleware to store sessions in Redis hashes, and a two Rails caching solution, one using Redis hashes and the other using Redis strings.

Example usage for NativeHash

require "redis_hash"

# Create a Ruby hash backed by Redis
hash =  # => {}
hash[:foo] = :bar
hash.key                     # => "20120512181125.368617.04d2abae82db62ece82b3805b654082b"                    # => true

# Retrieve an existing hash from Redis
existing = Redis::NativeHash.find(hash.key) # => {"foo" => :bar}

# Symbols and strings can be used interchangeably, sort of like HashWithIndifferentAccess
existing[:foo]    # => :bar
existing["foo"]   # => :bar

# Convert existing hash to Redis backed hash
hash = {yin: "yang"}
redis_hash =

# Create a hash with a custom key
hash.key = :custom_key
hash.key  # => :custom_key

# Use namespaces
hash =
hash.namespace  # => :custom_namespace
hash.key        # => "20120512212206.376929.5194d9ea37e2d1b6c773b860cce58c7d"
hash.redis_key  # => "custom_namespace:20120512212206.376929.5194d9ea37e2d1b6c773b860cce58c7d"

# Initialize with custom namespace and key
hash = "my_hash_key")
hash[:test] = "value"
hash.namespace  # => :custom_namespace
hash.key        # => "my_hash_key"
hash.redis_key  # => "custom_namespace:my_hash_key"       # => true

# Retrieve existing hash using namespace and key
existing = Redis::NativeHash.find(custom_namespace: "my_hash_key")  # => {"test" => "value"}

Example usage for BigHash

# Initializing a BigHash
big =  # => #<Redis::BigHash:0x007fcdfc8890d8 @key=nil, @namespace=nil>
big ="custom_key")
big ="custom_key", "app_namespace")

# No #save method as writes take place immediately
big = Redis::BigHash("my_key")
big.[:test] = "right now"
redis.hget("my_key", "test")  # => "right_now"

Usage for LazyHash

A simple lazy-loading proxy object that should behave identically to NativeHash. Check hash.loaded? if you need to know whether the underlying hash has been read.

Using as a Rails session store

Change your config/initializers/session_store.rb to look something like this:

require "redis_hash"
MyAppName::Application.config.session_store :redis_hash

Using Redis string-based caching implementation

Only the string based implementation is able to properly handle automatic expiration, so it is preferred.

Add the following line to the appropriate environment config in config/environments/

config.cache_store = :redis_store

To set a cache expiration, use a line like this:

config.cache_store = [:redis_store, :expires_in => 24.hours]

Client helpers

This gem adds a useful Redis::ClientHelper module to simplify both connection sharing and using multiple connections. Using Redis::Client.default= you can set the connection all future instances of NativeHash/BigHash/LazyHash will use.

# Changes to the default cascade down, unless class-level defaults have already been set
Redis::Client.default     # => #<Redis client v2.2.2 connected to redis:// (Redis v2.4.6)>
redis = 8)  # => #<Redis client v2.2.2 connected to redis:// (Redis v2.4.6)>
Redis::Client.default = redis
Redis::Client.default     # => #<Redis client v2.2.2 connected to redis:// (Redis v2.4.6)>
Redis::BigHash.redis      # => #<Redis client v2.2.2 connected to redis:// (Redis v2.4.6)>  # => #<Redis client v2.2.2 connected to redis:// (Redis v2.4.6)>

The client helper also lets you set the redis connection to use for an entire class, or a single instance.

Redis::BigHash.redis = 4)
Redis::BigHash.redis      # => #<Redis client v2.2.2 connected to redis:// (Redis v2.4.6)>
Redis::Client.default     # => #<Redis client v2.2.2 connected to redis:// (Redis v2.4.6)>
hash =
hash.redis = 5)
hash.redis                # => #<Redis client v2.2.2 connected to redis:// (Redis v2.4.6)>
Redis::BigHash.redis      # => #<Redis client v2.2.2 connected to redis:// (Redis v2.4.6)>

You can include the client helper into your own classes to give your own classes similar behavior.

class CustomClass
  include Redis::ClientHelper
CustomClass.redis         # => #<Redis client v2.2.2 connected to redis:// (Redis v2.4.6)>
x =
x.redis = 3)
x.redis                   # => #<Redis client v2.2.2 connected to redis:// (Redis v2.4.6)>

Note: Your own classes will use Redis::Client.default unless a class-level or instance-level connection is set.

See tests for more examples.

Contributing to redis-native_hash

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.


Copyright (c) 2011 Lyconic. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.