Class: Profiler::Storage::MemoryStore
- Defined in:
- lib/profiler/storage/memory_store.rb
Instance Method Summary collapse
- #cleanup(older_than: 24 * 60 * 60) ⇒ Object
- #clear(type: nil) ⇒ Object
- #delete(token) ⇒ Object
- #find_by_parent(parent_token) ⇒ Object
-
#initialize(options = {}) ⇒ MemoryStore
constructor
A new instance of MemoryStore.
- #list(limit: 50, offset: 0) ⇒ Object
- #load(token) ⇒ Object
- #save(token, profile) ⇒ Object
Methods inherited from BaseStore
Constructor Details
#initialize(options = {}) ⇒ MemoryStore
Returns a new instance of MemoryStore.
10 11 12 13 |
# File 'lib/profiler/storage/memory_store.rb', line 10 def initialize( = {}) @profiles = Concurrent::Hash.new @max_profiles = [:max_profiles] || Profiler.configuration.max_profiles || 100 end |
Instance Method Details
#cleanup(older_than: 24 * 60 * 60) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/profiler/storage/memory_store.rb', line 37 def cleanup(older_than: 24 * 60 * 60) cutoff_time = Time.now - older_than @profiles.delete_if do |_token, data| profile = deserialize_profile(data) profile.started_at < cutoff_time end end |
#clear(type: nil) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/profiler/storage/memory_store.rb', line 56 def clear(type: nil) if type.nil? @profiles.clear else @profiles.delete_if do |_token, data| deserialize_profile(data).profile_type == type.to_s end end end |
#delete(token) ⇒ Object
52 53 54 |
# File 'lib/profiler/storage/memory_store.rb', line 52 def delete(token) @profiles.delete(token) end |
#find_by_parent(parent_token) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/profiler/storage/memory_store.rb', line 45 def find_by_parent(parent_token) @profiles.values .map { |data| deserialize_profile(data) } .select { |profile| profile.parent_token == parent_token } .sort_by { |profile| profile.started_at } end |
#list(limit: 50, offset: 0) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/profiler/storage/memory_store.rb', line 28 def list(limit: 50, offset: 0) @profiles.values .map { |data| deserialize_profile(data) } .sort_by { |p| p.started_at } .reverse .drop(offset) .take(limit) end |
#load(token) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/profiler/storage/memory_store.rb', line 21 def load(token) data = @profiles[token] return nil unless data deserialize_profile(data) end |
#save(token, profile) ⇒ Object
15 16 17 18 19 |
# File 'lib/profiler/storage/memory_store.rb', line 15 def save(token, profile) cleanup_if_needed @profiles[token] = serialize_profile(profile) token end |