Class: TRuby::DeclarationCache

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

Overview

Declaration file cache

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir: ".t-ruby-cache/declarations") ⇒ DeclarationCache

Returns a new instance of DeclarationCache.



263
264
265
266
# File 'lib/t_ruby/cache.rb', line 263

def initialize(cache_dir: ".t-ruby-cache/declarations")
  @file_cache = FileCache.new(cache_dir: cache_dir, max_age: 86_400) # 24 hours
  @memory_cache = MemoryCache.new(max_size: 200)
end

Instance Method Details

#clearObject



299
300
301
302
# File 'lib/t_ruby/cache.rb', line 299

def clear
  @memory_cache.clear
  @file_cache.clear
end

#get(file_path) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/t_ruby/cache.rb', line 268

def get(file_path)
  # Check modification time
  return nil unless File.exist?(file_path)

  mtime = File.mtime(file_path).to_i
  cache_key = "#{file_path}:#{mtime}"

  # Try memory first
  result = @memory_cache.get(cache_key)
  return result if result

  # Try file cache
  result = @file_cache.get(cache_key)
  if result
    @memory_cache.set(cache_key, result)
    return result
  end

  nil
end

#set(file_path, declarations) ⇒ Object



289
290
291
292
293
294
295
296
297
# File 'lib/t_ruby/cache.rb', line 289

def set(file_path, declarations)
  mtime = File.mtime(file_path).to_i
  cache_key = "#{file_path}:#{mtime}"

  @memory_cache.set(cache_key, declarations)
  @file_cache.set(cache_key, declarations)

  declarations
end