lua-users home
lua-l archive

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


From my opinion, implement a data cache using weak table is not a good idea. If you do so, your application will have no control over when the data in cache will be garbage collected. Even if the data is access very frequently by clients, it may still be collected immediately after all references are released. You should implement the cache in a regular table and implement a replacement algorithm to manage the data in cache.

Regards
Long

Mark Hamburg 写道:
I understand the theoretical arguments for treating strings as values with respect to weak tables, but are there any practical benefits? It would seem, for example, to get in the way of caching the results of file reads a la:

fileCache = setmetatable( { }, {
__mode = 'kv',
__index = function( t, k )
local v
local f = io.open( k )
if f then
v = f:read( "*a" )
t[ k ] = v
f:close()
end
return v
end } )

Mark