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")
$redis.hget("my-hash", "color")
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')
cache_fetch(key, expires_in:, &block)
- Get or set a value (to be stored with Marshal) expiring in some number of seconds
ret = $redis.cache_fetch('my-key', expires_in: 60) do
'cached-value'
end
ret = $redis.cache_fetch('my-key', expires_in: 60) do
'something-else'
end
mdump(key, val)
- Serialize and store a value at key
using Marshal
$redis.mdump('my-key', [1,2,3])
mload(key)
- Retrieve a Marshalled value from key
$redis.mdump('my-key', [1,2,3])
$redis.mload('my-key')
$redis.mload('does-not-exist')
Contributing
- Fork it ( http://github.com//redis-client-extensions/fork )
- Create your feature branch (
git checkout -b my-new-feature
)
- Commit your changes (
git commit -am 'Add some feature'
)
- Push to the branch (
git push origin my-new-feature
)
- Create new Pull Request