Sha256: ddf7bfe45b877a06141d9e3fc50a46804a2979f230a9d25511012aaf94a4cbfc

Contents?: true

Size: 1.63 KB

Versions: 12

Compression:

Stored size: 1.63 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
      

      private 


      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

12 entries across 12 versions & 2 rubygems

Version Path
miniprofiler-0.1.7.4 lib/mini_profiler/storage/memory_store.rb
miniprofiler-0.1.7.3 lib/mini_profiler/storage/memory_store.rb
miniprofiler-0.1.7.2 lib/mini_profiler/storage/memory_store.rb
miniprofiler-0.1.7.1 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.7 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.6 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.5 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.4 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.3 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.2 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1.1 lib/mini_profiler/storage/memory_store.rb
rack-mini-profiler-0.1 lib/mini_profiler/storage/memory_store.rb