Caching may improve your application's performance and responsiveness to your consumers. The SAP Cloud SDK for Java provides abstractions for caching that make it easy to integrate caching into an application with almost no configuration overhead. Also it integrates with other SDK features and most importantly is tenant aware out of the box. The following article describes how to enable caching with the Cloud SDK for Java in your application.
The Cloud SDK relies on the
JCache SPI to create and manage cache instances.
JCache is a Service Provider Interface which requires an implementation of this interface to be present at runtime.
Be sure to that such an implementation is present on your classpath.
We recommend Caffeine which you can use by simply adding the below dependency to your application
Caching only works when a service provider or implementation for JCache is available at runtime. Otherwise you will face a runtime exception.
As described in our Resilience section the Cloud SDK allows to run any code in the context of one or more resilience patterns.
Caching is also a resilience pattern so you can leverage the same API to build and apply a
This builds a simple cache that will store values for one day and does not require any additional parameters. The builder pattern requires at least a duration and whether or not the cache requires parameters. The configuration options are explained further below.
This configuration must then be added to a
The above creates an otherwise empty
ResilienceConfiguration and thus only enabling caching.
Of course you may use other resilience patterns as well.
Since the cache configuration is part of a resilience configuration it is applied in the same way:
The above code will invoke
operation() only once since the result of the first invocation will be cached.
Only if the first call fails the second one would still happen.
Note that a specific cache instance is not yet created when configuration is created.
Instead it is only created when an operation is decorated or executed by the
The cache functionality is tenant aware by default. That means that by default cache entries created under a specific tenant will not be shared with other tenants. See the section on multi tenancy below.
The cache configuration offers 3 options:
- Cache Duration (Required)
- Expiration Strategy (Optional)
- Parameters (Optional)
Each cache configuration must at least specify a duration after which a cache expires. How this timeout is applied can be configured through the Expiration Strategy. By default a cache entry is cleared once the configured duration has passed since the entry was created.
Finally, parameters can be listed that further refine the caching functionality. Parameters values will be attached to cache key. When the cache is accessed only entries with matching parameter values will be returned.
So you can use parameters to separate cache entries as you see fit. Please note that there is no need to do this to achieve multi tenancy. The SDK is tenant aware by default as described below.
By default this caching functionality is tenant aware. If a tenant is available the SDK will create cache entries only for that specific tenant. Other tenants will not see the value cached for that specific tenant.
For this example assume that
config is a
ResilienceConfiguration that has a
CacheConfiguration in place:
This isolation is configured via the isolation mode of the resilience configuration.
So you can disable tenant aware caching by setting the isolation mode to
See the section on isolation levels under Resilience for more details.
You may manually clear caches by:
This currently clears all cache entries associated with the resilience configuration irrespective of the current tenant and principal.
To get hold of the created cache instance and to use it for advanced operations like for e.g registering event listeners you can use JCache API:
SimpleCacheEntryListener is a class that implements the interfaces of the required events w.r.t cache defined in EventType
Note that the identifier of the resilience configuration is also the identifier of the cache associated with it.