Module: Rails::Cache::Tags::Store

Extended by:
ActiveSupport::Concern
Defined in:
lib/rails/cache/tags/store.rb

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Instance Method Details

#delete_tag(*names) ⇒ Object Also known as: delete_by_tag, delete_by_tags, expire_tag

Increment the version of tags, so all entries referring to the tags become invalid



103
104
105
106
# File 'lib/rails/cache/tags/store.rb', line 103

def delete_tag(*names)
  tags = Tag.build(names)
  tags.each { |tag| tag_set.expire(tag) }
end

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

Returns:

  • (Boolean)


66
67
68
# File 'lib/rails/cache/tags/store.rb', line 66

def exist_with_tags?(name, options = nil)
  exist_without_tags?(name, options) && !read(name, options).nil?
end

#fetch_with_tags(name, options = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rails/cache/tags/store.rb', line 79

def fetch_with_tags(name, options = nil)
  return read(name, options) unless block_given?

  yielded = false

  result = fetch_without_tags(name, options) do
    yielded = true
    yield
  end

  if yielded
    result
  else # only :read occured
    # read occured, and result is fresh
    if (entry = tag_set.check(result))
      entry
    else # result is stale
      delete(name, options)
      fetch(name, options) { yield }
    end
  end
end

#read_multi_with_tags(*names) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/rails/cache/tags/store.rb', line 70

def read_multi_with_tags(*names)
  result = read_multi_without_tags(*names)

  names.extract_options!
  names.each_with_object(Hash.new) do |name, hash|
    hash[name.to_s] = tag_set.check(result[name.to_s])
  end
end

#read_with_tags(name, options = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails/cache/tags/store.rb', line 39

def read_with_tags(name, options = nil)
  result = read_without_tags(name, options)
  return if result.nil?
  entry = tag_set.check(result)

  if entry
    entry
  else
    delete(name, options)

    nil
  end
end

#tag_setObject



35
36
37
# File 'lib/rails/cache/tags/store.rb', line 35

def tag_set
  @tag_set ||= Set.new(self)
end

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



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails/cache/tags/store.rb', line 53

def write_with_tags(name, value, options = nil)
  if options && options[:tags].present?
    tags = Tag.build(options[:tags])
    tags_hash = tags.each_with_object(Hash.new) do |tag, hash|
      hash[tag.name] = tag_set.current(tag)
    end

    value = Entry.new(value, tags_hash)
  end

  write_without_tags(name, value, options)
end