[concurrency-interest] How to implement a self populating memoizer cache?

Christian Vest Hansen karmazilla at gmail.com
Wed Nov 3 08:44:16 EDT 2010


Oh, didn't spot that requirement at first.

If you wrap the get-code in a retry-if-we-got-null loop, then you just
remove the mapping if you think you no longer need it.

On Wed, Nov 3, 2010 at 13:26, √iktor Klang <viktor.klang at gmail.com> wrote:
> How do you do cache evictions/invalidations on top of that? Storing
> SoftRefs?
>
> On Wed, Nov 3, 2010 at 12:48 PM, Christian Vest Hansen
> <karmazilla at gmail.com> wrote:
>>
>> Here's a simple algorithm based on ConcurrentHashMap:
>>
>> Try .get'ing the value by the key.
>>  * If the value is a CountDownLatch, then wait on the latch, then get
>> again and return that value.
>>  * If the value is otherwise something non-null, then return it.
>>  * If the value is null, then create a new CountDownLatch with count
>> 1, and putIfAbsent on the key.
>>  ** If the value returned by putIfAbsent is null, then create our real
>> value, put it, and count the latch down.
>>  ** If the value is a latch, wait on it and return the result of getting
>> again.
>>  ** Otherwise return whatever you got.
>>
>> Note that this algorithm does not support nulls as values, so you'd
>> have to use a Null Object if you need to support nulls.
>>
>> On Wed, Nov 3, 2010 at 10:07, Nader Aeinehchi <nader at aeinehchi.com> wrote:
>> > Hello
>> >
>> > How can a self populating cache be implemented?
>> >
>> > Requirements:
>> >
>> > 1. the put(key,value) operation will be a memoizer (value =
>> > computer(computable)) where computable==key.
>> > 2. how can a remove operation be implemented?
>> > 3. the cache will be thread safe
>> > 4. the cache will have a good performance for up to upper medium
>> > contention
>> > (i.e. very high contention is not required)
>> > 5. preferrably a non-blocking algorithm
>> > 6. no need for timers,....
>> >
>> > In advance, thank you very much.
>> >
>> > _______________________________________________
>> > Concurrency-interest mailing list
>> > Concurrency-interest at cs.oswego.edu
>> > http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>> >
>>
>>
>>
>> --
>> Venlig hilsen / Kind regards,
>> Christian Vest Hansen.
>> _______________________________________________
>> Concurrency-interest mailing list
>> Concurrency-interest at cs.oswego.edu
>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
>
> --
> Viktor Klang,
> Code Connoisseur
> Work:   akka.io
> Code:   github.com/viktorklang
> Follow: twitter.com/viktorklang
> Read:   klangism.tumblr.com
>
>



-- 
Venlig hilsen / Kind regards,
Christian Vest Hansen.



More information about the Concurrency-interest mailing list