Class: NebulousStomp::RedisHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/nebulous_stomp/redis_helper.rb

Overview

A class to help out users who want to talk to Redis themselves; the “Redis use case”.

redis = NebulousStomp::RedisHelper.new

redis.set(:thing, "thingy")
redes.set(:gone_in_30_seconds, "thingy", 30)

value = redis.get(:thing)

redis.del(:thing)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRedisHelper

:call-seq: RedisHelper.new -> (helper)



30
31
32
33
34
35
36
# File 'lib/nebulous_stomp/redis_helper.rb', line 30

def initialize
  @param_hash = Param.get(:redisConnectHash)

  fail NebulousError, "NebulousStomp.init has not been called or Redis not configured" \
    if @param_hash.nil? || @param_hash.empty?

end

Instance Attribute Details

#redis_handler=(value) ⇒ Object

For testing only



24
25
26
# File 'lib/nebulous_stomp/redis_helper.rb', line 24

def redis_handler=(value)
  @redis_handler = value
end

Instance Method Details

#del(key) ⇒ Object

:call-seq: redis.del(key)

Remove a value from the store. Raise an exception if there is none.



77
78
79
80
81
# File 'lib/nebulous_stomp/redis_helper.rb', line 77

def del(key)
  ensure_connected
  num = redis_handler.del(key.to_s)
  fail ArgumentError, "Unknown key, cannot delete" if num == 0
end

#get(key) ⇒ Object

:call-seq: redis.get(key) -> value

Get a string value from the store. Return nil if there is none.



66
67
68
69
# File 'lib/nebulous_stomp/redis_helper.rb', line 66

def get(key)
  ensure_connected
  json_to_value(redis_handler.get key.to_s)
end

#set(key, value, timeout = nil) ⇒ Object

:call-seq: redis.set(key, value) redis.set(key, value, timeout)

Set a value in the store.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nebulous_stomp/redis_helper.rb', line 45

def set(key, value, timeout=nil)
  rtimeout = (Integer(timeout.to_s, 10) rescue nil)
  rvalue   = value_to_json(value)
  fail ArgumentError, "Timeout must be a number" if timeout && rtimeout.nil?
  ensure_connected

  if timeout
    redis_handler.set(key.to_s, rvalue, ex: rtimeout)
  else
    redis_handler.set(key.to_s, rvalue)
  end

  self
end