Class: Coverband::Adapters::HashRedisStore::GetCoverageRedisCacheStore

Inherits:
Object
  • Object
show all
Defined in:
lib/coverband/adapters/hash_redis_store.rb

Constant Summary collapse

LOCK_LIMIT =

30 minutes

60 * 30

Instance Method Summary collapse

Constructor Details

#initialize(redis, key_prefix) ⇒ GetCoverageRedisCacheStore

Returns a new instance of GetCoverageRedisCacheStore.



20
21
22
23
# File 'lib/coverband/adapters/hash_redis_store.rb', line 20

def initialize(redis, key_prefix)
  @redis = redis
  @key_prefix = [key_prefix, "get-coverage"].join(".")
end

Instance Method Details

#clear!(local_types = Coverband::TYPES) ⇒ Object



47
48
49
50
51
52
# File 'lib/coverband/adapters/hash_redis_store.rb', line 47

def clear!(local_types = Coverband::TYPES)
  Array(local_types).each do |local_type|
    del(local_type)
    unlock!(local_type)
  end
end

#fetch(local_type) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/coverband/adapters/hash_redis_store.rb', line 25

def fetch(local_type)
  cached_result = get(local_type)

  # if no cache available, block the call and populate the cache
  # if cache is available, return it and start re-populating it (with a lock)
  if cached_result.nil?
    value = yield(0)
    result = set(local_type, JSON.generate(value))
    value
  else
    if lock!(local_type)
      Thread.new do
        result = yield(deferred_time)
        set(local_type, JSON.generate(result))
      ensure
        unlock!(local_type)
      end
    end
    JSON.parse(cached_result)
  end
end