Distributed Cache
Writing a custom Open Banking Distributed Cache Key¶
WSO2 Open Banking Distributed Cache is a caching service that creates a clustered cache among one or more WSO2 API
Manager or Identity Server instances. The Open Banking Distributed Cache Key contains an extension.
According to your requirements, you can extend and override the methods in the OpenBankingDistributedCacheKey
class
to implement a distributed cache according to your open banking requirements.
To achieve the above, extend the following class.
com.wso2.openbanking.accelerator.common.distributed.caching.OpenBankingDistributedCacheKey
To extend the capabilities according to your requirements, override relevant methods of this class. Given below is a brief description of each method.
OpenBankingDistributedCacheKey method¶
This method is the public constructor for this class.
public OpenBankingDistributedCacheKey(String cacheKey) {
setCacheKey(cacheKey);
}
Retrieve OpenBankingDistributedCacheKey method¶
This method lets you retrieve an instance of an OpenBankingDistributedCacheKey
.
public static OpenBankingDistributedCacheKey of(String cacheKey) {
return new OpenBankingDistributedCacheKey(cacheKey);
}
setCacheKey method¶
This method lets you assign a value to (setter) the cache key string.
public void setCacheKey(String cacheKey) {
this.cacheKey = cacheKey;
}
getCacheKey method¶
This method lets you retrieve (getter) the cache key string.
public String getCacheKey() {
return this.cacheKey;
}
equals method¶
This method checks if the passed object is an OpenBankingDistributedCacheKey
object.
Given below is the method signature:
public boolean equals(Object o) {
}
hashCode method¶
This method returns the hashcode value for the OpenBankingDistributedCacheKey
object.
public int hashCode() {
return Objects.hash(getCacheKey());
}
Writing a custom Open Banking Distributed Cache¶
In order to create a custom distribute cache, you need to extend the OpenBankingDistributedCache
class as well.
The Fully Qualified Name (FQN) of this class is as follows:
com.wso2.openbanking.accelerator.common.distributed.caching.OpenBankingDistributedCache
To extend the capabilities according to your requirements, override relevant methods of this class. Given below is a brief description of each method.
OpenBankingDistributedCache method¶
This method lets you initialize a unique name for the cache.
public OpenBankingDistributedCache(String cacheName) {
}
getFromCache method¶
This method lets you retrieve a cache based on the cache key.
public V getFromCache(K key) {
}
addToCache method¶
This method lets you add an object to the cache in key-value format. Use the key
parameter to set the cache key
and the value
parameter to set the object that needs to be cached.
public void addToCache(K key, V value) {
}
removeFromCache method¶
This method lets you remove a cache based on the cache key.
public void removeFromCache(K key) {
}
isEmpty method¶
This method lets you check whether the cache is empty. If the cache is empty, the return value is true
, otherwise,
the boolean value false
is returned.
public boolean isEmpty() {
}
getCacheTimeToLiveMinutes method¶
This method lets you retrieve the cache expiry time in minutes upon modification.
public abstract int getCacheTimeToLiveMinutes();
Creating cache¶
- Extend the
OpenBankingDistributedCache
class. - Generify the extended
OpenBankingDistributedCache
class with the previously createdOpenBankingDistributedCacheKey
class and preferred object for the cache value. - Implement abstract methods from the super class.
- Create a constructor with the super constructor.
- Create the
int cacheTimeToLiveMinutes
variable. - Override the getter method and create a setter method. Initialize the
cacheTimeToLive
in the constructor.
Using custom cache¶
- Creating a cache object
- A cache object can be created using the cache class created above.
- The cache name has to be passed as a String parameter to the constructor.
- Creating a cache key object
- A cache key object can be created using the cache key class above.
- The value of the cache key has passed as a String parameter to the constructor.
- Adding to cache
- Use the
addToCache
method to add a cache entry. - Pass the cache key and the value to be cached as parameters.
- Use the
- Retrieving from cache
- Use the
getFromCache
method to retrieve a cache entry. - Pass the cache key as a parameter.
- Use the
- Removing a cache
- Use the
removeFromCache
method to remove a cache. - Pass the cache key as a parameter.
- Use the
Given below is a sample usage of the cache:
Configuring a custom Open Banking Distributed Cache Key¶
- Once implemented, build JAR files for your projects.
- Place the above-created JAR files in the relevant
lib
directory of the base product:<APIM_HOME>/repository/components/lib
<IS_HOME>/repository/components/lib
- Configure the
deployment.toml
file of the instance based on your caching requirements. For more information, see Distributed Cache Configurations.