Class: ActiveSupport::Cache::Store
- Inherits:
-
Object
- Object
- ActiveSupport::Cache::Store
- Defined in:
- lib/caching_with_tags/store.rb
Instance Method Summary collapse
- #exist?(name, options = nil) ⇒ Boolean
- #fetch(name, options = nil) ⇒ Object
- #increment_tag(name, options = nil) ⇒ Object
- #read(name, options = nil) ⇒ Object
- #tags_actual?(tags) ⇒ Boolean
- #write(name, value, options = nil) ⇒ Object
-
#write_meta(entry, tags) ⇒ Object
patch.
Instance Method Details
#exist?(name, options = nil) ⇒ 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, = nil) = () instrument(:exist?, name) do |payload| #####patched entry = read_entry(namespaced_key(name, ), ) entry = nil if entry && entry. && !(entry.) #####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, = nil) if block_given? = () key = namespaced_key(name, ) ########patched unless [:force] entry = instrument(:read, name, ) do |payload| payload[:super_operation] = :fetch if payload ent = read_entry(key, ) ent = nil if ent && ent. && !(ent.) ent end end ########end of patch if entry && entry.expired? race_ttl = [: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, ) end entry = nil end if entry instrument(:fetch_hit, name, ) { |payload| } entry.value else result = instrument(:generate, name, ) do |payload| yield end write(name, result, ) result end else read(name, ) 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, = nil) = () entry = read_entry(namespaced_key(name, {:namespace => nil}), ) value = entry ? entry.value + 1 : 1 write_entry(namespaced_key(name, {:namespace => nil}), ActiveSupport::Cache::Entry.new(value), ) 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, = nil) = () key = namespaced_key(name, ) instrument(:read, name, ) do |payload| ########patched entry = read_entry(key, ) entry = nil if entry && entry. && !(entry.) #######patch end if entry if entry.expired? delete_entry(key, ) 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
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/caching_with_tags/store.rb', line 107 def () if .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(*.keys.map { |k| k.dup } << {:namespace => nil}) actual_versions == 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, = nil) = () instrument(:write, name, ) do |payload| ######patch entry = ActiveSupport::Cache::Entry.new(value, ) (entry, [:tags]) if [:tags] write_entry(namespaced_key(name, ), entry, ) ######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 (entry, ) t = if .is_a?(Array) .inject({}) do |res, el| res[el] = fetch(el, {:namespace => nil}) { 1 } res end end entry. = t end |