diff --git a/Gemfile b/Gemfile index 1466767..9394193 100644 --- a/Gemfile +++ b/Gemfile @@ -3,3 +3,6 @@ source 'https://rubygems.org' gemspec gem "snappy" + +# gem "memcached", path: "../memcached" +gem "memcached", github: "Shopify/memcached", branch: "catlee/with_error_value" diff --git a/lib/active_support/cache/memcached_store.rb b/lib/active_support/cache/memcached_store.rb index 92fde8b..7180159 100644 --- a/lib/active_support/cache/memcached_store.rb +++ b/lib/active_support/cache/memcached_store.rb @@ -138,14 +138,20 @@ def cas(name, options = nil) success = handle_exceptions(return_value_on_error: false) do instrument(:cas, name, options) do - @connection.cas(key, expiration(options)) do |raw_value| - entry = deserialize_entry(raw_value) - value = yield entry.value - break true if read_only - payload = serialize_entry(Entry.new(value, **options), options) + @connection.without_exceptions do + ret = @connection.cas(key, expiration(options)) do |raw_value| + entry = deserialize_entry(raw_value) + value = yield entry.value + break true if read_only + payload = serialize_entry(Entry.new(value, **options), options) + end + # `cas` returns NOT_FOUND if the key doesn't exist, DATA_EXISTS if the key was modified by another client + # and it returns `nil` on success, + # + # Here we just want to return true/false if the key was set successfully, which is the case when `cas` returns nil + ret == nil end end - true end if success @@ -288,7 +294,11 @@ def read_entry(key, **options) # :nodoc: def read_serialized_entry(key, **) handle_exceptions(return_value_on_error: nil) do - @connection.get(key) + @connection.without_exceptions do + val = @connection.get(key) + return nil if val == Memcached::NOT_FOUND + val + end end end diff --git a/memcached_store.gemspec b/memcached_store.gemspec index d2d1524..d6e2ff3 100644 --- a/memcached_store.gemspec +++ b/memcached_store.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.6.0" spec.add_runtime_dependency "activesupport", ">= 6" - spec.add_runtime_dependency "memcached", "~> 1.8" + spec.add_runtime_dependency "memcached", ">= 1.10" spec.add_development_dependency "rake" spec.add_development_dependency "mocha"