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.


But to follow up on Mark's example, given a path,, 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 aPath, 'rb' ):read( '*a' )
    cache[ aPath ] = aContent

    return aContent

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