Skip to content

Commit 0110de1

Browse files
committed
Forgot the supplier
1 parent 0d12109 commit 0110de1

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

stubbornjava-webapp/ui/src/posts/lazy-loading-and-caching-objects-in-java-with-guava-s-suppliers-memoize.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
{{#assign "markdown"}}
33
Lazy loading and caching are extremely useful tools in the developer toolbox, like most tools they can be often overused / abused so use them sparingly. Lazy loading can be useful when creating a singleton with any variety of double checked locking, static holder class, enum singleton pattern, ect. Lazy loading is also great for expensive operations that you only need to handle some of the time. Hibernate heavily utilizes lazy loading. Lazy loading can either be request scoped or in a more global scope. Google's Guava provides an extremely convenient way to create lazy loaded values as well as caching individual values with or without an expiration.
44

5+
## Supplier
6+
We will be using a very simple "hello world" `Supplier` that will log every time it is called.
7+
8+
{{> templates/src/widgets/code/code-snippet file=suppliers section=suppliers.sections.supplier}}
9+
10+
511
## Suppliers.memoize
612
`Suppliers.memoize` is a simple method that takes a `Supplier` and returns a new `Supplier` that caches the value returned from the supplied `Supplier.get()` method. This convenient helper allows us to turn any `Supplier` into a lazily loaded cached value. The returned `Supplier` is thread-safe backed with double checked locking and volatile fields. It even allows the original `Supplier` to be garbage collected once it has been called. If you are utilizing this pattern for code that is not multi-threaded it might be useful to make a non thread-safe version.
713

0 commit comments

Comments
 (0)