Class: Gts::RedisStorage

Inherits:
Storage show all
Defined in:
lib/gts/storages/redis_storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/gts/storages/redis_storage.rb', line 7

def host
  @host
end

#list_idObject (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

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/gts/storages/redis_storage.rb', line 7

def port
  @port
end

#redisObject (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

#dumpObject

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

#infoObject



40
41
42
# File 'lib/gts/storages/redis_storage.rb', line 40

def info
  redis.info.map{ |k,v| "#{k}: #{v}" }.join("\n")
end

#sizeObject

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