You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
724 B
Lua
26 lines
724 B
Lua
3 years ago
|
--[=[
|
||
|
@c WeakCache x Cache
|
||
|
@mt mem
|
||
|
@d Extends the functionality of a regular cache by making use of weak references
|
||
|
to the objects that are cached. If all references to an object are weak, as they
|
||
|
are here, then the object will be deleted on the next garbage collection cycle.
|
||
|
]=]
|
||
|
|
||
|
local Cache = require('iterables/Cache')
|
||
|
local Iterable = require('iterables/Iterable')
|
||
|
|
||
|
local WeakCache = require('class')('WeakCache', Cache)
|
||
|
|
||
|
local meta = {__mode = 'v'}
|
||
|
|
||
|
function WeakCache:__init(array, constructor, parent)
|
||
|
Cache.__init(self, array, constructor, parent)
|
||
|
setmetatable(self._objects, meta)
|
||
|
end
|
||
|
|
||
|
function WeakCache:__len() -- NOTE: _count is not accurate for weak caches
|
||
|
return Iterable.__len(self)
|
||
|
end
|
||
|
|
||
|
return WeakCache
|