Class: Rack::MiniProfiler::RedisStore
- Inherits:
-
AbstractStore
- Object
- AbstractStore
- Rack::MiniProfiler::RedisStore
- Defined in:
- lib/mini_profiler/storage/redis_store.rb
Constant Summary collapse
- EXPIRES_IN_SECONDS =
60 * 60 * 24
- COUNTER_LUA =
<<~LUA if redis.call("INCR", KEYS[1]) % ARGV[1] == 0 then redis.call("DEL", KEYS[1]) return 1 else return 0 end LUA
- COUNTER_LUA_SHA =
Digest::SHA1.hexdigest(COUNTER_LUA)
Constants inherited from AbstractStore
Instance Attribute Summary collapse
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #allowed_tokens ⇒ Object
- #diagnostics(user) ⇒ Object
- #fetch_snapshots(batch_size: 200, &blk) ⇒ Object
- #flush_tokens ⇒ Object
-
#get_unviewed_ids(user) ⇒ Object
Remove expired ids from the unviewed sorted set and return the remaining ids.
-
#initialize(args = nil) ⇒ RedisStore
constructor
A new instance of RedisStore.
- #load(id) ⇒ Object
- #load_snapshot(id) ⇒ Object
- #push_snapshot(page_struct, config) ⇒ Object
- #save(page_struct) ⇒ Object
- #set_all_unviewed(user, ids) ⇒ Object
- #set_unviewed(user, id) ⇒ Object
- #set_viewed(user, id) ⇒ Object
- #should_take_snapshot?(period) ⇒ Boolean
-
#simulate_expire ⇒ Object
Only used for testing.
Methods inherited from AbstractStore
#find_snapshots_group, #snapshot_groups_overview
Constructor Details
#initialize(args = nil) ⇒ RedisStore
Returns a new instance of RedisStore.
13 14 15 16 17 18 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 13 def initialize(args = nil) @args = args || {} @prefix = @args.delete(:prefix) || 'MPRedisStore' @redis_connection = @args.delete(:connection) @expires_in_seconds = @args.delete(:expires_in) || EXPIRES_IN_SECONDS end |
Instance Attribute Details
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
9 10 11 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 9 def prefix @prefix end |
Instance Method Details
#allowed_tokens ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 85 def allowed_tokens key1, key1_old, key2 = redis.mget("#{@prefix}-key1", "#{@prefix}-key1_old", "#{@prefix}-key2") if key1 && (key1.length == 32) return [key1, key2].compact end timeout = Rack::MiniProfiler::AbstractStore::MAX_TOKEN_AGE # TODO this could be moved to lua to correct a concurrency flaw # it is not critical cause worse case some requests will miss profiling info # no key so go ahead and set it key1 = SecureRandom.hex if key1_old && (key1_old.length == 32) key2 = key1_old redis.setex "#{@prefix}-key2", timeout, key2 else key2 = nil end redis.setex "#{@prefix}-key1", timeout, key1 redis.setex "#{@prefix}-key1_old", timeout * 2, key1 [key1, key2].compact end |
#diagnostics(user) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 68 def diagnostics(user) client = (redis.respond_to? :_client) ? redis._client : redis.client "Redis prefix: #{@prefix} Redis location: #{client.host}:#{client.port} db: #{client.db} unviewed_ids: #{get_unviewed_ids(user)} " end |
#fetch_snapshots(batch_size: 200, &blk) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 165 def fetch_snapshots(batch_size: 200, &blk) zset_key = snapshot_zset_key() hash_key = snapshot_hash_key() iteration = 0 corrupt_snapshots = [] while true ids = redis.zrange( zset_key, batch_size * iteration, batch_size * iteration + batch_size - 1 ) break if ids.size == 0 batch = redis.mapped_hmget(hash_key, *ids).to_a batch.map! do |id, bytes| begin Marshal.load(bytes) rescue corrupt_snapshots << id nil end end batch.compact! blk.call(batch) if batch.size != 0 break if ids.size < batch_size iteration += 1 end if corrupt_snapshots.size > 0 redis.pipelined do redis.zrem(zset_key, corrupt_snapshots) redis.hdel(hash_key, corrupt_snapshots) end end end |
#flush_tokens ⇒ Object
76 77 78 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 76 def flush_tokens redis.del("#{@prefix}-key1", "#{@prefix}-key1_old", "#{@prefix}-key2") end |
#get_unviewed_ids(user) ⇒ Object
Remove expired ids from the unviewed sorted set and return the remaining ids
62 63 64 65 66 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 62 def get_unviewed_ids(user) key = user_key(user) redis.zremrangebyscore(key, '-inf', Process.clock_gettime(Process::CLOCK_MONOTONIC).to_i) redis.zrevrangebyscore(key, '+inf', '-inf') end |
#load(id) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 24 def load(id) key = prefixed_id(id) raw = redis.get key begin Marshal::load(raw) if raw rescue # bad format, junk old data redis.del key nil end end |
#load_snapshot(id) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 199 def load_snapshot(id) hash_key = snapshot_hash_key() bytes = redis.hget(hash_key, id) begin Marshal.load(bytes) rescue redis.pipelined do redis.zrem(snapshot_zset_key(), id) redis.hdel(hash_key, id) end nil end end |
#push_snapshot(page_struct, config) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 134 def push_snapshot(page_struct, config) zset_key = snapshot_zset_key() hash_key = snapshot_hash_key() id = page_struct[:id] score = page_struct.duration_ms limit = config.snapshots_limit bytes = Marshal.dump(page_struct) lua = <<~LUA local zset_key = KEYS[1] local hash_key = KEYS[2] local id = ARGV[1] local score = tonumber(ARGV[2]) local bytes = ARGV[3] local limit = tonumber(ARGV[4]) redis.call("ZADD", zset_key, score, id) redis.call("HSET", hash_key, id, bytes) if redis.call("ZCARD", zset_key) > limit then local lowest_snapshot_id = redis.call("ZRANGE", zset_key, 0, 0)[1] redis.call("ZREM", zset_key, lowest_snapshot_id) redis.call("HDEL", hash_key, lowest_snapshot_id) end LUA redis.eval( lua, keys: [zset_key, hash_key], argv: [id, score, bytes, limit] ) end |
#save(page_struct) ⇒ Object
20 21 22 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 20 def save(page_struct) redis.setex prefixed_id(page_struct[:id]), @expires_in_seconds, Marshal::dump(page_struct) end |
#set_all_unviewed(user, ids) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 45 def set_all_unviewed(user, ids) key = user_key(user) redis.del(key) ids.each do |id| if redis.call([:exists, prefixed_id(id)]) == 1 expire_at = Process.clock_gettime(Process::CLOCK_MONOTONIC).to_i + redis.ttl(prefixed_id(id)) redis.zadd(key, expire_at, id) end end redis.expire(key, @expires_in_seconds) end |
#set_unviewed(user, id) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 36 def set_unviewed(user, id) key = user_key(user) if redis.call([:exists, prefixed_id(id)]) == 1 expire_at = Process.clock_gettime(Process::CLOCK_MONOTONIC).to_i + redis.ttl(prefixed_id(id)) redis.zadd(key, expire_at, id) end redis.expire(key, @expires_in_seconds) end |
#set_viewed(user, id) ⇒ Object
57 58 59 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 57 def set_viewed(user, id) redis.zrem(user_key(user), id) end |
#should_take_snapshot?(period) ⇒ Boolean
124 125 126 127 128 129 130 131 132 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 124 def should_take_snapshot?(period) 1 == cached_redis_eval( COUNTER_LUA, COUNTER_LUA_SHA, reraise: false, keys: [snapshot_counter_key()], argv: [period] ) end |
#simulate_expire ⇒ Object
Only used for testing
81 82 83 |
# File 'lib/mini_profiler/storage/redis_store.rb', line 81 def simulate_expire redis.del("#{@prefix}-key1") end |