Class: DiskStore::Reaper

Inherits:
Object
  • Object
show all
Defined in:
lib/disk_store/reaper.rb,
lib/disk_store/eviction_strategies/lru.rb

Defined Under Namespace

Modules: LRU

Constant Summary collapse

DEFAULT_OPTS =
{
  cache_size: 1073741824, # 1 gigabyte
  reaper_interval: 10, # seconds,
  eviction_strategy: nil
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, opts = {}) ⇒ Reaper

Returns a new instance of Reaper.



32
33
34
35
36
37
38
# File 'lib/disk_store/reaper.rb', line 32

def initialize(path, opts = {})
  @path = path
  @options = DEFAULT_OPTS.merge(opts)
  @thread = nil

  set_eviction_strategy(@options[:eviction_strategy])
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



30
31
32
# File 'lib/disk_store/reaper.rb', line 30

def path
  @path
end

#threadObject (readonly)

Returns the value of attribute thread.



30
31
32
# File 'lib/disk_store/reaper.rb', line 30

def thread
  @thread
end

Class Method Details

.kill_all!Object

Mostly useful for testing purposes



25
26
27
28
# File 'lib/disk_store/reaper.rb', line 25

def self.kill_all!
  @reapers.each { |path, reaper| reaper.thread.kill }
  @reapers = {}
end

.spawn_for(path, opts = {}) ⇒ Object

Spawn exactly 1 reaper for each cache path



14
15
16
17
18
19
20
21
22
# File 'lib/disk_store/reaper.rb', line 14

def self.spawn_for(path, opts = {})
  return @reapers[path] if @reapers.has_key?(path)

  reaper = Reaper.new(path, opts)
  reaper.spawn!

  @reapers[path] = reaper
  reaper
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/disk_store/reaper.rb', line 54

def alive?
  @thread && @thread.alive?
end

#running?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/disk_store/reaper.rb', line 58

def running?
  @thread && !@thread.stop?
end

#set_eviction_strategy(strategy) ⇒ Object



40
41
42
43
# File 'lib/disk_store/reaper.rb', line 40

def set_eviction_strategy(strategy)
  return if strategy.nil?
  self.class.send :prepend, DiskStore::Reaper.const_get(strategy)
end

#spawn!Object



45
46
47
48
49
50
51
52
# File 'lib/disk_store/reaper.rb', line 45

def spawn!
  @thread = Thread.new do
    loop do
      perform_sweep! if needs_eviction?
      wait_for_next
    end
  end
end