-
Notifications
You must be signed in to change notification settings - Fork 0
Integration steps
To integrate Cache-Only, you need to first grab the jar as a dependency.
<dependency>
<groupId>com.github.nagyesta</groupId>
<artifactId>cache-only</artifactId>
<version>RELEASE</version>
</dependency>
implementation 'com.github.nagyesta:cache-only:+'
This interface is the piece which will be calling the origin service using each batch request we have in case cache did not have the data we hoped to get from it. BatchServiceCaller can be very easy to implement if you don't want to rely on the partitioning features provided. Ultimately it can be done by implementing a single method
/**
* Calls the real batch service with a single batch.
* The number of items in the batch is guaranteed to be lower or equal to {@link #maxPartitionSize()}.
*
* @param batchRequest The batch request we need to send.
* @return The batch response we received.
* @throws BatchServiceException In case the batch service failed to resolve the response due to an error.
* Should not be used if the response is empty because items are not found.
*/
BS callBatchService(BR batchRequest) throws BatchServiceException;
BatchRequestTransformer lets us simply define how we can split and merge requests by providing these methods:
/**
* Splits the batch request into the small partial requests used for caching.
*
* @param batchRequest The batch request containing all information needed for
* the partial requests.
* @return A map of partial requests using the request Id to identify them.
*/
Map<I, P> splitToPartialRequest(B batchRequest);
/**
* Merges the partial request map (supposedly generated by a previous split)
* into a batch request.
*
* @param requestMap The map containing the partial requests.
* @return The merged batch request.
*/
B mergeToBatchRequest(Map<I, P> requestMap);
Since v1.2.0
there are a number of generic request transformers at your disposal. Please find them
here
BatchResponseTransformer is providing the same functionality for the reponses using these methods:
/**
* Splits the batch response into the small partial responses used for caching.
*
* @param batchResponse The batch response containing all information received
* in response to the batch request.
* @return A map of partial response using the request Id to identify them.
*/
Map<I, P> splitToPartialResponse(B batchResponse);
/**
* Merges the partial response map (supposedly generated by a previous split or
* obtained from the cache) into a batch response.
*
* @param entityMap The map containing the partial responses.
* @return The merged batch response.
*/
B mergeToBatchResponse(Map<I, P> entityMap);
Since v1.2.0
Cache-Only ships with some generic response transformers. Please find them
here
The methods provided by PartialCacheSupport define an easy way to cache each part separately:
/**
* The name of the cache we will use for caching this pair.
*
* @return The cache name
*/
String cacheName();
/**
* Returns the class of the cached entity.
*
* @return The cached class
*/
Class<PS> getEntityClass();
/**
* Converts a partial request to a cache key.
*
* @param partialRequest The partial request.
* @return The cache key
*/
CacheKey<C, I> toCacheKey(PR partialRequest);
/**
* Returns the cache manager instance used for this caching operation.
*
* @return the cache manager
*/
CacheManager getCacheManager();
You can simply instantiate a properly parameterized version of DefaultCacheServiceTemplate to glue things together.
At this point you should see how this will work. Maybe you should write some cool tests and make sure it works.
Good luck!