lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Oct 21, 2009, at 7:25 PM, Wim Couwenberg wrote:

If you cannot reproduce a
certain input, it's pointless to keep the output.

Right.

But to follow up on Mark's example, given a path, io.open():read(), and it's result, how would one cache the content of a file?

local cache = setmetatable( {}, { __mode = 'k' } )

local function content( aPath )
local aContent = cache[ aPath ] or io.open( aPath, 'rb' ):read( '*a' )
	
    cache[ aPath ] = aContent

    return aContent
end

The above seems like a memory leak, as 'cache[ aPath ] = aContent' will hold everything forever.
Thoughts?