Class: ActiveSupport::Cache::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/caching_with_tags/store.rb

Instance Method Summary collapse

Instance Method Details

#exist?(name, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/caching_with_tags/store.rb', line 80

def exist?(name, options = nil)
  options = merged_options(options)
  instrument(:exist?, name) do |payload|
    #####patched
    entry = read_entry(namespaced_key(name, options), options)
    entry = nil if entry && entry.tags && !tags_actual?(entry.tags)
    #####end of patch
    if entry && !entry.expired?
      true
    else
      false
    end
  end
end

#fetch(name, options = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/caching_with_tags/store.rb', line 29

def fetch(name, options = nil)
  if block_given?
    options = merged_options(options)
    key = namespaced_key(name, options)
    ########patched
    unless options[:force]
      entry = instrument(:read, name, options) do |payload|
        payload[:super_operation] = :fetch if payload
        ent = read_entry(key, options)
        ent = nil if ent && ent.tags && !tags_actual?(ent.tags)
        ent
      end
    end
    ########end of patch
    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
      result = instrument(:generate, name, options) do |payload|
        yield
      end
      write(name, result, options)
      result
    end
  else
    read(name, options)
  end
end

#increment_tag(name, options = nil) ⇒ Object



120
121
122
123
124
125
# File 'lib/caching_with_tags/store.rb', line 120

def increment_tag(name, options = nil)
  options = merged_options(options)
  entry = read_entry(namespaced_key(name, {:namespace => nil}), options)
  value = entry ? entry.value + 1 : 1
  write_entry(namespaced_key(name, {:namespace => nil}), ActiveSupport::Cache::Entry.new(value), options)
end

#read(name, options = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/caching_with_tags/store.rb', line 4

def read(name, options = nil)
  options = merged_options(options)
  key = namespaced_key(name, options)
  instrument(:read, name, options) do |payload|
    ########patched
    entry = read_entry(key, options)
    entry = nil if entry && entry.tags && !tags_actual?(entry.tags)
    #######patch end

    if entry
      if entry.expired?
        delete_entry(key, options)
        payload[:hit] = false if payload
        nil
      else
        payload[:hit] = true if payload
        entry.value
      end
    else
      payload[:hit] = false if payload
      nil
    end
  end
end

#tags_actual?(tags) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/caching_with_tags/store.rb', line 107

def tags_actual?(tags)
  if tags.is_a?(Hash)
    # We're doing dup here because of Dalli makes force_encoding
    # and ActiveSupport returns frozen values, so it raises an exception.
    # Ruby uses copy-on-write technique so it's extremely cheap to make a dup
    # of all tags.keys as far as we don't modify them
    actual_versions = read_multi(*tags.keys.map { |k| k.dup } << {:namespace => nil})
    actual_versions == tags
  else
    false
  end
end

#write(name, value, options = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/caching_with_tags/store.rb', line 69

def write(name, value, options = nil)
  options = merged_options(options)
  instrument(:write, name, options) do |payload|
    ######patch
    entry = ActiveSupport::Cache::Entry.new(value, options)
    write_meta(entry, options[:tags]) if options[:tags]
    write_entry(namespaced_key(name, options), entry, options)
    ######end of patch
  end
end

#write_meta(entry, tags) ⇒ Object

patch



97
98
99
100
101
102
103
104
105
# File 'lib/caching_with_tags/store.rb', line 97

def write_meta(entry, tags)
  t = if tags.is_a?(Array)
        tags.inject({}) do |res, el|
      res[el] = fetch(el, {:namespace => nil}) { 1 }
      res
    end
      end
  entry.tags = t
end