Module: Cassette::Cache

Included in:
Authentication::Cache, Cassette::Client::Cache
Defined in:
lib/cassette/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backendObject


7
8
9
10
11
12
13
14
15
# File 'lib/cassette/cache.rb', line 7

def backend
  @backend ||= begin
    if defined?(::Rails) && ::Rails.cache
      ::Rails.cache
    else
      ActiveSupport::Cache::MemoryStore.new
    end
  end
end

Instance Method Details

#fetch(key, options = {}, &block) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cassette/cache.rb', line 23

def fetch(key, options = {}, &block)
  if options[:max_uses].to_i != 0
    uses_key = self.uses_key(key)
    uses = backend.read(uses_key, raw: true)
    backend.write(uses_key, 0, raw: true, expires_in: options[:expires_in]) if uses.nil?

    if uses.to_i >= options[:max_uses].to_i
      options[:force] = true
      backend.write(uses_key, 0, raw: true, expires_in: options[:expires_in])
    else
      backend.increment(uses_key)
    end
  end

  backend.fetch(key, options, &block)
end

#uses_key(key) ⇒ Object


19
20
21
# File 'lib/cassette/cache.rb', line 19

def uses_key(key)
  "uses:#{key}"
end