Silly question: How do I make a cache registry

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Silly question: How do I make a cache registry

Göran Krampe
Hey!

Say I want to keep a Dictionary with say UUIDs as keys and objects as
values. And I hand out the UUIDs to an external program as "object
handles". But if an object gets garbage collected I want the registry to
loose that association. How?

WeakValueDictionary only looses the value, not the whole entry.

Sure, I can use finalization etc, but shouldn't there be a
WeakDictionary that can do this?

regards, Göran

Reply | Threaded
Open this post in threaded view
|

Re: Silly question: How do I make a cache registry

marcel.taeumel (old)
Hi.

Use this:

   WeakArray addWeakDependent: myDict.

There is a process called "WeakArray finalization process" which calls #finalizeValues on its dependents frequently. For WeakValueDictionary that would be a removal of the key that got "nil".

Bye,
Marcel
Reply | Threaded
Open this post in threaded view
|

Re: Silly question: How do I make a cache registry

Levente Uzonyi-2
On Tue, 15 Mar 2011, Marcel Taeumel wrote:

> Hi.
>
> Use this:
>
>   WeakArray addWeakDependent: myDict.
>
> There is a process called "WeakArray finalization process" which calls
> #finalizeValues on its dependents frequently. For WeakValueDictionary that
> would be a removal of the key that got "nil".

This is a pretty bad idea, because sooner or later the dictionary will be
invalid, because of concurrent access to it (your process and the
finalization process).

Also WeakValueDictionary doesn't implement #finalizeValues.

The right solution is a WeakValueDictionary which is protected from
concurrent access + registering each stored object in a WeakRegistry.

I made a wrapper object based on Igor's Stratified proxies
(http://code.google.com/p/pharo/issues/detail?id=3648 ) which encapsulates
another object and ensures that only one process can access the wrapped
object at a time. This could help with the first part.


Levente

>
> Bye,
> Marcel
>
> --
> View this message in context: http://forum.world.st/Silly-question-How-do-I-make-a-cache-registry-tp3357027p3357049.html
> Sent from the Squeak - Dev mailing list archive at Nabble.com.
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Silly question: How do I make a cache registry

Chris Muller-3
In reply to this post by Göran Krampe
Hi Göran, you can find auto-cleaning weak-key and weak-value
dictionary's in the MaBase package on SqueakMap which were developed
by Igor.  See the MaDictionary hierarchy.

 - Chris


2011/3/15 Göran Krampe <[hidden email]>:

> Hey!
>
> Say I want to keep a Dictionary with say UUIDs as keys and objects as
> values. And I hand out the UUIDs to an external program as "object handles".
> But if an object gets garbage collected I want the registry to loose that
> association. How?
>
> WeakValueDictionary only looses the value, not the whole entry.
>
> Sure, I can use finalization etc, but shouldn't there be a WeakDictionary
> that can do this?
>
> regards, Göran
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Silly question: How do I make a cache registry

marcel.taeumel (old)
In reply to this post by Levente Uzonyi-2
Oh, I didn't realize that. It worked pretty well in one of my projects, but you are right. Using my approach, the dictionary may get corrupted at any time.

Thanks for the info :)

Marcel