How do I safely use ThreadLocal caches in Actors?
My app ends up doing a lot of background processing via Actors, specifically loading Mapper instances and then doing some work upon them.  It's very repetitive and I'd like to cache some of these lookups across my Actor code.

I'd typically use a ThreadLocal for this.  However, since the thread initialization is handled by the Actor thread pool, it seems like the only place to initialize and subsequently clear the ThreadLocal would be in the actor's PartialFunction which receives incoming messages.

What I'm doing now is to create another method in my Actor, like this:

    override def aroundUpdates[T](fn: => T) : T = {
      clientCache.init {
        fn
      }
    }


Where the <code>init</code> method handles clearing the ThreadLocal in a finally block.  I don't like this approach because aroundUpdates only exists for the purpose of setting up the cache and it smells like a code smell.

Is there a better way to do this?

How do I safely use ThreadLocal caches in Actors?
===

My app ends up doing a lot of background processing via Actors, specifically loading Mapper instances and then doing some work upon them.  It's very repetitive and I'd like to cache some of these lookups across my Actor code.

I'd typically use a ThreadLocal for this.  However, since the thread initialization is handled by the Actor thread pool, it seems like the only place to initialize and subsequently clear the ThreadLocal would be in the actor's PartialFunction which receives incoming messages.

What I'm doing now is to create another method in my Actor, like this:

    override def aroundUpdates[T](fn: => T) : T = {
      clientCache.init {
        fn
      }
    }


Where the <code>init</code> method handles clearing the ThreadLocal in a finally block.  I don't like this approach because aroundUpdates only exists for the purpose of setting up the cache and it smells like a code smell.

Is there a better way to do this?
