Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0d12109

Browse files
committed
Added Suppliers.memoize post
1 parent6d2dbdf commit0d12109

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/post/PostData.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,21 @@ public class PostData {
631631
))
632632
.build()
633633
);
634+
posts.add(PostRaw.builder()
635+
.postId(905411988100628470L)
636+
.title("Lazy loading and caching objects in Java with Guava's Suppliers.memoize")
637+
.metaDesc("Use Guava's Suppliers.memoize to lazy load and cache objects. Use Suppliers.memoizeWithExpiration to have an expireable cached object in Java.")
638+
.dateCreated(LocalDateTime.parse("2017-09-06T01:15:30"))
639+
.dateUpdated(LocalDateTime.parse("2017-09-06T01:15:30"))
640+
.javaLibs(Lists.newArrayList(JavaLib.Guava))
641+
.tags(Lists.newArrayList(Tags.Caching))
642+
.gitFileReferences(Lists.newArrayList(
643+
FileReference.stubbornJava(
644+
"suppliers",
645+
"stubbornjava-examples/src/main/java/com/stubbornjava/examples/common/SuppliersExamples.java")
646+
))
647+
.build()
648+
);
634649
}
635650

636651
publicstaticList<PostRaw>getPosts() {

‎stubbornjava-webapp/src/main/java/com/stubbornjava/webapp/post/Tags.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class Tags {
2424
publicstaticfinalTagSEO =addTag(newTag(880770018813220697L,"SEO"));
2525
publicstaticfinalTagMicroservice =addTag(newTag(897667693917183622L,"Microservice"));
2626
publicstaticfinalTagMonolith =addTag(newTag(897667693920667427L,"Monolith"));
27+
publicstaticfinalTagCaching =addTag(newTag(905411988101360294L,"Caching"));
2728

2829
privatestaticTagaddTag(Tagtag) {
2930
TAGS.add(tag);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<divclass="anchored-md">
2+
{{#assign"markdown"}}
3+
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.
4+
5+
## Suppliers.memoize
6+
`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.
7+
8+
{{>templates/src/widgets/code/code-snippetfile=supplierssection=suppliers.sections.memoize}}
9+
<preclass="line-numbers"><codeclass="language-text">2017-09-06 08:50:58.069 [main] INFO c.s.e.common.SuppliersExamples - Memoized
10+
2017-09-06 08:50:58.156 [main] INFO c.s.e.common.SuppliersExamples - supplying
11+
2017-09-06 08:50:58.157 [main] INFO c.s.e.common.SuppliersExamples - hello world
12+
2017-09-06 08:50:58.157 [main] INFO c.s.e.common.SuppliersExamples - hello world</code></pre>
13+
14+
## Suppliers.memoizeWithExpiration
15+
`Suppliers.memoizeWithExpiration` is also straight forward. It allows us to memoize a value from a given `Supplier` but have it update anytime we exceed the expiration time. This is a great caching mechanism for any data you know changes infrequently. A minor drawback is if the operation is expensive you may see a hiccup every time the object needs to be reloaded. Often times this is not a major concern. If it is an issue you can investigate refreshing the object asynchronously with a background thread. `Suppliers.memoizeWithExpiration` is used to cache the scraped results for our [HTML / CSS Themes](https://www.stubbornjava.com/best-selling-html-css-themes-and-website-templates) page and can be seen in the [Web scraping in Java with jsoup and OkHttp](/posts/web-scraping-in-java-using-jsoup-and-okhttp#theme-service-layer).
16+
17+
{{>templates/src/widgets/code/code-snippetfile=supplierssection=suppliers.sections.memoizeWithExpiration}}
18+
<preclass="line-numbers"><codeclass="language-text">2017-09-06 08:50:58.157 [main] INFO c.s.e.common.SuppliersExamples - Memoized with Expiration
19+
2017-09-06 08:50:58.171 [main] INFO c.s.e.common.SuppliersExamples - supplying
20+
2017-09-06 08:50:58.171 [main] INFO c.s.e.common.SuppliersExamples - hello world
21+
2017-09-06 08:50:58.171 [main] INFO c.s.e.common.SuppliersExamples - hello world
22+
2017-09-06 08:50:58.171 [main] INFO c.s.e.common.SuppliersExamples - sleeping
23+
2017-09-06 08:50:58.272 [main] INFO c.s.e.common.SuppliersExamples - supplying
24+
2017-09-06 08:50:58.272 [main] INFO c.s.e.common.SuppliersExamples - hello world
25+
2017-09-06 08:50:58.272 [main] INFO c.s.e.common.SuppliersExamples - hello world
26+
2017-09-06 08:50:58.272 [main] INFO c.s.e.common.SuppliersExamples - sleeping
27+
2017-09-06 08:50:58.377 [main] INFO c.s.e.common.SuppliersExamples - supplying
28+
2017-09-06 08:50:58.377 [main] INFO c.s.e.common.SuppliersExamples - hello world
29+
2017-09-06 08:50:58.377 [main] INFO c.s.e.common.SuppliersExamples - hello world</code></pre>
30+
31+
{{/assign}}
32+
{{mdmarkdown}}
33+
</div>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp