Skip to content

Integration steps

Esta Nagy edited this page Jan 28, 2021 · 2 revisions

1. Add the dependency

To integrate Cache-Only, you need to first grab the jar as a dependency.

Using Maven

<dependency>
  <groupId>com.github.nagyesta</groupId>
  <artifactId>cache-only</artifactId>
  <version>RELEASE</version>
</dependency>

Using Gradle

implementation 'com.github.nagyesta:cache-only:+'

2. Implement Cache-Only interfaces

BatchServiceCaller

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

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

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

PartialCacheSupport

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();

Last bits

You can simply instantiate a properly parameterized version of DefaultCacheServiceTemplate to glue things together.

3. Test it

At this point you should see how this will work. Maybe you should write some cool tests and make sure it works.

Good luck!