Sha256: f8516db6a00a60ad53f7242dfd2108a0139f4067e2dcadeea8dffa3d8f2c6728

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 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['Root']['StartMilliseconds'] < expire_older_than }
        }
      end
    
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rack-mini-profiler-0.1.12.pre lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.11.pre lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.10 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.9 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.8 lib/mini_profiler/storage/memory_store.rb