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

Christian Vest Hansen karmazilla at gmail.com
Wed Nov 3 07:48:05 EDT 2010


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.


More information about the Concurrency-interest mailing list