On Mon, Jul 23, 2012 at 12:25 PM, Leigh Klotz, Jr. <[email protected]>
wrote:
> @LazySingleton @Provides CachedThings cachedThings(DB db) {
>> Map<String, String> cache = readCache(db);
>> return new CachedThings(cache);
>> }
>>
>
> This last seems like the nicest approach because you get to keep the cache
>> field final, and you keep the reading separate from the access.
>
>
> It's really clear what it does and it's fairly concise, but it fails to
> satisfy the case where CachedThings injects other values.
>
Well, it could still be done:
@LazySingleton @Provides CachedThings cachedThings(DB db,
MembersInjector<CachedThings> membersInjector) {
Map<String, String> cache = readCache(db);
CachedThings result = new CachedThings(cache);
membersInjector.injectMembers(result);
return result;
}
but as you say:
It also loses a bit on conciseness since the @Provides method is outside
> the definition of CachedThings. I'd prefer to have this logic in one place.
So readCache in the constructor is probably the way to go for now. However:
I guess in a single-threaded scope such as @RequestScoped, it's safe to do
> the work on demand in the first access, but it's still clunky to write your
> own cache.
>
Is Guava's LoadingCache an option?
--tim
--
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.