memoization

This commit is contained in:
Gwenhael Le Moine 2021-04-10 17:39:24 +02:00
parent b031127e9c
commit 140ec7f9d5
No known key found for this signature in database
GPG key ID: FDFE3669426707A7

View file

@ -7,11 +7,19 @@ class Ledger
ledger_file : String = ENV[ "LEDGER_FILE" ] ||= "${ENV[ \"HOME\" ]}/org/comptes.ledger" )
@binary = binary
@file = ledger_file
@cache = Hash(String, String).new
end
def run( options : String, command : String = "", command_parameters : String = "" ) : String
STDERR.puts "#{@binary} -f #{@file} #{options} #{command} #{command_parameters}"
`#{@binary} -f #{@file} #{options} #{command} #{command_parameters}`
command = "#{@binary} -f #{@file} #{options} #{command} #{command_parameters}"
STDERR.puts command
mtime = File.info(@file).modification_time
key = "#{mtime}#{command}"
@cache[ key ] = `#{command}` unless @cache.has_key?( key )
@cache[ key ]
end
def version : String