Class: Rack::MiniProfiler::MemoryStore

Inherits:
AbstractStore show all
Defined in:
lib/mini_profiler/storage/memory_store.rb

Defined Under Namespace

Classes: CacheCleanupThread

Constant Summary collapse

EXPIRES_IN_SECONDS =
60 * 60 * 24
CLEANUP_INTERVAL =
10
CLEANUP_CYCLE =
3600

Constants inherited from AbstractStore

AbstractStore::MAX_TOKEN_AGE

Instance Method Summary collapse

Methods inherited from AbstractStore

#diagnostics

Constructor Details

#initialize(args = nil) ⇒ MemoryStore

Returns a new instance of MemoryStore.


50
51
52
53
54
55
56
57
58
# File 'lib/mini_profiler/storage/memory_store.rb', line 50

def initialize(args = nil)
  args ||= {}
  @expires_in_seconds = args.fetch(:expires_in) { EXPIRES_IN_SECONDS }

  @token1, @token2, @cycle_at = nil

  initialize_locks
  initialize_cleanup_thread(args)
end

Instance Method Details

#allowed_tokensObject


125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mini_profiler/storage/memory_store.rb', line 125

def allowed_tokens
  @token_lock.synchronize do

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

    [@token1, @token2].compact

  end
end

#cleanup_cacheObject


118
119
120
121
122
123
# File 'lib/mini_profiler/storage/memory_store.rb', line 118

def cleanup_cache
  expire_older_than = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - @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) ⇒ Object


112
113
114
115
116
# File 'lib/mini_profiler/storage/memory_store.rb', line 112

def get_unviewed_ids(user)
  @user_view_lock.synchronize {
    @user_view_cache[user]
  }
end

#initialize_cleanup_thread(args = {}) ⇒ Object

FIXME: use weak ref, trouble it may be broken in 1.9 so need to use the 'ref' gem


69
70
71
72
73
74
75
76
77
78
# File 'lib/mini_profiler/storage/memory_store.rb', line 69

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
    until Thread.current[:should_exit] do
      t.sleepy_run
    end
  end
  at_exit { t[:should_exit] = true }
end

#initialize_locksObject


60
61
62
63
64
65
66
# File 'lib/mini_profiler/storage/memory_store.rb', line 60

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) ⇒ Object


86
87
88
89
90
# File 'lib/mini_profiler/storage/memory_store.rb', line 86

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

#save(page_struct) ⇒ Object


80
81
82
83
84
# File 'lib/mini_profiler/storage/memory_store.rb', line 80

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

#set_all_unviewed(user, ids) ⇒ Object


106
107
108
109
110
# File 'lib/mini_profiler/storage/memory_store.rb', line 106

def set_all_unviewed(user, ids)
  @user_view_lock.synchronize {
    @user_view_cache[user] = ids
  }
end

#set_unviewed(user, id) ⇒ Object


92
93
94
95
96
97
# File 'lib/mini_profiler/storage/memory_store.rb', line 92

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

#set_viewed(user, id) ⇒ Object


99
100
101
102
103
104
# File 'lib/mini_profiler/storage/memory_store.rb', line 99

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