From 140ec7f9d52c3737d015df2d92e29aa50c6c7d87 Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Sat, 10 Apr 2021 17:39:24 +0200 Subject: [PATCH] memoization --- ledger.cr | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ledger.cr b/ledger.cr index d488f03..b648346 100644 --- a/ledger.cr +++ b/ledger.cr @@ -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