So you have 2 caching levels (cache, request)? I recommend making a
request-scoped container object, inject that instead. then that can
look up the cache and store it in a map locally if needed:

@RequestScoped
public class MyCache {
   private final Map<Class<Widget>, Widget> cache = new ...;

   @Inject Cache mainCache; //the main cache

   public Widget getFromCacheOrMakeNew(Class<Widget> cl) {
      if (cache.containsKey(cl)) return cache.get(cl);

      Widget w = cache.get(cl);
      mainCache.put(cl, w);

      return w;
   }
}

Then inject MyCache into your servlet.

Dhanji.

On Thu, Dec 4, 2008 at 5:20 AM, wolffiex <[EMAIL PROTECTED]> wrote:
>
> Hi all,
> I'm just starting to use scopes, and I'm wondering how to do this:
>
> I have an object which can sometimes be cached. I want to consult the
> cache and/or create a new object and then put that in the request
> scope for DI. So I have something like this
>
>    public void doGet(HttpServletRequest request, HttpServletResponse
> response)
>            throws ServletException {
>        Widget widget = getFromCacheOrMakeNew(request);
>
>        //now bind this widget instance to request scope...
>
>
> I naively tried this kind of thing:
>
>        ServletScopes.REQUEST.scope(Key.get(Widget.class), new
> Provider<Widget>(){
>            public Widget get() {
>                return widget;
>            }
>        });
>
> But guice is still looking for a constructor for Widget. Any guidance
> here? I think that really what's going on is that Widget lives in a
> custom scope, but I'd like to avoid implementing one if possible.
>
> TIA,
> A
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to