lib/phlex/attribute_cache.rb
Lines
100%56 of 56 covered
Branches
100%44 of 44 covered
Methods
100%6 of 6 covered
| 1 | # frozen_string_literal: true | |
| 2 | ||
| 3 | # @api private | |
| 4 | # | |
| 5 | # Caches serialised attributes. Hash#eql? ignores order, a String's class and | |
| 6 | # whether it's a safe object, any of which can change the output, so a hit | |
| 7 | # must match the frozen snapshot taken when it was cached, in order and class | |
| 8 | # for class. The snapshot interns its Strings, so a frozen literal matches by | |
| 9 | # identity, and a safe object can't, since it can't be frozen before it's | |
| 10 | # extended. Only plain Hashes, Arrays, Strings, SafeValues, Symbols, Integers, | |
| 11 | # true, false and nil are cached; anything else is serialised every time. | |
| 12 | 1 | class Phlex::AttributeCache < Phlex::FIFO |
| 13 | 1 | Pairs = Data.define(:items) |
| 14 | ||
| 15 | 1 | def fetch(attributes) |
| 16 | 1864 | digest = attributes.hash |
| 17 | ||
| 18 | 1864 | if (entry = @store[digest]) |
| 19 | 1022 | snapshot, value = entry |
| 20 | 1022 | return matches?(snapshot, attributes) ? value : yield |
| 21 | end | |
| 22 | ||
| 23 | 842 | value = yield |
| 24 | 545 | return value if value.bytesize.zero? || value.bytesize > @max_value_bytesize |
| 25 | ||
| 26 | 642 | snapshot = catch(:uncacheable) { snapshot(attributes) } |
| 27 | 321 | store(digest, snapshot, value) unless :uncacheable == snapshot |
| 28 | 321 | value |
| 29 | end | |
| 30 | ||
| 31 | 1 | def [](attributes) |
| 32 | 27 | snapshot, value = @store[attributes.hash] |
| 33 | 27 | value if snapshot && matches?(snapshot, attributes) |
| 34 | end | |
| 35 | ||
| 36 | 1 | def []=(attributes, value) |
| 37 | 4 | snapshot = catch(:uncacheable) { snapshot(attributes) } |
| 38 | 2 | store(attributes.hash, snapshot, value) unless :uncacheable == snapshot |
| 39 | end | |
| 40 | ||
| 41 | 1 | private def snapshot(value) |
| 42 | 1636 | case value |
| 43 | when Symbol, Integer, true, false, nil | |
| 44 | 685 | value |
| 45 | when String | |
| 46 | 300 | throw :uncacheable, :uncacheable unless String.equal?(value.class) && !(Phlex::SGML::SafeObject === value) |
| 47 | 297 | -value |
| 48 | when Phlex::SGML::SafeValue | |
| 49 | 24 | throw :uncacheable, :uncacheable unless Phlex::SGML::SafeValue.equal?(value.class) && String.equal?(value.to_s.class) |
| 50 | 23 | Phlex::SGML::SafeValue.new(-value.to_s) |
| 51 | when Hash | |
| 52 | 446 | throw :uncacheable, :uncacheable unless Hash.equal?(value.class) |
| 53 | 1008 | Pairs.new(value.flat_map { |k, v| [snapshot_key(k), snapshot(v)] }.freeze) |
| 54 | when Array | |
| 55 | 92 | throw :uncacheable, :uncacheable unless Array.equal?(value.class) |
| 56 | 260 | value.map { |item| snapshot(item) }.freeze |
| 57 | else | |
| 58 | 89 | throw :uncacheable, :uncacheable |
| 59 | end | |
| 60 | end | |
| 61 | ||
| 62 | # A pair with a nil or false value is skipped when serialising, so its key | |
| 63 | # can be anything, even a cyclic Array. | |
| 64 | 1 | private def snapshot_key(key) |
| 65 | 563 | case key |
| 66 | 561 | when Symbol, String then snapshot(key) |
| 67 | 2 | else throw :uncacheable, :uncacheable |
| 68 | end | |
| 69 | end | |
| 70 | ||
| 71 | 1 | private def matches?(snapshot, value) |
| 72 | 1378 | case snapshot |
| 73 | when Pairs | |
| 74 | 1287 | items = snapshot.items |
| 75 | 1287 | return false unless Hash.equal?(value.class) && value.size * 2 == items.size |
| 76 | ||
| 77 | 1276 | i = 0 |
| 78 | 1276 | value.each do |k, v| |
| 79 | 2783 | key = items[i] |
| 80 | 2783 | item = items[i + 1] |
| 81 | 2783 | return false unless key.equal?(k) || matches?(key, k) |
| 82 | 2777 | return false unless item.equal?(v) || matches?(item, v) |
| 83 | 2735 | i += 2 |
| 84 | end | |
| 85 | ||
| 86 | 1228 | true |
| 87 | when Array | |
| 88 | 29 | return false unless Array.equal?(value.class) && value.size == snapshot.size |
| 89 | ||
| 90 | 21 | i = 0 |
| 91 | 21 | while i < snapshot.size |
| 92 | 37 | item = snapshot[i] |
| 93 | 37 | return false unless item.equal?(value[i]) || matches?(item, value[i]) |
| 94 | 33 | i += 1 |
| 95 | end | |
| 96 | ||
| 97 | 17 | true |
| 98 | when String | |
| 99 | 33 | String.equal?(value.class) && snapshot.eql?(value) && snapshot.encoding == value.encoding && !(Phlex::SGML::SafeObject === value) |
| 100 | when Phlex::SGML::SafeValue | |
| 101 | 15 | Phlex::SGML::SafeValue.equal?(value.class) && String.equal?(value.to_s.class) && snapshot.to_s.eql?(value.to_s) && snapshot.to_s.encoding == value.to_s.encoding |
| 102 | else | |
| 103 | 14 | snapshot.eql?(value) |
| 104 | end | |
| 105 | end | |
| 106 | end |