From 2406bc74204af7ffd1ed64dd5cfb185dfb47e905 Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Wed, 12 May 2021 16:58:31 +0200 Subject: [PATCH] mitigate memory leak --- ledger.cr | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ledger.cr b/ledger.cr index b648346..b50d158 100644 --- a/ledger.cr +++ b/ledger.cr @@ -3,23 +3,28 @@ require "csv" # Crystal wrapper module for calling ledger class Ledger + @last_mtime : Time + def initialize( binary : String = "ledger", ledger_file : String = ENV[ "LEDGER_FILE" ] ||= "${ENV[ \"HOME\" ]}/org/comptes.ledger" ) @binary = binary @file = ledger_file + @last_mtime = File.info(@file).modification_time @cache = Hash(String, String).new end def run( options : String, command : String = "", command_parameters : String = "" ) : String 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 ) + if @last_mtime < mtime || !@cache.has_key?( command ) + @last_mtime = mtime - @cache[ key ] + @cache[ command ] = `#{command}` + end + + @cache[ command ] end def version : String