[Borges-users] Re: and some more on caching
Слепнев Владимир
slepnev_v at rambler.ru
Mon Apr 12 02:56:44 EDT 2004
Well, it seems I just wrote a StateRegistry that uses WeakRef's, not
Weak::.
I do not attach finalizers to anything, it's too buggy. Instead, I
delete references to GC'd objects when I iterate over the cache making
a snapshot.
require 'weakref'
class Borges::StateRegistry
def initialize
@wrefs = []
end
def register(obj)
wref = WeakRef.new(obj)
@wrefs << wref
end
##
# Restore the state of all objects in the registry from a
# previously recorded snapshot.
def restore_snapshot(snap)
snap.each do |obj, copy|
restore_object_from_snapshot(obj, copy)
end
end
##
# Returns a snapshot of the current state of all registered
# objects.
def snapshot
snapshot = {}
removed = []
gcWasDisabled = GC.disable
@wrefs.each do |wref|
if wref.weakref_alive? then
obj = wref.__getobj__
snapshot[obj] = obj.clone
else removed<<wref
end
end
GC.enable unless gcWasDisabled
@wrefs = @wrefs-removed
return snapshot
end
private
##
# Restore an individual object's state from a copy.
def restore_object_from_snapshot(obj, copy)
obj.instance_variables.each do |var|
obj.instance_variable_set(var, copy.instance_variable_get(var))
end
end
end
What do you think? At least it works with my app... SushiNet is still
buggy, but it's a SushiNet problem I think.
Vladimir Slepnev.
More information about the Borges-users
mailing list