class Rack::MiniProfiler::FileStore

Constants

EXPIRES_IN_SECONDS

Public Class Methods

new(args = nil) click to toggle source
# File Ruby/lib/mini_profiler/storage/file_store.rb, line 32
def initialize(args = nil)
  args ||= {}
  @path = args[:path]
  @expires_in_seconds = args[:expires_in] || EXPIRES_IN_SECONDS
  raise ArgumentError.new :path unless @path
  @timer_struct_cache = FileCache.new(@path, "mp_timers")
  @timer_struct_lock = Mutex.new
  @user_view_cache = FileCache.new(@path, "mp_views")
  @user_view_lock = Mutex.new

  me = self
  Thread.new do
    begin
      while true do
        # TODO: a sane retry count before bailing
        me.cleanup_cache
        sleep(3600)
      end
    rescue
      # don't crash the thread, we can clean up next time
    end
  end
end

Public Instance Methods

cleanup_cache() click to toggle source
# File Ruby/lib/mini_profiler/storage/file_store.rb, line 93
def cleanup_cache
  files = Dir.entries(@path)
  @timer_struct_lock.synchronize {
    files.each do |f|
      f = @path + '/' + f
      ::File.delete f if ::File.basename(f) =~ %r^mp_timers/ and (Time.now - ::File.mtime(f)) > @expires_in_seconds
    end
  }
  @user_view_lock.synchronize {
    files.each do |f|
      f = @path + '/' + f
      ::File.delete f if ::File.basename(f) =~ %r^mp_views/ and (Time.now - ::File.mtime(f)) > @expires_in_seconds
    end
  }
end
get_unviewed_ids(user) click to toggle source
# File Ruby/lib/mini_profiler/storage/file_store.rb, line 87
def get_unviewed_ids(user)
  @user_view_lock.synchronize {
    @user_view_cache[user]
  }
end
load(id) click to toggle source
# File Ruby/lib/mini_profiler/storage/file_store.rb, line 62
def load(id)
  @timer_struct_lock.synchronize {
    @timer_struct_cache[id]
  }
end
save(page_struct) click to toggle source
# File Ruby/lib/mini_profiler/storage/file_store.rb, line 56
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 Ruby/lib/mini_profiler/storage/file_store.rb, line 68
def set_unviewed(user, id)
  @user_view_lock.synchronize {
    current = @user_view_cache[user]
    current = [] unless Array === current
    current << id
    @user_view_cache[user] = current.uniq
  }
end
set_viewed(user, id) click to toggle source
# File Ruby/lib/mini_profiler/storage/file_store.rb, line 77
def set_viewed(user, id)
  @user_view_lock.synchronize {
    @user_view_cache[user] ||= []
    current = @user_view_cache[user]
    current = [] unless Array === current
    current.delete(id)
    @user_view_cache[user] = current.uniq
  }
end