Class: Rack::MiniProfiler::MemoryStore::CacheCleanupThread

Inherits:
Thread
  • Object
show all
Defined in:
lib/mini_profiler/storage/memory_store.rb

Overview

Sub-class thread so we have a named thread (useful for debugging in Thread.list).

Instance Method Summary collapse

Constructor Details

#initialize(interval, cycle, store) ⇒ CacheCleanupThread

Returns a new instance of CacheCleanupThread.



12
13
14
15
16
17
18
# File 'lib/mini_profiler/storage/memory_store.rb', line 12

def initialize(interval, cycle, store)
  @store       = store
  @interval    = interval
  @cycle       = cycle
  @cycle_count = 1
  super
end

Instance Method Details

#cleanupObject



34
35
36
37
# File 'lib/mini_profiler/storage/memory_store.rb', line 34

def cleanup
  @store.cleanup_cache
  @cycle_count = 1
end

#cycle_countObject



39
40
41
# File 'lib/mini_profiler/storage/memory_store.rb', line 39

def cycle_count
  @cycle_count
end

#increment_cycleObject



43
44
45
# File 'lib/mini_profiler/storage/memory_store.rb', line 43

def increment_cycle
  @cycle_count += 1
end

#should_cleanup?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/mini_profiler/storage/memory_store.rb', line 20

def should_cleanup?
  @cycle_count * @interval >= @cycle
end

#sleepy_runObject

We don’t want to hit the filesystem every 10s to clean up the cache so we need to do a bit of accounting to avoid sleeping that entire time. We don’t want to sleep for the entire period because it means the thread will stay live in hot deployment scenarios, keeping a potentially large memory graph from being garbage collected upon undeploy.



28
29
30
31
32
# File 'lib/mini_profiler/storage/memory_store.rb', line 28

def sleepy_run
  cleanup if should_cleanup?
  sleep(@interval)
  increment_cycle
end