Class: ActiveSupport::Cache::TaggedStore

Inherits:
Store
  • Object
show all
Defined in:
lib/active_support/cache/tagged_store.rb

Defined Under Namespace

Classes: Dependencies

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ TaggedStore

Returns a new instance of TaggedStore.



37
38
39
40
41
42
43
44
45
46
# File 'lib/active_support/cache/tagged_store.rb', line 37

def initialize(options = nil)
  unless options && options[:tag_store] && options[:entity_store]
    raise ":tag_store and :entity_store options are required"
  else
    @tag_store = Cache.lookup_store(*options.delete(:tag_store))
    @entity_store = Cache.lookup_store(*options.delete(:entity_store))
    
    @options = {}
  end
end

Instance Method Details

#cleanup(options = nil) ⇒ Object



130
131
132
# File 'lib/active_support/cache/tagged_store.rb', line 130

def cleanup(options = nil)
  @entity_store.cleanup(options)
end

#clear(options = nil) ⇒ Object



134
135
136
# File 'lib/active_support/cache/tagged_store.rb', line 134

def clear(options = nil)
  @entity_store.clear(options)
end

#decrement(name, amount = 1, options = nil) ⇒ Object



126
127
128
# File 'lib/active_support/cache/tagged_store.rb', line 126

def decrement(name, amount = 1, options = nil)
  @entity_store.decrement(name, amount, options)
end

#delete_matched(matcher, options = nil) ⇒ Object



118
119
120
# File 'lib/active_support/cache/tagged_store.rb', line 118

def delete_matched(matcher, options = nil)
  @entity_store.delete_matched(matcher, options)
end

#increment(name, amount = 1, options = nil) ⇒ Object



122
123
124
# File 'lib/active_support/cache/tagged_store.rb', line 122

def increment(name, amount = 1, options = nil)
  @entity_store.increment(name, amount, options)
end

#read_tag(key) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_support/cache/tagged_store.rb', line 48

def read_tag(key)
  instrument(:read_tag, key) do
    key = expanded_tag(key)
    tag_value = @tag_store.read(key, :raw => true)
    if tag_value.nil? || tag_value.to_i.zero?
      new_value = Time.now.to_i
      @tag_store.write(key, new_value, :raw => true)
      new_value
    else
      tag_value.to_i
    end
  end
end

#read_tags(*keys) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_support/cache/tagged_store.rb', line 62

def read_tags(*keys)
  instrument(:read_tags, keys) do
    options = keys.extract_options!
    keys = keys.map { |k| expanded_tag(k) }
    tags = @tag_store.read_multi(*(keys + [options.merge(:raw => true)]))
    (keys - tags.keys).each do |unknown_tag|
      tags[unknown_tag] = read_tag(unknown_tag)
    end
    tags
  end
end

#tagged_fetch(name, options = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/active_support/cache/tagged_store.rb', line 81

def tagged_fetch(name, options = nil)
  if block_given?
    options = merged_options(options)
    key = namespaced_key(name, options)
    unless options[:force]
      entry = instrument(:read, name, options) do |payload|
        payload[:super_operation] = :fetch if payload
        read_entry(key, options)
      end
    end
    if entry && entry.expired?
      race_ttl = options[:race_condition_ttl].to_f
      if race_ttl and Time.now.to_f - entry.expires_at <= race_ttl
        entry.expires_at = Time.now + race_ttl
        write_entry(key, entry, :expires_in => race_ttl * 2)
      else
        delete_entry(key, options)
      end
      entry = nil
    end

    if entry
      instrument(:fetch_hit, name, options) { |payload| }
      entry.value
    else
      dependencies = Dependencies.new(options[:depends])
      result = instrument(:generate, name, options) do |payload|
        yield(dependencies)
      end
      write(name, result, options.merge(:depends => dependencies.tags))
      result
    end
  else
    read(name, options)
  end        
end

#touch_tag(key) ⇒ Object



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

def touch_tag(key)
  instrument(:touch_tag, key) do
    key = expanded_tag(key)
    @tag_store.increment(key) || @tag_store.write(key, Time.now.to_i, :raw => true)
  end
end