Module: Redis::Objects

Defined in:
lib/redis/objects.rb,
lib/redis/objects/sets.rb,
lib/redis/objects/lists.rb,
lib/redis/objects/locks.rb,
lib/redis/objects/values.rb,
lib/redis/objects/counters.rb,
lib/redis/objects/sorted_sets.rb

Overview

Redis::Objects enables high-performance atomic operations in your app by leveraging the atomic features of the Redis server. To use Redis::Objects, first include it in any class you want. (This example uses an ActiveRecord subclass, but that is not required.) Then, use counter, lock, set, etc to define your primitives:

class Game < ActiveRecord::Base
  include Redis::Objects

  counter :joined_players
  counter :active_players, :key => 'game:#{id}:act_plyr'
  lock :archive_game
  set :player_ids
end

The, you can use these counters both for bookeeping and as atomic actions:

@game = Game.find(id)
@game_user = @game.joined_players.increment do |val|
  break if val > @game.max_players
  gu = @game.game_users.create!(:user_id => @user.id)
  @game.active_players.increment
  gu
end
if @game_user.nil?
  # game is full - error screen
else
  # success
end

Defined Under Namespace

Modules: ClassMethods, Counters, InstanceMethods, Lists, Locks, Sets, SortedSets, Values Classes: MissingID, NotConnected, UndefinedCounter, UndefinedLock

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/redis/objects.rb', line 56

def included(klass)
  # Core (this file)
  klass.instance_variable_set('@redis', @redis)
  klass.instance_variable_set('@redis_objects', {})
  klass.send :include, InstanceMethods
  klass.extend ClassMethods
  
  # Pull in each object type
  klass.send :include, Redis::Objects::Counters
  klass.send :include, Redis::Objects::Lists
  klass.send :include, Redis::Objects::Locks
  klass.send :include, Redis::Objects::Sets
  klass.send :include, Redis::Objects::SortedSets
  klass.send :include, Redis::Objects::Values
end

.redisObject



52
53
54
# File 'lib/redis/objects.rb', line 52

def redis
  @redis ||= $redis || raise(NotConnected, "Redis::Objects.redis not set to a Redis.new connection")
end

.redis=(conn) ⇒ Object



51
# File 'lib/redis/objects.rb', line 51

def redis=(conn) @redis = conn end