class Rack::MiniProfiler::MemoryStore
Constants
- CLEANUP_CYCLE
- CLEANUP_INTERVAL
- EXPIRES_IN_SECONDS
Public Class Methods
new(args = nil)
click to toggle source
# 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
Public Instance Methods
cleanup_cache()
click to toggle source
# 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)
click to toggle source
# 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={})
click to toggle source
FIXME: use weak ref, trouble it may be broken in 1.9 so need to use the 'ref' gem
# 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_locks()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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