Class: Rack::MiniProfiler::MemcacheStore

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

Constant Summary collapse

EXPIRES_IN_SECONDS =
60 * 60 * 24
MAX_RETRIES =
10

Constants inherited from AbstractStore

AbstractStore::MAX_TOKEN_AGE

Instance Method Summary collapse

Methods inherited from AbstractStore

#diagnostics, #fetch_snapshots_group, #fetch_snapshots_overview, #load_snapshot, #push_snapshot, #should_take_snapshot?, #snapshots_group, #snapshots_overview

Constructor Details

#initialize(args = nil) ⇒ MemcacheStore

Returns a new instance of MemcacheStore.



10
11
12
13
14
15
16
17
18
19
# File 'lib/mini_profiler/storage/memcache_store.rb', line 10

def initialize(args = nil)
  require 'dalli' unless defined? Dalli
  args ||= {}

  @prefix = args[:prefix] || "MPMemcacheStore"
  @prefix += "-#{Rack::MiniProfiler::VERSION}"

  @client             = args[:client]     || Dalli::Client.new
  @expires_in_seconds = args[:expires_in] || EXPIRES_IN_SECONDS
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
# File 'lib/mini_profiler/storage/memcache_store.rb', line 21

def alive?
  begin
    @client.alive!
    true
  rescue Dalli::RingError
    false
  end
end

#allowed_tokensObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mini_profiler/storage/memcache_store.rb', line 73

def allowed_tokens

  token_info = @client.get("#{@prefix}-tokens")
  key1, key2, cycle_at = nil

  if token_info
    # rubocop:disable Security/MarshalLoad
    key1, key2, cycle_at = Marshal.load(token_info)
    # rubocop:enable Security/MarshalLoad

    key1 = nil unless key1 && key1.length == 32
    key2 = nil unless key2 && key2.length == 32

    if key1 && cycle_at && (cycle_at > Process.clock_gettime(Process::CLOCK_MONOTONIC))
      return [key1, key2].compact
    end
  end

  timeout = Rack::MiniProfiler::AbstractStore::MAX_TOKEN_AGE

  # cycle keys
  key2 = key1
  key1 = SecureRandom.hex
  cycle_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout

  @client.set("#{@prefix}-tokens", Marshal::dump([key1, key2, cycle_at]))

  [key1, key2].compact
end

#flush_tokensObject



69
70
71
# File 'lib/mini_profiler/storage/memcache_store.rb', line 69

def flush_tokens
  @client.set("#{@prefix}-tokens", nil)
end

#get_unviewed_ids(user) ⇒ Object



65
66
67
# File 'lib/mini_profiler/storage/memcache_store.rb', line 65

def get_unviewed_ids(user)
  @client.get("#{@prefix}-#{user}-v") || []
end

#load(id) ⇒ Object



34
35
36
37
38
39
# File 'lib/mini_profiler/storage/memcache_store.rb', line 34

def load(id)
  raw = @client.get("#{@prefix}#{id}")
  # rubocop:disable Security/MarshalLoad
  Marshal.load(raw) if raw
  # rubocop:enable Security/MarshalLoad
end

#save(page_struct) ⇒ Object



30
31
32
# File 'lib/mini_profiler/storage/memcache_store.rb', line 30

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

#set_all_unviewed(user, ids) ⇒ Object



61
62
63
# File 'lib/mini_profiler/storage/memcache_store.rb', line 61

def set_all_unviewed(user, ids)
  @client.set("#{@prefix}-#{user}-v", ids, @expires_in_seconds)
end

#set_unviewed(user, id) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/mini_profiler/storage/memcache_store.rb', line 41

def set_unviewed(user, id)
  @client.add("#{@prefix}-#{user}-v", [], @expires_in_seconds)
  MAX_RETRIES.times do
    break if @client.cas("#{@prefix}-#{user}-v", @expires_in_seconds) do |ids|
      ids << id unless ids.include?(id)
      ids
    end
  end
end

#set_viewed(user, id) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/mini_profiler/storage/memcache_store.rb', line 51

def set_viewed(user, id)
  @client.add("#{@prefix}-#{user}-v", [], @expires_in_seconds)
  MAX_RETRIES.times do
    break if @client.cas("#{@prefix}-#{user}-v", @expires_in_seconds) do |ids|
      ids.delete id
      ids
    end
  end
end