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.



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

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

Instance Method Details

#cleanupObject



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

def cleanup
  @store.cleanup_cache
  @cycle_count = 1
end

#cycle_countObject



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

def cycle_count
  @cycle_count
end

#increment_cycleObject



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

def increment_cycle
  @cycle_count += 1
end

#should_cleanup?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/mini_profiler/storage/memory_store.rb', line 18

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.



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

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