Class: Card::Cache

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/card/cache.rb,
lib/card/cache/all.rb,
lib/card/cache/shared.rb,
lib/card/cache/populate.rb,
lib/card/cache/temporary.rb,
lib/card/cache/card_class.rb,
lib/card/cache/shared_class.rb,
lib/card/cache/class_methods.rb

Overview

The Cache class manages and integrates Temporary and Shared caching. The Temporary cache is typically process- and request- specific and is often “ahead” of the database; the Shared cache is typically shared across processes and tends to stay true to the database.

Any ruby Class can declare and/or retrieve its own cache as follows:

““ Card::Cache ““

Typically speaking, mod developers do not need to use the Cache classes directly, because caching is automatically handled by Card#fetch

Defined Under Namespace

Modules: All, CardClass, ClassMethods, Populate, SharedClass Classes: Shared, Temporary

Instance Attribute Summary collapse

Attributes included from ClassMethods

#counter, #no_renewal

Instance Method Summary collapse

Methods included from ClassMethods

[], cache_by_class, renew, renew_shared, reset_all, reset_global, reset_other, reset_shared, reset_temp, restore, shared_cache, shared_on!, tallies

Methods included from Populate

#populate_fields, #populate_ids, #populate_names

Constructor Details

#initialize(opts = {}) ⇒ Cache

Cache#new initializes a Temporary cache, and – if a :store opt is provided – a Shared cache

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :store (Rails::Cache)
  • :class (Constant)


26
27
28
29
30
# File 'lib/card/cache.rb', line 26

def initialize opts={}
  @klass = opts[:class]
  @shared = Shared.new opts if opts[:store]
  @temp = Temporary.new @klass
end

Instance Attribute Details

#sharedObject (readonly)

Returns the value of attribute shared.



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

def shared
  @shared
end

#tempObject (readonly)

Returns the value of attribute temp.



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

def temp
  @temp
end

Instance Method Details

#delete(key) ⇒ Object

delete specific cache entries by key

Parameters:

  • key (String)


74
75
76
77
78
79
# File 'lib/card/cache.rb', line 74

def delete key
  track :delete, key do
    @shared&.delete key
    @temp.delete key
  end
end

#exist?(key) ⇒ true/false

test for the existence of the key in either cache

Returns:

  • (true/false)


89
90
91
# File 'lib/card/cache.rb', line 89

def exist? key
  @temp.exist?(key) || @shared&.exist?(key)
end

#fetch(key, &block) ⇒ Object

read and (if not there yet) write

Parameters:

  • key (String)


64
65
66
67
68
69
70
# File 'lib/card/cache.rb', line 64

def fetch key, &block
  @temp.fetch(key) do
    track :fetch, key do
      @shared ? @shared.fetch(key, &block) : yield(key)
    end
  end
end

#read(key) ⇒ Object

read cache value (and write to temp cache if missing)

Parameters:

  • key (String)


34
35
36
37
38
39
40
# File 'lib/card/cache.rb', line 34

def read key
  @temp.fetch(key) do
    track :read, key do
      @shared&.read key
    end
  end
end

#read_multi(keys) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/card/cache.rb', line 42

def read_multi keys
  with_multiple_keys keys do
    @temp.fetch_multi keys do |missing_keys|
      track :read_multi, missing_keys do
        @shared ? @shared.read_multi(missing_keys) : {}
      end
    end
  end
end

#resetObject

reset both caches (for a given Card::Cache instance)



82
83
84
85
# File 'lib/card/cache.rb', line 82

def reset
  @shared&.reset
  @temp.reset
end

#write(key, value) ⇒ Object

write to hard (where applicable) and temp cache

Parameters:

  • key (String)
  • value


55
56
57
58
59
60
# File 'lib/card/cache.rb', line 55

def write key, value
  track :write, key do
    @shared&.write key, value
    @temp.write key, value
  end
end