google/guice — pull request #879 by mcculls (2014-11-07 19:41:30+00:00)
Title: Proof of concept: Providers.cache(Binding)

**This is more of an FYI to give background why I was originally (mis)using Scopes.SINGLETON.scope.**
**On reflection this change doesn't pull enough weight to justify its addition as the use-case is narrow.**

The recent change to support per-injector singleton locks removed
the ability to take a prototype binding from the injector and use
`Scopes.SINGLETON.scope(...);` to create a local caching provider
that lazily caches the first result.

This POC introduces a new method Providers.cache(Binding) which
takes a binding and returns a provider that lazily caches the
initial result from the binding. This acts like a local cache
which is useful when integrating with third-party frameworks.

The same feature can be implemented outside of Guice, however it
would then use a different lock to the one used to synchronize
creation of singletons, and this could potentially lead to a
deadlock if you had a multi-threaded application with singleton
components that interacted with these caching providers for their
injected dependencies.