[MGNLCACHE-69] Implement javax.inject.Provider which injects a @Named cache Created: 13/Aug/14  Updated: 18/Jun/15  Resolved: 09/Sep/14

Status: Closed
Project: Cache Modules
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Neutral
Reporter: Magnolia International Assignee: Magnolia International
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
relation
is related to MGNLCACHE-74 Implement a javax.inject.Provider for... Closed
Template:
Acceptance criteria:
Empty
Task DoR:
Empty

 Description   

With MGNLCACHE-55, one can cache arbitrary objects (that was already the case, but the API was awkward, and the cache could not be configured), but one still has to :

public class FooBar {
  private final Cache cache;
  @Inject
  public FooBar(CacheModule cacheModule) {
    // or keep a ref to module and retrieve the cache when needed
    this.cache = cacheModule.getCacheFactory().getCache("foo-cache");
  }
}

It'd be nice if one could do this instead:

public class FooBar {
  private final Cache cache;
  @Inject
  public FooBar(@Named("foo-cache") Cache cache) {
    this.cache = cache;
  }
}

Another approach (or additional) would let one inject an unnamed Cache, and the name would, by convention, be the class name of the injectee.

package zim.zam;

public class FooBar {
  private final Cache cache;
  @Inject
  public FooBar(Cache cache) {
    assert cache.getName().equals("zim.zam.FooBar");
    this.cache = cache;
  }
}


 Comments   
Comment by Magnolia International [ 08/Sep/14 ]

I thought Mycila could help with this, but I've failed miserably. Couldn't find a way for a Provider to know about they key it's being used for, or more generally the "context" in which it's being used. (we can't pre-bind named caches, since we don't know their names.

http://code.mycila.com/guice/#1-customizes-injection-annotations
GuiceyFruit supposedly has something similar, but haven't found more info either.

Comment by Magnolia International [ 08/Sep/14 ]

Maybe Guice's multi injection might be of inspiration (and/or useful for other purposes too)

Comment by Magnolia International [ 09/Sep/14 ]

There are possible ways to do this with Guice, but absolutely none that feels like it wouldn't be a giant hack.

Guice's main target is systems where you know all the bindings when a container is started. This isn't the case which caches (although by writing this I wonder how much of a hack it'd be to create a container per Cache).

The other problem is that I couldn't find a way to somehow pass the injectee, or information about it, to a Provider. (i.e to use the annotation or its value to create/get the cache)
https://github.com/google/guice/issues/27 and a whole bunch of follow-up issues all end up in "nope, won't do" or "well, we considered it but then nobody was interested so we abandoned the idea".

GuiceyFruit and Mycila seem to have what it takes to do it, but it smells so bad, and I couldn't figure it out. Their stuff seems based mostly on Guice listeners, and not applicable to constructor injection.

Generated at Sun Feb 11 23:51:54 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.