Class: Gts::RedisStorage
- Defined in:
- lib/gts/storages/redis_storage.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#list_id ⇒ Object
readonly
Returns the value of attribute list_id.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
-
#append(value) ⇒ Object
append new item to the end of the list.
-
#dump ⇒ Object
get all the elements in the list and empty it.
- #info ⇒ Object
-
#initialize(opts) ⇒ RedisStorage
constructor
A new instance of RedisStorage.
-
#size ⇒ Object
get the count of the elements in the list.
Constructor Details
#initialize(opts) ⇒ RedisStorage
Returns a new instance of RedisStorage.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/gts/storages/redis_storage.rb', line 9 def initialize(opts) @host = opts[:host] || "0.0.0.0" @port = opts[:port] || "6379" @list_id = opts[:list_id] || "gts_data" @redis = Redis.new(:host => @host, :port => @port) begin @redis.ping rescue Redis::CannotConnectError puts "Failed to start. Redis is not running or wrong host/port provided." exit end end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
7 8 9 |
# File 'lib/gts/storages/redis_storage.rb', line 7 def host @host end |
#list_id ⇒ Object (readonly)
Returns the value of attribute list_id.
7 8 9 |
# File 'lib/gts/storages/redis_storage.rb', line 7 def list_id @list_id end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
7 8 9 |
# File 'lib/gts/storages/redis_storage.rb', line 7 def port @port end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
7 8 9 |
# File 'lib/gts/storages/redis_storage.rb', line 7 def redis @redis end |
Instance Method Details
#append(value) ⇒ Object
append new item to the end of the list
23 24 25 |
# File 'lib/gts/storages/redis_storage.rb', line 23 def append(value) redis.rpush list_id, value end |
#dump ⇒ Object
get all the elements in the list and empty it
28 29 30 31 32 33 |
# File 'lib/gts/storages/redis_storage.rb', line 28 def dump current_size = size dumped_items = redis.lrange list_id, 0, current_size - 1 redis.ltrim list_id, current_size, size - 1 dumped_items end |
#info ⇒ Object
40 41 42 |
# File 'lib/gts/storages/redis_storage.rb', line 40 def info redis.info.map{ |k,v| "#{k}: #{v}" }.join("\n") end |
#size ⇒ Object
get the count of the elements in the list
36 37 38 |
# File 'lib/gts/storages/redis_storage.rb', line 36 def size redis.llen list_id end |