Class: Resque::Data::RedisManager

Inherits:
Object
  • Object
show all
Defined in:
lib/resque-data/manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(default_redis = nil, multi_namespace = true, multi_redis = true, redi = []) ⇒ RedisManager

Returns a new instance of RedisManager.



3
4
5
6
7
8
9
10
11
12
# File 'lib/resque-data/manager.rb', line 3

def initialize(default_redis = nil, multi_namespace = true, multi_redis = true, redi = [])
  @default_redis = default_redis || 'redis://localhost:6379'

  @multi_namespace, @multi_redis = multi_namespace, multi_redis

  @redi = Hash[redi.map {|r| [r, new_redis(r)]}]
  @redi[@default_redis] = new_redis(@default_redis) if @redi[@default_redis].nil?

  @timestamps = Hash.new
end

Instance Method Details

#for(host = @default_redis) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/resque-data/manager.rb', line 14

def for(host = @default_redis)
  return @redi[@default_redis] unless @multi_redis

  (@redi[host] ||= new_redis(host)).tap do
    @timestamps[host] = Time.now
  end
end

#needs_pruning?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/resque-data/manager.rb', line 28

def needs_pruning?
  @redi.length >= 20
end

#new_redis(host) ⇒ Object



22
23
24
25
26
# File 'lib/resque-data/manager.rb', line 22

def new_redis(host)
  prune if needs_pruning?

  Resque::Data::Fetcher.new(redis: host, multi_namespace: @multi_namespace)
end

#pruneObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/resque-data/manager.rb', line 32

def prune
  sorted = @timestamps.sort_by {|host, time| time}
  sorted.pop if sorted.first.first == @default_redis

  oldest = sorted.first.first
  puts "deleting #{oldest}"

  @timestamps.delete(oldest)
  @redi.delete(oldest)
end

#to_jsonObject



43
44
45
# File 'lib/resque-data/manager.rb', line 43

def to_json
  @redi.to_json
end