Class: Phlex::AttributeCache Private

Inherits:
FIFO
  • Object
show all
Defined in:
lib/phlex/attribute_cache.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Caches serialised attributes. Hash#eql? ignores order, a String's class and whether it's a safe object, any of which can change the output, so a hit must match the frozen snapshot taken when it was cached, in order and class for class. The snapshot interns its Strings, so a frozen literal matches by identity, and a safe object can't, since it can't be frozen before it's extended. Only plain Hashes, Arrays, Strings, SafeValues, Symbols, Integers, true, false and nil are cached; anything else is serialised every time.

The snapshot is kept with the output, so it counts against the budget too: each value it holds costs SLOT_BYTESIZE, plus a String's bytes. Attributes whose snapshot and output together cost more than one value may aren't cached, so a large input that renders to little, such as an Array of nils, can't hold memory the budget doesn't see.

API:

  • private

Defined Under Namespace

Classes: Pairs

Constant Summary collapse

SLOT_BYTESIZE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

8

Instance Method Summary collapse

Instance Method Details

#[](attributes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



39
40
41
42
# File 'lib/phlex/attribute_cache.rb', line 39

def [](attributes)
	snapshot, value = @store[attributes.hash]
	value if snapshot && matches?(snapshot, attributes)
end

#[]=(attributes, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



44
45
46
47
48
49
50
# File 'lib/phlex/attribute_cache.rb', line 44

def []=(attributes, value)
	return if value.bytesize > @max_value_bytesize

	remaining = [@max_value_bytesize - value.bytesize]
	snapshot = catch(:uncacheable) { snapshot(attributes, remaining) }
	store(attributes.hash, snapshot, value, @max_value_bytesize - remaining[0]) unless :uncacheable == snapshot
end

#fetch(attributes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/phlex/attribute_cache.rb', line 22

def fetch(attributes)
	digest = attributes.hash

	if (entry = @store[digest])
		snapshot, value = entry
		return matches?(snapshot, attributes) ? value : yield
	end

	value = yield
	return value if value.bytesize.zero? || value.bytesize > @max_value_bytesize

	remaining = [@max_value_bytesize - value.bytesize]
	snapshot = catch(:uncacheable) { snapshot(attributes, remaining) }
	store(digest, snapshot, value, @max_value_bytesize - remaining[0]) unless :uncacheable == snapshot
	value
end