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 }

  @token1, @token2, @cycle_tokens_at = nil

  initialize_locks
  initialize_cleanup_thread(args)
end

Public Instance Methods

allowed_tokens() click to toggle source
# File lib/mini_profiler/storage/memory_store.rb, line 124
def allowed_tokens
  @token_lock.synchronize do

    unless @cycle_at && (@cycle_at > Time.now)
      @token2 = @token1
      @token1 = SecureRandom.hex
      @cycle_at = Time.now + Rack::MiniProfiler::AbstractStore::MAX_TOKEN_AGE
    end

    [@token1, @token2].compact

  end
end
cleanup_cache() click to toggle source
# File lib/mini_profiler/storage/memory_store.rb, line 117
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 111
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 68
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 59
def initialize_locks
  @token_lock = Mutex.new
  @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 85
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 79
def save(page_struct)
  @timer_struct_lock.synchronize {
    @timer_struct_cache[page_struct[:id]] = page_struct
  }
end
set_all_unviewed(user, ids) click to toggle source
# File lib/mini_profiler/storage/memory_store.rb, line 105
def set_all_unviewed(user, ids)
  @user_view_lock.synchronize {
    @user_view_cache[user] = ids
  }
end
set_unviewed(user, id) click to toggle source
# File lib/mini_profiler/storage/memory_store.rb, line 91
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 98
def set_viewed(user, id)
  @user_view_lock.synchronize {
    @user_view_cache[user] ||= []
    @user_view_cache[user].delete(id)
  }
end