Class: Coradoc::AsciiDoc::Parser::Cache
- Inherits:
-
Object
- Object
- Coradoc::AsciiDoc::Parser::Cache
- 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.
Constant Summary collapse
- DEFAULT_MAX_SIZE =
Default maximum cache size
50
Class Attribute Summary collapse
-
.global ⇒ Cache?
Get the global cache instance.
Class Method Summary collapse
-
.clear_global! ⇒ Object
Clear the global cache.
-
.with_global(max_size: DEFAULT_MAX_SIZE) {|Cache| ... } ⇒ Object
Execute a block with a global cache.
Instance Method Summary collapse
-
#cached?(content) ⇒ Boolean
Check if content is cached.
-
#clear ⇒ Object
Clear the cache.
-
#fetch_or_parse(content) { ... } ⇒ Object
Fetch a cached result or parse and cache.
-
#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ Cache
constructor
Initialize a new cache.
-
#size ⇒ Integer
Get the current cache size.
-
#stats ⇒ Hash
Get cache statistics.
Constructor Details
#initialize(max_size: DEFAULT_MAX_SIZE) ⇒ Cache
Initialize a new 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
.global ⇒ Cache?
Get 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
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
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 |
#clear ⇒ Object
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
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 |
#size ⇒ Integer
Get the current cache size
130 131 132 |
# File 'lib/coradoc/asciidoc/parser/cache.rb', line 130 def size mutex.synchronize { cache.size } end |
#stats ⇒ Hash
Get cache statistics
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 |