RedisClientExtensions

A set of useful core extensions to the redis-rb client library, extracted from KnowMyRankings.

Installation

Add this line to your application's Gemfile:

gem 'redis-client-extensions'

And then execute:

$ bundle

Or install it yourself as:

$ gem install redis-client-extensions

The extensions will add themselves to the Redis client automatically - in our Rails app, we have an initializer that looks something like

require 'redis-client-extensions'

$redis = Redis.new(...)

The extensions also work with MockRedis out of the box if it is installed.

Note that the library is coded against Ruby 2.1+.

Usage

Currently added functions are:

  • hmultiset(hkey, hash) - convenience method for storing a Ruby hash into a redis hash
$redis.hmultiset("my-hash", { name: "tom", color: "red" })
$redis.hget("my-hash", "name") # => "tom"
$redis.hget("my-hash", "color") # => "red"
  • find_keys_in_batches(options) - A wrapper for SCAN to iterate over keys matching a pattern. Options are passed through to SCAN.
$redis.find_keys_in_batches(match: "mypattern*", count: 100) do |keys|
  puts "Got batch of #{keys.count} keys"
  puts "Values for this batch: #{$redis.mget(keys)}"
end
  • get_i(key) - Parse an Integer stored at a key
$redis.get_i('price') # => 9
  • cache_fetch(key, expires_in:, &block) - Get or set a value (to be stored with Marshal) expiring in some number of seconds
# Initial cache miss, block evaluated to store value
ret = $redis.cache_fetch('my-key', expires_in: 60) do
  'cached-value'
end
# => 'cached-value'
#
# Calling again retrieves cached value, block will not be called
ret = $redis.cache_fetch('my-key', expires_in: 60) do
  'something-else' # Not called!
end
# => 'cached-value'
  • mdump(key, val) - Serialize and store a value at key using Marshal
$redis.mdump('my-key', [1,2,3]) # => 'OK'
  • mload(key) - Retrieve a Marshalled value from key
$redis.mdump('my-key', [1,2,3])
$redis.mload('my-key') # => [1,2,3]
$redis.mload('does-not-exist') # => nil

Contributing

  1. Fork it ( http://github.com//redis-client-extensions/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request