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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backendObject



27
28
29
# File 'lib/cassette/cache.rb', line 27

def backend
  @backend ||= Cassette::Cache.backend
end

Class Method Details

.backendObject



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

def self.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

.backend=(backend) ⇒ Object



23
24
25
# File 'lib/cassette/cache.rb', line 23

def self.backend=(backend)
  @backend = backend
end

Instance Method Details

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cassette/cache.rb', line 37

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



33
34
35
# File 'lib/cassette/cache.rb', line 33

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