Class: Paquito::FlatCacheEntryCoder

Inherits:
Object
  • Object
show all
Defined in:
lib/paquito/flat_cache_entry_coder.rb

Constant Summary collapse

METADATA_CODEC =
CodecFactory.build
EXPIRES_AT_FORMAT =

Float double-precision, little-endian byte order (8 bytes)

"E"
VERSION_SIZE_FORMAT =

32-bit signed, little-endian byte order (int32_t) (4 bytes)

"l<"
PREFIX_FORMAT =
-(EXPIRES_AT_FORMAT + VERSION_SIZE_FORMAT)
VERSION_SIZE_OFFSET =

should be 8

[0.0].pack(EXPIRES_AT_FORMAT).bytesize
VERSION_OFFSET =

Should be 12

[0.0, 0].pack(PREFIX_FORMAT).bytesize
VERSION_SIZE_UNPACK =
-"@#{VERSION_SIZE_OFFSET}#{VERSION_SIZE_FORMAT}"

Instance Method Summary collapse

Constructor Details

#initialize(value_coder) ⇒ FlatCacheEntryCoder

Returns a new instance of FlatCacheEntryCoder.



16
17
18
# File 'lib/paquito/flat_cache_entry_coder.rb', line 16

def initialize(value_coder)
  @value_coder = value_coder
end

Instance Method Details

#dump(entry) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/paquito/flat_cache_entry_coder.rb', line 20

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

#load(payload) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/paquito/flat_cache_entry_coder.rb', line 30

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