mirror of
https://github.com/gwenhael-le-moine/credger.git
synced 2024-12-27 09:58:20 +01:00
mitigate memory leak
This commit is contained in:
parent
d3ff3891c8
commit
2406bc7420
1 changed files with 9 additions and 4 deletions
13
ledger.cr
13
ledger.cr
|
@ -3,23 +3,28 @@ require "csv"
|
||||||
|
|
||||||
# Crystal wrapper module for calling ledger
|
# Crystal wrapper module for calling ledger
|
||||||
class Ledger
|
class Ledger
|
||||||
|
@last_mtime : Time
|
||||||
|
|
||||||
def initialize( binary : String = "ledger",
|
def initialize( binary : String = "ledger",
|
||||||
ledger_file : String = ENV[ "LEDGER_FILE" ] ||= "${ENV[ \"HOME\" ]}/org/comptes.ledger" )
|
ledger_file : String = ENV[ "LEDGER_FILE" ] ||= "${ENV[ \"HOME\" ]}/org/comptes.ledger" )
|
||||||
@binary = binary
|
@binary = binary
|
||||||
@file = ledger_file
|
@file = ledger_file
|
||||||
|
@last_mtime = File.info(@file).modification_time
|
||||||
|
|
||||||
@cache = Hash(String, String).new
|
@cache = Hash(String, String).new
|
||||||
end
|
end
|
||||||
|
|
||||||
def run( options : String, command : String = "", command_parameters : String = "" ) : String
|
def run( options : String, command : String = "", command_parameters : String = "" ) : String
|
||||||
command = "#{@binary} -f #{@file} #{options} #{command} #{command_parameters}"
|
command = "#{@binary} -f #{@file} #{options} #{command} #{command_parameters}"
|
||||||
STDERR.puts command
|
|
||||||
|
|
||||||
mtime = File.info(@file).modification_time
|
mtime = File.info(@file).modification_time
|
||||||
key = "#{mtime}#{command}"
|
if @last_mtime < mtime || !@cache.has_key?( command )
|
||||||
@cache[ key ] = `#{command}` unless @cache.has_key?( key )
|
@last_mtime = mtime
|
||||||
|
|
||||||
@cache[ key ]
|
@cache[ command ] = `#{command}`
|
||||||
|
end
|
||||||
|
|
||||||
|
@cache[ command ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def version : String
|
def version : String
|
||||||
|
|
Loading…
Reference in a new issue