Class: Rack::MiniProfiler::RedisStore

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

Constant Summary collapse

EXPIRES_IN_SECONDS =
60 * 60 * 24

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ RedisStore

Returns a new instance of RedisStore.



7
8
9
10
11
12
# File 'lib/mini_profiler/storage/redis_store.rb', line 7

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 Method Details

#diagnostics(user) ⇒ Object



44
45
46
47
48
49
# File 'lib/mini_profiler/storage/redis_store.rb', line 44

def diagnostics(user)
"Redis prefix: #{@prefix}
Redis location: #{redis.client.host}:#{redis.client.port} db: #{redis.client.db}
unviewed_ids: #{get_unviewed_ids(user)}
"
end

#get_unviewed_ids(user) ⇒ Object



40
41
42
# File 'lib/mini_profiler/storage/redis_store.rb', line 40

def get_unviewed_ids(user)
  redis.smembers "#{@prefix}-#{user}-v"
end

#load(id) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mini_profiler/storage/redis_store.rb', line 18

def load(id)
  key = "#{@prefix}#{id}"
  raw = redis.get key
  begin
    Marshal::load(raw) if raw
  rescue
    # bad format, junk old data
    redis.del key
    nil
  end
end

#save(page_struct) ⇒ Object



14
15
16
# File 'lib/mini_profiler/storage/redis_store.rb', line 14

def save(page_struct)
  redis.setex "#{@prefix}#{page_struct[:id]}", @expires_in_seconds, Marshal::dump(page_struct)
end

#set_unviewed(user, id) ⇒ Object



30
31
32
33
34
# File 'lib/mini_profiler/storage/redis_store.rb', line 30

def set_unviewed(user, id)
  key = "#{@prefix}-#{user}-v"
  redis.sadd key, id
  redis.expire key, @expires_in_seconds
end

#set_viewed(user, id) ⇒ Object



36
37
38
# File 'lib/mini_profiler/storage/redis_store.rb', line 36

def set_viewed(user, id)
  redis.srem "#{@prefix}-#{user}-v", id
end