Skip to content

A platform for community discussion. Free, open, simple.

FIX: Treat corrupt cache as cache miss

Loïc Guitaut2a22a3b51d99
diff --git a/lib/cache.rb b/lib/cache.rb
index 296519b17ed..313fc418a83 100644
--- a/lib/cache.rb
+++ b/lib/cache.rb
@@ -61,7 +61,7 @@ class Cache
# this removes a bunch of stuff we do not need like instrumentation and versioning
def read(name)
key = normalize_key(name)
- read_entry(key)
+ read_entry(key).tap { |entry| break if entry == :__corrupt_cache__ }
end
def write(name, value, expires_in: nil)
@@ -73,38 +73,30 @@ class Cache
end
def fetch(name, expires_in: nil, force: nil, &blk)
- if block_given?
- key = normalize_key(name)
- raw = nil
-
- raw = redis.get(key) if !force
-
- if raw
- begin
- Marshal.load(raw) # rubocop:disable Security/MarshalLoad
- rescue => e
- log_first_exception(e)
- end
- else
- val = blk.call
- write_entry(key, val, expires_in: expires_in)
- val
+ if !block_given?
+ if force
+ raise ArgumentError,
+ "Missing block: Calling `Cache#fetch` with `force: true` requires a block."
end
- elsif force
- raise ArgumentError,
- "Missing block: Calling `Cache#fetch` with `force: true` requires a block."
- else
- read(name)
+ return read(name)
end
+
+ key = normalize_key(name)
+ raw = redis.get(key) if !force
+ entry = read_entry(key) if raw
+ return entry if raw && !(entry == :__corrupt_cache__)
+
+ val = blk.call
+ write_entry(key, val, expires_in: expires_in)
+ val
end
protected
- def log_first_exception(e)
- if !defined?(@logged_a_warning)
- @logged_a_warning = true
- Discourse.warn_exception(e, "Corrupt cache... skipping entry for key #{key}")
- end
+ def log_first_exception(e, key)
+ return if defined?(@logged_a_warning)
+ @logged_a_warning = true
+ Discourse.warn_exception(e, message: "Corrupt cache... skipping entry for key #{key}")
end
def read_entry(key)
@@ -116,7 +108,8 @@ class Cache
# changes. Log it once so we can tell it is happening.
# should not happen under any normal circumstances, but we
# do not want to flood logs
- log_first_exception(e)
+ log_first_exception(e, key)
+ :__corrupt_cache__
end
def write_entry(key, value, expires_in: nil)
diff --git a/spec/lib/cache_spec.rb b/spec/lib/cache_spec.rb
index ae09bcc39b6..96d497ebeb4 100644
--- a/spec/lib/cache_spec.rb
+++ b/spec/lib/cache_spec.rb
@@ -3,9 +3,7 @@
require "cache"
RSpec.describe Cache do
- let :cache do
- Cache.new
- end
+ subject(:cache) { Cache.new }
it "supports exist?" do
cache.write("testing", 1.1)
@@ -104,4 +102,24 @@ RSpec.describe Cache do
expect(cache.redis.ttl("#{cache.namespace}:foo:bar")).to eq(180)
end
+
+ describe ".fetch" do
+ subject(:fetch_value) { cache.fetch("my_key") { "bob" } }
+
+ context "when the cache is corrupt" do
+ before do
+ cache.delete("my_key")
+ Discourse.redis.setex(cache.normalize_key("my_key"), described_class::MAX_CACHE_AGE, "")
+ end
+
+ it "runs and return the provided block" do
+ expect(fetch_value).to eq("bob")
+ end
+
+ it "generates a new cache entry" do
+ fetch_value
+ expect(cache.read("my_key")).to eq("bob")
+ end
+ end
+ end
end