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.



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

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

Instance Method Details

#clearObject



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

def clear
  @memory_cache.clear
  @file_cache.clear
end

#get(file_path) ⇒ Object



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

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



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

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