Module: Cassette::Cache

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

Instance Method Summary collapse

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

#backend=(backend) ⇒ Object



17
18
19
# File 'lib/cassette/cache.rb', line 17

def backend=(backend)
  @backend = backend
end

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



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

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



21
22
23
# File 'lib/cassette/cache.rb', line 21

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