mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-06-03 14:12:38 +00:00
docs: Content SEO updates (#2990)
* update yaml frontmatter format * update abstract document * update abstract factory * use the new pattern template * acyclic visitor seo * adapter seo * ambassador seo * acl seo * aaa seo * async method invocation seo * balking seo * bridge seo * builder seo * business delegate and bytecode seo * caching seo * callback seo * chain seo * update headings * circuit breaker seo * client session + collecting parameter seo * collection pipeline seo * combinator SEO * command seo * cqrs seo * commander seo * component seo * composite seo * composite entity seo * composite view seo * context object seo * converter seo * crtp seo * currying seo * dao seo * data bus seo * data locality seo * data mapper seo * dto seo * decorator seo * delegation seo * di seo * dirty flag seo * domain model seo * double buffer seo * double checked locking seo * double dispatch seo * dynamic proxy seo * event aggregator seo * event-based asynchronous seo * eda seo * event queue seo * event sourcing seo * execute around seo * extension objects seo * facade seo * factory seo * factory kit seo * factory method seo * fanout/fanin seo * feature toggle seo * filterer seo * fluent interface seo * flux seo * flyweight seo * front controller seo * function composition seo * game loop seo * gateway seo * guarded suspension seo * half-sync/half-async seo * health check seo * hexagonal seo * identity map seo * intercepting filter seo * interpreter seo * iterator seo * layers seo * lazy loading seo * leader election seo * leader/followers seo * lockable object seo * rename and add seo for marker interface * master-worker seo * mediator seo * memento seo * metadata mapping seo * microservice aggregator seo * api gw seo * microservices log aggregration seo * mvc seo * mvi seo * mvp seo * mvvm seo * monad seo * monitor seo * monostate seo * multiton seo * mute idiom seo * naked objects & notification seo * null object seo * object mother seo * object pool seo * observer seo * optimistic locking seo * page controller seo * page object seo * parameter object seo * partial response seo * pipeline seo * poison pill seo * presentation model seo * private class data seo * producer-consumer seo * promise seo * property seo * prototype seo * proxy seo * queue-based load leveling seo * reactor seo * registry seo * repository seo * RAII seo * retry seo * role object seo * saga seo * separated interface seo * serialized entity seo * serialized lob seo * servant seo * server session seo * service layer seo * service locator seo * service to worker seo * sharding seo * single table inheritance seo * singleton seo * spatial partition seo * special case seo * specification seo * state seo * step builder seo * strangler seo * strategy seo * subclass sandbox seo * table module seo * template method seo * throttling seo * tolerant reader seo * trampoline seo * transaction script seo * twin seo * type object seo * unit of work seo * update method seo * value object seo * version number seo * virtual proxy seo * visitor seo * seo enhancements * seo improvements * SEO enhancements * SEO improvements * SEO additions * SEO improvements * more SEO improvements * rename hexagonal + SEO improvements * SEO improvements * more SEO stuff * SEO improvements * SEO optimizations * SEO enhancements * enchance SEO * improve SEO * SEO improvements * update headers
This commit is contained in:
+24
-18
@@ -1,28 +1,30 @@
|
||||
---
|
||||
title: Caching
|
||||
title: "Caching Pattern in Java: Accelerating Data Access Speeds"
|
||||
shortTitle: Caching
|
||||
description: "Learn how to optimize performance with the Java Caching Design Pattern. Explore various caching strategies, real-world examples, and implementation techniques for efficient resource management."
|
||||
category: Performance optimization
|
||||
language: en
|
||||
tag:
|
||||
- Caching
|
||||
- Data access
|
||||
- Performance
|
||||
- Resource management
|
||||
- Caching
|
||||
- Data access
|
||||
- Performance
|
||||
- Resource management
|
||||
---
|
||||
|
||||
## Intent
|
||||
|
||||
The caching pattern avoids expensive re-acquisition of resources by not releasing them immediately after use. The resources retain their identity, are kept in some fast-access storage, and are re-used to avoid having to acquire them again.
|
||||
|
||||
## Also known as
|
||||
|
||||
* Cache
|
||||
* Temporary Storage
|
||||
|
||||
## Explanation
|
||||
## Intent of Caching Design Pattern
|
||||
|
||||
The Java Caching Design Pattern is crucial for performance optimization and resource management. It involves various caching strategies such as write-through, read-through, and LRU cache to ensure efficient data access. The caching pattern avoids expensive re-acquisition of resources by not releasing them immediately after use. The resources retain their identity, are kept in some fast-access storage, and are re-used to avoid having to acquire them again.
|
||||
|
||||
## Detailed Explanation of Caching Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
> A real-world example of the Caching design pattern can be seen in a library's catalog system. When patrons frequently search for popular books, the system can cache the results of these searches. Instead of querying the database every time a user searches for a popular book, the system quickly retrieves the results from the cache. This reduces the load on the database and provides faster response times for users, enhancing their overall experience. However, the system must also ensure that the cache is updated when new books are added or existing ones are checked out, to maintain accurate information.
|
||||
> A real-world example of the Caching Design Pattern in Java is a library's catalog system. By caching frequently searched book results, the system reduces database load and enhances performance. When patrons frequently search for popular books, the system can cache the results of these searches. Instead of querying the database every time a user searches for a popular book, the system quickly retrieves the results from the cache. This reduces the load on the database and provides faster response times for users, enhancing their overall experience. However, the system must also ensure that the cache is updated when new books are added or existing ones are checked out, to maintain accurate information.
|
||||
|
||||
In plain words
|
||||
|
||||
@@ -32,7 +34,9 @@ Wikipedia says
|
||||
|
||||
> In computing, a cache is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere. A cache hit occurs when the requested data can be found in a cache, while a cache miss occurs when it cannot. Cache hits are served by reading data from the cache, which is faster than recomputing a result or reading from a slower data store; thus, the more requests that can be served from the cache, the faster the system performs.
|
||||
|
||||
**Programmatic Example**
|
||||
## Programmatic Example of Caching Pattern in Java
|
||||
|
||||
In this programmatic example, we demonstrate different Java caching strategies, including write-through, write-around, and write-behind, using a user account management system.
|
||||
|
||||
A team is working on a website that provides new homes for abandoned cats. People can post their cats on the website after registering, but all the new posts require approval from one of the site moderators. The user accounts of the site moderators contain a specific flag and the data is stored in a MongoDB database. Checking for the moderator flag each time a post is viewed becomes expensive, and it's a good idea to utilize caching here.
|
||||
|
||||
@@ -66,7 +70,7 @@ public interface DbManager {
|
||||
}
|
||||
```
|
||||
|
||||
In the example, we are demonstrating various different caching policies
|
||||
In the example, we are demonstrating various different caching policies. The following caching strategies are implemented in Java: Write-through, Write-around, Write-behind, and Cache-aside. Each strategy offers unique benefits for improving performance and reducing load on the database.
|
||||
|
||||
* Write-through writes data to the cache and DB in a single transaction
|
||||
* Write-around writes data immediately into the DB instead of the cache
|
||||
@@ -419,7 +423,9 @@ UserAccount(userId=003, userName=Adam, additionalInfo=He likes food.)
|
||||
17:00:56.314 [Thread-0] INFO com.iluwatar.caching.CacheStore -- # flushCache...
|
||||
```
|
||||
|
||||
## Applicability
|
||||
Implementing the Java Caching Design Pattern using various strategies like LRU cache and write-through caching significantly enhances application performance and scalability.
|
||||
|
||||
## When to Use the Caching Pattern in Java
|
||||
|
||||
Use the Caching pattern when
|
||||
|
||||
@@ -427,14 +433,14 @@ Use the Caching pattern when
|
||||
* In scenarios where the cost of recomputing or re-fetching data is significantly higher than storing and retrieving it from cache
|
||||
* For read-heavy applications with relatively static data or data that changes infrequently
|
||||
|
||||
## Known Uses
|
||||
## Real-World Applications of Caching Pattern in Java
|
||||
|
||||
* Web page caching to reduce server load and improve response time
|
||||
* Database query caching to avoid repeated expensive SQL queries
|
||||
* Caching results of CPU-intensive computations
|
||||
* Content Delivery Networks (CDNs) for caching static resources like images, CSS, and JavaScript files closer to the end users
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Caching Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -448,14 +454,14 @@ Trade-Offs:
|
||||
* Resource Utilization: Requires additional memory or storage resources to maintain the cache
|
||||
* Stale Data: There's a risk of serving outdated data if the cache is not properly invalidated or updated when the underlying data changes
|
||||
|
||||
## Related patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [Proxy](https://java-design-patterns.com/patterns/proxy/): Caching can be implemented using the Proxy pattern, where the proxy object intercepts requests and returns cached data if available
|
||||
* [Observer](https://java-design-patterns.com/patterns/observer/): Can be used to notify the cache when the underlying data changes, so that it can be updated or invalidated accordingly
|
||||
* [Decorator](https://java-design-patterns.com/patterns/decorator/): Can be used to add caching behavior to an existing object without modifying its code
|
||||
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Different caching strategies can be implemented using the Strategy pattern, allowing the application to switch between them at runtime
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Effective Java](https://amzn.to/4cGk2Jz)
|
||||
* [High Performance Browser Networking](https://amzn.to/3TiNNY4)
|
||||
|
||||
Reference in New Issue
Block a user