Class: Profiler::Storage::RedisStore
- Defined in:
- lib/profiler/storage/redis_store.rb
Constant Summary collapse
- DEFAULT_TTL =
24 hours
24 * 60 * 60
Instance Attribute Summary collapse
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
- #cleanup(older_than: 24 * 60 * 60) ⇒ Object
- #clear(type: nil) ⇒ Object
- #delete(token) ⇒ Object
- #do_save(token, profile) ⇒ Object
- #find_by_parent(parent_token) ⇒ Object
-
#initialize(options = {}) ⇒ RedisStore
constructor
A new instance of RedisStore.
- #list(limit: 50, offset: 0) ⇒ Object
- #load(token) ⇒ Object
Methods inherited from BaseStore
Constructor Details
#initialize(options = {}) ⇒ RedisStore
Returns a new instance of RedisStore.
15 16 17 18 19 |
# File 'lib/profiler/storage/redis_store.rb', line 15 def initialize( = {}) @redis = [:redis] || build_redis_client() @ttl = [:ttl] || DEFAULT_TTL @key_prefix = [:key_prefix] || "profiler" end |
Instance Attribute Details
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
13 14 15 |
# File 'lib/profiler/storage/redis_store.rb', line 13 def redis @redis end |
Instance Method Details
#cleanup(older_than: 24 * 60 * 60) ⇒ Object
47 48 49 50 |
# File 'lib/profiler/storage/redis_store.rb', line 47 def cleanup(older_than: 24 * 60 * 60) cutoff_time = Time.now.to_f - older_than @redis.zremrangebyscore(list_key, "-inf", cutoff_time) end |
#clear(type: nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/profiler/storage/redis_store.rb', line 68 def clear(type: nil) tokens = @redis.zrange(list_key, 0, -1) tokens.each do |token| if type.nil? @redis.del(profile_key(token)) @redis.zrem(list_key, token) else profile = load(token) if profile&.profile_type == type.to_s @redis.del(profile_key(token)) @redis.zrem(list_key, token) end end end end |
#delete(token) ⇒ Object
63 64 65 66 |
# File 'lib/profiler/storage/redis_store.rb', line 63 def delete(token) @redis.del(profile_key(token)) @redis.zrem(list_key, token) end |
#do_save(token, profile) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/profiler/storage/redis_store.rb', line 21 def do_save(token, profile) key = profile_key(token) @redis.setex(key, @ttl, profile.to_json) # Add to sorted set for listing @redis.zadd(list_key, profile.started_at.to_f, token) token end |
#find_by_parent(parent_token) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/profiler/storage/redis_store.rb', line 52 def find_by_parent(parent_token) # Get all tokens from the sorted set tokens = @redis.zrange(list_key, 0, -1) # Load each profile and filter by parent_token tokens.map { |token| load(token) } .compact .select { |profile| profile.parent_token == parent_token } .sort_by { |profile| profile.started_at } end |
#list(limit: 50, offset: 0) ⇒ Object
42 43 44 45 |
# File 'lib/profiler/storage/redis_store.rb', line 42 def list(limit: 50, offset: 0) tokens = @redis.zrevrange(list_key, offset, offset + limit - 1) tokens.map { |token| load(token) }.compact end |
#load(token) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/profiler/storage/redis_store.rb', line 31 def load(token) key = profile_key(token) json_data = @redis.get(key) return nil unless json_data Models::Profile.from_json(json_data) rescue => e warn "Failed to load profile #{token}: #{e.}" nil end |