头图

Introduce Caching to Your Application

Contents of this article

  • What caching is and why you should care about it
  • How a cache works
  • How to cache your OData Call
  • How to configure the cache

Caches

Sometimes, the service call from your application to an external server is very expensive in terms of performance and latency. Once the critical response time for customers and customers is reached, further evaluation of the query data will make things worse.

In order to improve the responsiveness to users, the data requested within your application can usually be stored for subsequent calls. This can be achieved in such a way that for each request, the information previously stored by the application can be reused. This general behavior is called caching. The cache stores a copy of the information passing through it. In addition to improving responsiveness, the technical goal is to reduce the overall required bandwidth and processing requirements, thereby ultimately reducing server load and perceived latency. In this way, the amount of information that needs to be transmitted across the network can be reduced.

Caching is very important in various use cases. This is one of the reasons for the progress of our modern Internet experience, such as on-demand multimedia streaming and persistent cloud storage. Unlike web caches that save entire request and response documents, internal application caches are used to persist temporary data for multiple intended purposes. Whenever the calculation or retrieval cost of information is high, and its value on a certain input is needed more than once, caching should be considered.

How do they work

Caching usually works by requesting information from a given topic (called a key). If information for a given key was previously requested, stored at the time of request, and can now be read, a so-called "cache hit" will occur: the data can be found and will be loaded. A "cache miss" occurs when it cannot.

The most important aspect of a cache is its size and the life cycle of its items. Both should be restricted to use cases to avoid outdated states or disproportionate memory consumption in the application. When the application repeatedly reads larger data blocks from an external source, you can see the biggest impact of using the cache. In this case, the use of caching will significantly reduce the bandwidth required to transmit information.

The cache is suitable for:

  • You are willing to spend some memory to increase speed.
  • You hope that the key will sometimes be queried multiple times.
  • Your cache does not need to store more data than RAM can hold.
    (By default, the cache is local in a single run of the application. It does not store the data in a file, nor on an external server.)

If these requirements apply to your use case, we strongly recommend that you use the caching function provided by the SAP Cloud SDK in your application. Now that you understand why caching is useful, you will understand what the Cloud SDK provides to enable it.

Caching with SAP Cloud SDK

The Cloud SDK can easily cache your requests because it can handle most of the complexity in the background. This includes handling tenant-aware requests, which is essential in multi-tenant applications. If your request requires it, the SDK will automatically isolate tenant or principal level caches.

In the SAP Cloud SDK, JCache (JSR 107) is used as the underlying caching technology. In the examples in this article, you will use the JCache adapter Caffeine for this, but you can use any implementation you like. For Caffeine, add the following dependencies to your application pom.xml:

<dependency>
  <groupId>com.github.ben-manes.caffeine</groupId>
  <artifactId>jcache</artifactId>
  <scope>runtime</scope>
  <version>2.7.0</version>
</dependency>

Cache your OData call

Now that we have discussed why caching is important and how it can help us improve performance and responsiveness, it is time to introduce it to your application.

Now, to make our OData call cacheable, you will enhance ResilienceConfiguration and add CacheConfiguration to it.

Add the following lines at the end of the constructor of GetBusinessPartnerCommand:

./application/src/main/java/com/sap/cloud/sdk/tutorial/GetBusinessPartnersCommand.java

final ResilienceConfiguration.CacheConfiguration cacheConfig =
        ResilienceConfiguration.CacheConfiguration
                .of(Duration.ofSeconds(10))
                .withoutParameters();

myResilienceConfig.cacheConfiguration(cacheConfig);                         

As mentioned above, ResilienceConfiguration is used to integrate caching functions and is described in CacheConfiguration. There are two steps to your configuration:

a. Determine how long the object should be cached
b. Declare the parameters that need to be stored with the cached data

The first step is obviously necessary, because the data should only be stored for a limited time. The longer the cached information is kept, the more it becomes outdated. How long you want to cache data depends on your specific use case. How quickly do you expect information to become out of date? How often is the data accessed? The timeout sets a trade-off between data being up-to-date and application response.

Second, you specify the parameters to be cached along with the data. For the request to retrieve the list of business partners, no parameters are required, so you can use htoutParameters to build a cache. But suppose you want to obtain information about a specific business partner by passing the ID to the system. In order to cache such a request, the cache not only needs to remember the received result, but also the ID associated with it. In this case, you can use .withParameters(param1, param2, ..) to simply pass these parameters.

Feel free to test that subsequent requests respond faster than the first request. Deploy your application locally or in the cloud and access the list of business partners multiple times.


注销
1k 声望1.6k 粉丝

invalid