Class: LLMSpecs::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_specs/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, ttl: 86400) ⇒ Cache

Returns a new instance of Cache.



4
5
6
7
# File 'lib/llm_specs/cache.rb', line 4

def initialize(file, ttl: 86400)
  @file = file
  @ttl  = ttl
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  File.exist?(@file)
end

#fetchObject



9
10
11
12
# File 'lib/llm_specs/cache.rb', line 9

def fetch
  return read if valid?
  yield.tap { write it }
end

#fresh?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/llm_specs/cache.rb', line 30

def fresh?
  Time.now - File.mtime(@file) < @ttl
end

#readObject



14
15
16
# File 'lib/llm_specs/cache.rb', line 14

def read
  File.read(@file) if exist?
end

#valid?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/llm_specs/cache.rb', line 22

def valid?
  exist? && fresh?
end

#write(data) ⇒ Object



18
19
20
# File 'lib/llm_specs/cache.rb', line 18

def write(data)
  File.write(@file, data, mode: "wb")
end