Class: Rack::MiniProfiler::MemoryStore

Inherits:
AbstractStore show all
Defined in:
lib/mini_profiler/storage/memory_store.rb

Defined Under Namespace

Classes: CacheCleanupThread

Constant Summary collapse

EXPIRES_IN_SECONDS =
60 * 60 * 24

Instance Method Summary collapse

Methods inherited from AbstractStore

#diagnostics

Constructor Details

#initialize(args = nil) ⇒ MemoryStore

Returns a new instance of MemoryStore.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mini_profiler/storage/memory_store.rb', line 11

def initialize(args = nil)
  args ||= {}
  @expires_in_seconds = args[:expires_in] || EXPIRES_IN_SECONDS
  @timer_struct_lock = Mutex.new
  @timer_struct_cache = {}
  @user_view_lock = Mutex.new
  @user_view_cache = {}

  # TODO: fix it to use weak ref, trouble is may be broken in 1.9 so need to use the 'ref' gem
  me = self
  t = CacheCleanupThread.new do
    interval = 10
    cleanup_cache_cycle = 3600
    cycle_count = 1

    until Thread.current[:should_exit] do
      # 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.
      if cycle_count * interval >= cleanup_cache_cycle
        cycle_count = 1
        me.cleanup_cache
      end

      sleep(interval)
      cycle_count += 1
    end
  end

  at_exit { t[:should_exit] = true }

  t
end

Instance Method Details

#cleanup_cacheObject



78
79
80
81
82
83
# File 'lib/mini_profiler/storage/memory_store.rb', line 78

def cleanup_cache
  expire_older_than = ((Time.now.to_f - @expires_in_seconds) * 1000).to_i
  @timer_struct_lock.synchronize {
    @timer_struct_cache.delete_if { |k, v| v['Started'] < expire_older_than }
  }
end

#get_unviewed_ids(user) ⇒ Object



72
73
74
75
76
# File 'lib/mini_profiler/storage/memory_store.rb', line 72

def get_unviewed_ids(user)
  @user_view_lock.synchronize {
    @user_view_cache[user]
  }
end

#load(id) ⇒ Object



52
53
54
55
56
# File 'lib/mini_profiler/storage/memory_store.rb', line 52

def load(id)
  @timer_struct_lock.synchronize {
    @timer_struct_cache[id]
  }
end

#save(page_struct) ⇒ Object



46
47
48
49
50
# File 'lib/mini_profiler/storage/memory_store.rb', line 46

def save(page_struct)
  @timer_struct_lock.synchronize {
    @timer_struct_cache[page_struct['Id']] = page_struct
  }
end

#set_unviewed(user, id) ⇒ Object



58
59
60
61
62
63
# File 'lib/mini_profiler/storage/memory_store.rb', line 58

def set_unviewed(user, id)
  @user_view_lock.synchronize {
    @user_view_cache[user] ||= []
    @user_view_cache[user] << id
  }
end

#set_viewed(user, id) ⇒ Object



65
66
67
68
69
70
# File 'lib/mini_profiler/storage/memory_store.rb', line 65

def set_viewed(user, id)
  @user_view_lock.synchronize {
    @user_view_cache[user] ||= []
    @user_view_cache[user].delete(id)
  }
end