Sha256: 382dd85d6889f5a7cf5e1cfe588db21e5d7143d0caa482e45f28eadd107f3931

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true
require 'dalli/cas/client'

module IdentityCache
  module MemCacheStoreCAS
    def cas(name, options = nil)
      options = merged_options(options)
      key = normalize_key(name, options)

      rescue_error_with(false) do
        instrument(:cas, key, options) do
          @data.with do |connection|
            connection.cas(key, options[:expires_in].to_i, options) do |raw_value|
              entry = deserialize_entry(raw_value)
              value = yield entry.value
              entry = ActiveSupport::Cache::Entry.new(value, **options)
              options[:raw] ? entry.value.to_s : entry
            end
          end
        end
      end
    end

    def cas_multi(*names, **options)
      return if names.empty?

      options = merged_options(options)
      keys_to_names = names.each_with_object({}) { |name, hash| hash[normalize_key(name, options)] = name }
      keys = keys_to_names.keys
      rescue_error_with(false) do
        instrument(:cas_multi, keys, options) do
          raw_values = @data.get_multi_cas(keys)

          values = {}
          raw_values.each do |key, raw_value|
            entry = deserialize_entry(raw_value.first)
            values[keys_to_names[key]] = entry.value unless entry.expired?
          end

          updates = yield values

          updates.each do |name, value|
            key = normalize_key(name, options)
            cas_id = raw_values[key].last
            entry = ActiveSupport::Cache::Entry.new(value, **options)
            payload = options[:raw] ? entry.value.to_s : entry
            @data.replace_cas(key, payload, cas_id, options[:expires_in].to_i, options)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
identity_cache-1.1.0 lib/identity_cache/mem_cache_store_cas.rb