Redis Classy
A very simple, class-based namespace prefixing and encapsulation for Redis. Key features include:
- Establishes a maintainable convention by prefixing keys with the class name (e.g.
YourClass:123
) - Delegates all method calls to the redis-rb within the namespace
- Adds a better abstraction layer around Redis objects and commands
Here's an example:
class Timer < RedisClassy
def start
pipelined do
set Time.now.to_i
expire 120.seconds
end
end
def stop
del
end
def running?
!!get
end
end
timer = Timer.new(123)
timer.start
timer.running?
=> true
Timer.keys
=> ["123"]
RedisClassy.keys
=> ["Timer:123"]
The Timer class above is self-contained and more readable.
This library is made intentionally small, yet powerful when you need better abstraction on Redis objects to keep things organized.
UPGRADING FROM v1
An important message about upgrading from 1.x
redis-rb vs redis-namespace vs redis-classy
With the vanilla redis
gem, you've been doing this:
redis = Redis.new
redis.set 'foo', 'bar'
redis.get 'foo'
With the redis-namespace
gem, you can add a prefix in the following manner:
redis_ns = Redis::Namespace.new('ns', :redis => redis)
redis_ns['foo'] = 'bar'
redis_ns['foo']
Now, with the redis-classy
gem, you finally achieve a class-based naming convention:
class Something < RedisClassy
end
Something.on('foo').set('bar')
Something.on('foo').get
something = Something.new('foo')
something.set 'bar'
something.get
Usage
In Gemfile:
gem 'redis-classy'
Register the Redis server: (e.g. in config/initializers/redis_classy.rb
for Rails)
RedisClassy.redis = Redis.current
Create a class that inherits RedisClassy. (e.g. in app/redis/cache.rb
for Rails, for auto- and eager-loading)
class Cache < RedisClassy
def put(content)
pipelined do
set content
expire 5.seconds
end
end
end
cache = Cache.new(123)
cache.put "This tape will self-destruct in five seconds. Good luck."
Since the on
method is added as a syntactic sugar for new
, you can also run a command in one shot as well:
Cache.on(123).persist
For convenience, singleton and predefined static keys are also supported.
class Counter < RedisClassy
singleton
end
Counter.incr
Counter.incr
Counter.get
class Stats < RedisClassy
singletons :median, :average
end
ages = [21,22,24,28,30]
Stats.median.set ages[ages.size/2]
Stats.average.set ages.inject(:+)/ages.size
Stats.median.get
Stats.average.get
Finally, you can also pass an arbitrary object that responds to id
as a key. This is useful when used in combination with ActiveRecord, etc.
class Lock < RedisClassy
end
class Room < ActiveRecord::Base
end
room = Room.create
lock = Lock.new(room)
When you need an access to the non-namespaced, raw Redis keys, it's available as RedisClass.keys
. Keep in mind that this method is very slow at O(N) computational complexity and potentially hazardous when you have many keys. Read the details.
RedisClassy.keys
=> ["Stats:median", "Stats:average", "Counter"]
RedisClassy.keys 'Stats:*'
=> ["Stats:median", "Stats:average"]
Since the redis
attribute is a class instance variable, you can dynamically assign different databases for each class, without affecting other classes.
Cache.redis = Redis::Namespace.new('Cache', redis: Redis.new(host: 'another.host'))
Unicorn support
If you run fork-based app servers such as Unicorn or Passenger, you need to reconnect to the Redis after forking.
after_fork do
RedisClassy.redis.client.reconnect
end
Note that since Redis Classy assigns a namespaced Redis instance upon the inheritance event of each subclass (class Something < RedisClassy
), reconnecting the master (non-namespaced) connection that is referenced from all subclasses should probably be the safest and the most efficient way to survive a forking event.
Reference
Dependency:
Use case: