Class: Ukiryu::Definition::DefinitionCache::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/ukiryu/definition/definition_cache.rb

Overview

Cache entry structure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, mtime: nil, source_key: nil) ⇒ Entry

Returns a new instance of Entry.



19
20
21
22
23
24
# File 'lib/ukiryu/definition/definition_cache.rb', line 19

def initialize(definition, mtime: nil, source_key: nil)
  @definition = definition
  @mtime = mtime
  @loaded_at = Time.now
  @source_key = source_key || generate_source_key(definition)
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



17
18
19
# File 'lib/ukiryu/definition/definition_cache.rb', line 17

def definition
  @definition
end

#loaded_atObject (readonly)

Returns the value of attribute loaded_at.



17
18
19
# File 'lib/ukiryu/definition/definition_cache.rb', line 17

def loaded_at
  @loaded_at
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



17
18
19
# File 'lib/ukiryu/definition/definition_cache.rb', line 17

def mtime
  @mtime
end

#source_keyObject (readonly)

Returns the value of attribute source_key.



17
18
19
# File 'lib/ukiryu/definition/definition_cache.rb', line 17

def source_key
  @source_key
end

Instance Method Details

#refreshEntry

Refresh the entry

Returns:

  • (Entry)

    refreshed entry



46
47
48
49
50
51
52
53
54
55
# File 'lib/ukiryu/definition/definition_cache.rb', line 46

def refresh
  if @definition.respond_to?(:load_definition)
    # Reload from metadata
    new_def = @definition.load_definition
    Entry.new(new_def, mtime: new_def.mtime, source_key: @source_key)
  else
    # Can't refresh, return as-is
    self
  end
end

#stale?(ttl: DEFAULT_TTL) ⇒ Boolean

Check if entry is stale

Parameters:

  • ttl (Integer) (defaults to: DEFAULT_TTL)

    time to live in seconds

Returns:

  • (Boolean)

    true if entry is stale



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ukiryu/definition/definition_cache.rb', line 30

def stale?(ttl: DEFAULT_TTL)
  # Check TTL
  return true if Time.now - @loaded_at > ttl

  # Check mtime for file-based definitions
  if @mtime && @definition.respond_to?(:path)
    path = @definition.path
    return true if path && File.exist?(path) && File.mtime(path) > @mtime
  end

  false
end