Module: Cassette::Cache

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

Defined Under Namespace

Classes: NullStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backendObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cassette/cache.rb', line 11

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

Instance Method Details

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cassette/cache.rb', line 29

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



25
26
27
# File 'lib/cassette/cache.rb', line 25

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