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/hashes.rb,
lib/redis/objects/values.rb,
lib/redis/objects/version.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, Hashes, InstanceMethods, Lists, Locks, Sets, SortedSets, Values Classes: MissingID, NilObjectId, NotConnected, UndefinedCounter, UndefinedLock

Constant Summary

VERSION =
"0.9.1"

Class Method Summary (collapse)

Class Method Details

+ (Object) included(klass)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/redis/objects.rb', line 70

def included(klass)
  # Core (this file)
  klass.instance_variable_set('@redis', nil)
  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
  klass.send :include, Redis::Objects::Hashes
end

+ (Object) redis



65
66
67
68
# File 'lib/redis/objects.rb', line 65

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

+ (Object) redis=(conn)



62
63
64
# File 'lib/redis/objects.rb', line 62

def redis=(conn)
  @redis = conn
end