Sha256: 7ddf36fc71bdf8b713692310b47c36b5bc072c8f35e9aa3871aba472499d528a

Contents?: true

Size: 1.58 KB

Versions: 8

Compression:

Stored size: 1.58 KB

Contents

module Rack
  class MiniProfiler
    class MemoryStore < AbstractStore

      EXPIRE_TIMER_CACHE = 3600 * 24
     
      def initialize(args)
        @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
        Thread.new do
          while true do
            me.cleanup_cache
            sleep(3600)
          end
        end
      end

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

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

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

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

      def get_unviewed_ids(user)
        @user_view_lock.synchronize {
          @user_view_cache[user]
        }
      end
      
      def cleanup_cache
        expire_older_than = ((Time.now.to_f - MiniProfiler::MemoryStore::EXPIRE_TIMER_CACHE) * 1000).to_i
        @timer_struct_lock.synchronize {
          @timer_struct_cache.delete_if { |k, v| v['Started'] < expire_older_than }
        }
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rack-mini-profiler-0.1.23 Ruby/lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.22 Ruby/lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.21 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.20 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.19 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.18 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.17 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.16 lib/mini_profiler/storage/memory_store.rb