Class: Coradoc::AsciiDoc::Parser::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/asciidoc/parser/cache.rb

Overview

Parser cache for optimizing repeated parses of the same content

This is an opt-in feature that caches parse results based on content hashing. Useful for IDE integrations, watch mode, or any scenario where the same content is parsed multiple times.

Examples:

Basic usage

cache = Coradoc::AsciiDoc::Parser::Cache.new
ast = cache.fetch_or_parse(content) { Parser::Base.parse(content) }

With size limit

cache = Coradoc::AsciiDoc::Parser::Cache.new(max_size: 100)

Global cache (use with caution)

Coradoc::AsciiDoc::Parser::Cache.global do |c|
  c.fetch_or_parse(content) { Parser::Base.parse(content) }
end

Constant Summary collapse

DEFAULT_MAX_SIZE =

Default maximum cache size

50

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ Cache

Initialize a new cache

Parameters:

  • max_size (Integer) (defaults to: DEFAULT_MAX_SIZE)

    Maximum number of entries to cache



62
63
64
65
66
67
# File 'lib/coradoc/asciidoc/parser/cache.rb', line 62

def initialize(max_size: DEFAULT_MAX_SIZE)
  @max_size = max_size
  @cache = {}
  @access_order = []
  @mutex = Mutex.new
end

Class Attribute Details

.globalCache?

Get the global cache instance

Returns:

  • (Cache, nil)

    The global cache instance



31
32
33
# File 'lib/coradoc/asciidoc/parser/cache.rb', line 31

def self.global
  @global ||= nil
end

Class Method Details

.clear_global!Object

Clear the global cache



56
57
58
# File 'lib/coradoc/asciidoc/parser/cache.rb', line 56

def self.clear_global!
  @global&.clear
end

.with_global(max_size: DEFAULT_MAX_SIZE) {|Cache| ... } ⇒ Object

Execute a block with a global cache

Parameters:

  • max_size (Integer) (defaults to: DEFAULT_MAX_SIZE)

    Maximum cache entries

Yields:

  • (Cache)

    The cache instance

Returns:

  • (Object)

    The block result



45
46
47
48
49
50
51
52
53
# File 'lib/coradoc/asciidoc/parser/cache.rb', line 45

def self.with_global(max_size: DEFAULT_MAX_SIZE)
  previous = @global
  @global = new(max_size: max_size)
  begin
    yield @global
  ensure
    @global = previous
  end
end

Instance Method Details

#cached?(content) ⇒ Boolean

Check if content is cached

Parameters:

  • content (String)

    The content to check

Returns:

  • (Boolean)

    True if cached



103
104
105
106
# File 'lib/coradoc/asciidoc/parser/cache.rb', line 103

def cached?(content)
  key = content_hash(content)
  mutex.synchronize { cache.key?(key) }
end

#clearObject

Clear the cache



121
122
123
124
125
126
# File 'lib/coradoc/asciidoc/parser/cache.rb', line 121

def clear
  mutex.synchronize do
    cache.clear
    access_order.clear
  end
end

#fetch_or_parse(content) { ... } ⇒ Object

Fetch a cached result or parse and cache

Parameters:

  • content (String)

    The content to parse

Yields:

  • Block to execute if cache miss

Returns:

  • (Object)

    The parsed AST



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/coradoc/asciidoc/parser/cache.rb', line 74

def fetch_or_parse(content)
  key = content_hash(content)

  mutex.synchronize do
    if cache.key?(key)
      # Move to end of access order (most recently used)
      access_order.delete(key)
      access_order.push(key)
      return cache[key]
    end
  end

  # Parse outside the lock for concurrency
  result = yield if block_given?

  mutex.synchronize do
    # Evict oldest if at capacity
    evict_oldest if cache.size >= max_size

    cache[key] = result
    access_order.push(key)
  end

  result
end

#sizeInteger

Get the current cache size

Returns:

  • (Integer)

    Number of cached entries



130
131
132
# File 'lib/coradoc/asciidoc/parser/cache.rb', line 130

def size
  mutex.synchronize { cache.size }
end

#statsHash

Get cache statistics

Returns:

  • (Hash)

    Statistics including size, max_size, hits, misses



110
111
112
113
114
115
116
117
118
# File 'lib/coradoc/asciidoc/parser/cache.rb', line 110

def stats
  mutex.synchronize do
    {
      size: cache.size,
      max_size: max_size,
      keys: access_order.dup
    }
  end
end