https://github.com/jmnarloch/completable-future-cache
Caching CompletableFuture computations and their results
https://github.com/jmnarloch/completable-future-cache
Last synced: 2 months ago
JSON representation
Caching CompletableFuture computations and their results
- Host: GitHub
- URL: https://github.com/jmnarloch/completable-future-cache
- Owner: jmnarloch
- License: apache-2.0
- Created: 2016-01-29T21:35:30.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-02-01T16:01:53.000Z (over 9 years ago)
- Last Synced: 2025-01-07T13:14:51.487Z (4 months ago)
- Size: 64.5 KB
- Stars: 4
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CompletableFuture cache
> Caches CompletableFuture computations and their results
[](https://travis-ci.org/jmnarloch/completable-future-cache)
[](https://coveralls.io/github/jmnarloch/completable-future-cache?branch=master)## Setup
Add the module to your project:
```xml
io.jmnarloch
completable-future-cache
1.0.0-SNAPSHOT```
## Features
A specialized cache for storing the CompletableFuture computations and caching their results.
```
ExecutorService executor = Executors.newFixedThreadPool(20);
EvictableCompletableFutureCache cache = new EvictableCompletableFutureCache<>(executor, 10, TimeUnit.SECONDS);
```Typical usage scenario requires to supply a unit of work to the cache through `supply` method,
this will trigger the execution of the task and return it in a form of `CompletableFuture`.```
public class MongoUserRepository {private final MongoOperations mongoOperations;
private final CompletableFutureCache userCache;
...
public CompletableFuture findByEmail(String email) {
return userCache.supply(email, () -> queryByEmail(email));
}
}
```The cache is going to observe the future for completion. Once the future finishes processing the task, the result will
be wrapped into `CompletableFuture` through `CompletableFuture#completedFuture` and cached.Note: It's worth noticing that due to the asynchronous nature of the future the cache can not block and wait for the
value to be computed. The cache guarantees that as long as the value did not expired at most one task will
be processing the supplied unit of work, effectively using your resources.The consequences is that when multiple supply methods are going to consecutively invoked all of them will gain access to
the *same* instance of `CompletableFuture` that they can observe for completion.## License
Apache 2.0