Class: Rack::MiniProfiler::MemoryStore
Defined Under Namespace
Classes: CacheCleanupThread
Constant Summary
collapse
- EXPIRES_IN_SECONDS =
60 * 60 * 24
- CLEANUP_INTERVAL =
10
- CLEANUP_CYCLE =
3600
AbstractStore::MAX_TOKEN_AGE
Instance Method Summary
collapse
#diagnostics
Constructor Details
#initialize(args = nil) ⇒ MemoryStore
Returns a new instance of MemoryStore.
49
50
51
52
53
54
55
56
57
|
# File 'lib/mini_profiler/storage/memory_store.rb', line 49
def initialize(args = nil)
args ||= {}
@expires_in_seconds = args.fetch(:expires_in) { EXPIRES_IN_SECONDS }
@token1, @token2, @cycle_tokens_at = nil
initialize_locks
initialize_cleanup_thread(args)
end
|
Instance Method Details
#allowed_tokens ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/mini_profiler/storage/memory_store.rb', line 124
def allowed_tokens
@token_lock.synchronize do
unless @cycle_at && (@cycle_at > Time.now)
@token2 = @token1
@token1 = SecureRandom.hex
@cycle_at = Time.now + Rack::MiniProfiler::AbstractStore::MAX_TOKEN_AGE
end
[@token1, @token2].compact
end
end
|
#cleanup_cache ⇒ Object
117
118
119
120
121
122
|
# File 'lib/mini_profiler/storage/memory_store.rb', line 117
def cleanup_cache
expire_older_than = ((Time.now.to_f - @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
111
112
113
114
115
|
# File 'lib/mini_profiler/storage/memory_store.rb', line 111
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
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/mini_profiler/storage/memory_store.rb', line 68
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 |t|
until Thread.current[:should_exit] do
self.sleepy_run
end
end
at_exit { t[:should_exit] = true }
end
|
#initialize_locks ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/mini_profiler/storage/memory_store.rb', line 59
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
85
86
87
88
89
|
# File 'lib/mini_profiler/storage/memory_store.rb', line 85
def load(id)
@timer_struct_lock.synchronize {
@timer_struct_cache[id]
}
end
|
#save(page_struct) ⇒ Object
79
80
81
82
83
|
# File 'lib/mini_profiler/storage/memory_store.rb', line 79
def save(page_struct)
@timer_struct_lock.synchronize {
@timer_struct_cache[page_struct[:id]] = page_struct
}
end
|
#set_all_unviewed(user, ids) ⇒ Object
105
106
107
108
109
|
# File 'lib/mini_profiler/storage/memory_store.rb', line 105
def set_all_unviewed(user, ids)
@user_view_lock.synchronize {
@user_view_cache[user] = ids
}
end
|
#set_unviewed(user, id) ⇒ Object
91
92
93
94
95
96
|
# File 'lib/mini_profiler/storage/memory_store.rb', line 91
def set_unviewed(user, id)
@user_view_lock.synchronize {
@user_view_cache[user] ||= []
@user_view_cache[user] << id
}
end
|
#set_viewed(user, id) ⇒ Object
98
99
100
101
102
103
|
# File 'lib/mini_profiler/storage/memory_store.rb', line 98
def set_viewed(user, id)
@user_view_lock.synchronize {
@user_view_cache[user] ||= []
@user_view_cache[user].delete(id)
}
end
|