Class: LLMSpecs::Cache
- Inherits:
-
Object
- Object
- LLMSpecs::Cache
- Defined in:
- lib/llm_specs/cache.rb
Instance Method Summary collapse
- #exist? ⇒ Boolean
- #fetch ⇒ Object
- #fresh? ⇒ Boolean
-
#initialize(file, ttl: 86400) ⇒ Cache
constructor
A new instance of Cache.
- #read ⇒ Object
- #valid? ⇒ Boolean
- #write(data) ⇒ Object
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
26 27 28 |
# File 'lib/llm_specs/cache.rb', line 26 def exist? File.exist?(@file) end |
#fetch ⇒ Object
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
30 31 32 |
# File 'lib/llm_specs/cache.rb', line 30 def fresh? Time.now - File.mtime(@file) < @ttl end |
#read ⇒ Object
14 15 16 |
# File 'lib/llm_specs/cache.rb', line 14 def read File.read(@file) if exist? end |
#valid? ⇒ 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 |