lib/sgs/redis_base.rb in sgslib-0.2.5 vs lib/sgs/redis_base.rb in sgslib-0.2.6

- old
+ new

@@ -30,22 +30,23 @@ # require 'redis' module SGS class RedisBase - ## + class << self + def redis + puts "Class init" + @@redis ||= Redis.new + end + end + + # # The base (inherited) class for dealing with Redis data for # the navigation system. Each model class inherits this parent, # and gets an update count for free. # - # Initialize the base class. - def initialize - $redis = Redis.new unless $redis - end - - # # Initialize the (sub-)class variables in Redis. def self.setup cls = new cls.instance_variables.each do |var| val = cls.instance_variable_get var @@ -65,11 +66,11 @@ # # Initialize a Redis variable. def self.var_init(var, val, idx = nil) cls = new - $redis.setnx cls.make_redis_name(var, :idx => idx), self.to_redis(var, val, idx) + SGS::RedisBase.redis.setnx cls.make_redis_name(var, :idx => idx), self.to_redis(var, val, idx) end # # Load the instance variables for the class. def self.load() @@ -131,15 +132,15 @@ end end # # Inside a multi-block, set all the variables and increment # the count. - $redis.multi do + SGS::RedisBase.redis.multi do var_list.each do |key, value| - $redis.set key, value + SGS::RedisBase.redis.set key, value end - $redis.incr count_name + SGS::RedisBase.redis.incr count_name end true end # @@ -147,11 +148,11 @@ # to a channel is that whenever there's a publish, the new count is # published as a string. If you subscribe to the channel (usually the # class name), you can remember the last received count and decide if # there is fresh data. Or, you can just act anyway. def publish - $redis.publish self.class.redis_handle, count.to_s + SGS::RedisBase.redis.publish self.class.redis_handle, count.to_s end # # Subscribe to messages from this particular channel. Each count is sent # to the code block. It's up to the called code block to decide if the @@ -172,11 +173,11 @@ end # # Retrieve the count def count - $redis.get count_name + SGS::RedisBase.redis.get count_name end # # What is the official name of the count instance variable def count_name @@ -185,10 +186,10 @@ # # Get an instance variable value from a Redis value. def redis_read_var(var, klass, opts = {}) redis_name = make_redis_name var, opts - redis_val = $redis.get redis_name + redis_val = SGS::RedisBase.redis.get redis_name redis_val = nil if redis_val == "" if redis_val if not klass or klass == NilClass redis_val = true if redis_val == "true" redis_val = false if redis_val == "false"