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
CLEANUP_INTERVAL =
10
CLEANUP_CYCLE =
3600

Instance Method Summary collapse

Methods inherited from AbstractStore

#diagnostics

Constructor Details

#initialize(args = nil) ⇒ MemoryStore

Returns a new instance of MemoryStore.



49
50
51
52
53
54
# File 'lib/mini_profiler/storage/memory_store.rb', line 49

def initialize(args = nil)
  args ||= {}
  @expires_in_seconds = args.fetch(:expires_in) { EXPIRES_IN_SECONDS }
  initialize_locks
  initialize_cleanup_thread(args)
end

Instance Method Details

#cleanup_cacheObject



107
108
109
110
111
112
# File 'lib/mini_profiler/storage/memory_store.rb', line 107

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



101
102
103
104
105
# File 'lib/mini_profiler/storage/memory_store.rb', line 101

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

#initialize_cleanup_thread(args = {}) ⇒ Object

FIXME: use weak ref, trouble it may be broken in 1.9 so need to use the ‘ref’ gem



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

def initialize_cleanup_thread(args={})
  cleanup_interval = args.fetch(:cleanup_interval) { CLEANUP_INTERVAL }
  cleanup_cycle    = args.fetch(:cleanup_cycle)    { CLEANUP_CYCLE }
  t = CacheCleanupThread.new(cleanup_interval, cleanup_cycle, self) do |t|
    until Thread.current[:should_exit] do
      self.sleepy_run
    end
  end
  at_exit { t[:should_exit] = true }
end

#initialize_locksObject



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

def initialize_locks
  @timer_struct_lock  = Mutex.new
  @user_view_lock     = Mutex.new
  @timer_struct_cache = {}
  @user_view_cache    = {}
end

#load(id) ⇒ Object



81
82
83
84
85
# File 'lib/mini_profiler/storage/memory_store.rb', line 81

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

#save(page_struct) ⇒ Object



75
76
77
78
79
# File 'lib/mini_profiler/storage/memory_store.rb', line 75

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

#set_unviewed(user, id) ⇒ Object



87
88
89
90
91
92
# File 'lib/mini_profiler/storage/memory_store.rb', line 87

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

#set_viewed(user, id) ⇒ Object



94
95
96
97
98
99
# File 'lib/mini_profiler/storage/memory_store.rb', line 94

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