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.



8
9
10
11
12
13
14
# File 'lib/mini_profiler/storage/memory_store.rb', line 8

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

Instance Method Details

#cleanupObject



31
32
33
34
# File 'lib/mini_profiler/storage/memory_store.rb', line 31

def cleanup
  @store.cleanup_cache
  @cycle_count = 1
end

#cycle_countObject



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

def cycle_count
  @cycle_count
end

#increment_cycleObject



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

def increment_cycle
  @cycle_count += 1
end

#should_cleanup?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/mini_profiler/storage/memory_store.rb', line 16

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.



25
26
27
28
29
# File 'lib/mini_profiler/storage/memory_store.rb', line 25

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