Sha256: 6d8e6eb0d6af189b68a8950e9fff8cef8205e655fa994748ead3e8a3566300ed

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

gem "activesupport", ">= 7.0"

module Paquito
  class FlatCacheEntryCoder
    METADATA_CODEC = CodecFactory.build

    EXPIRES_AT_FORMAT = "E" # Float double-precision, little-endian byte order (8 bytes)
    VERSION_SIZE_FORMAT = "l<" # 32-bit signed, little-endian byte order (int32_t) (4 bytes)
    PREFIX_FORMAT = -(EXPIRES_AT_FORMAT + VERSION_SIZE_FORMAT)
    VERSION_SIZE_OFFSET = [0.0].pack(EXPIRES_AT_FORMAT).bytesize # should be 8
    VERSION_OFFSET = [0.0, 0].pack(PREFIX_FORMAT).bytesize # Should be 12
    VERSION_SIZE_UNPACK = -"@#{VERSION_SIZE_OFFSET}#{VERSION_SIZE_FORMAT}"

    def initialize(value_coder)
      @value_coder = value_coder
    end

    def dump(entry)
      version = entry.version
      payload = [
        entry.expires_at || 0.0,
        version ? version.bytesize : -1,
      ].pack(PREFIX_FORMAT)
      payload << version if version
      payload << @value_coder.dump(entry.value)
    end

    def load(payload)
      expires_at = payload.unpack1(EXPIRES_AT_FORMAT)
      expires_at = nil if expires_at == 0.0

      version_size = payload.unpack1(VERSION_SIZE_UNPACK)
      if version_size < 0
        version_size = 0
      else
        version = payload.byteslice(VERSION_OFFSET, version_size)
      end

      ::ActiveSupport::Cache::Entry.new(
        @value_coder.load(payload.byteslice((VERSION_OFFSET + version_size)..-1).freeze),
        expires_at: expires_at,
        version: version,
      )
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
paquito-0.11.2 lib/paquito/flat_cache_entry_coder.rb
paquito-0.11.1 lib/paquito/flat_cache_entry_coder.rb
paquito-0.11.0 lib/paquito/flat_cache_entry_coder.rb
paquito-0.10.0 lib/paquito/flat_cache_entry_coder.rb
paquito-0.9.2 lib/paquito/flat_cache_entry_coder.rb
paquito-0.9.1 lib/paquito/flat_cache_entry_coder.rb
paquito-0.9.0 lib/paquito/flat_cache_entry_coder.rb