Class: Servus::Schema::Cache Private

Inherits:
Object
  • Object
show all
Defined in:
lib/servus/schema/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.

Memoized $ref resolutions, plus the generation counter that invalidates everything derived from them.

Entries are keyed by ref string and hold the resolved target of that ref, before any sibling properties are merged over it. That is what makes a single entry safe to share across every site that uses the ref: the target depends only on the ref string and the registry contents, and callers apply their own siblings afterwards with Hash#merge, which returns a new hash and never mutates the cached one.

The generation counter lets consumers that build on compiled schemas — Base, Event — memoize alongside the generation they compiled under and rebuild when it moves, with no dependency tracking.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

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.

Returns a new instance of Cache.



28
29
30
31
32
# File 'lib/servus/schema/cache.rb', line 28

def initialize
  @entries = {}
  @generation = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#generationInteger (readonly)

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.

Monotonic counter, advanced by #invalidate!.

Returns:

  • (Integer)


26
27
28
# File 'lib/servus/schema/cache.rb', line 26

def generation
  @generation
end

Instance Method Details

#invalidate!void

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.

This method returns an undefined value.

Discards every memoized resolution and advances #generation.



54
55
56
57
58
59
# File 'lib/servus/schema/cache.rb', line 54

def invalidate!
  @mutex.synchronize do
    @entries = {}
    @generation += 1
  end
end

#resolve(ref) ⇒ 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.

Returns the memoized resolution of ref, computing it on a miss.

A raising block leaves no entry behind, so a ref that failed part way through resolution is never cached in a half-built state.

Parameters:

  • ref (String)

    the ref string

Yield Returns:

  • (Object)

    the resolved target, computed on a miss

Returns:

  • (Object)

    the resolved target



42
43
44
45
46
47
48
49
# File 'lib/servus/schema/cache.rb', line 42

def resolve(ref)
  cached = @entries[ref]
  return cached unless cached.nil?

  resolved = yield
  @mutex.synchronize { @entries[ref] = resolved }
  resolved
end

#sizeInteger

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.

Number of memoized resolutions. Used by specs to assert that a repeated ref is expanded once rather than once per occurrence.

Returns:

  • (Integer)


65
66
67
# File 'lib/servus/schema/cache.rb', line 65

def size
  @entries.size
end