Cache in APIs
Available since release 2.3.0-immortal
Introduction
Up until now, all the operations of the REST APIs available on the platform end up reaching the data sources. Although this will be the default operation, yet in certain cases, it may be desirable for speed of response, that the data is accessible in a faster way. This will be the case for data sources that do not change over time or change infrequently.
This way, applications using such APIs will benefit from improved response times, increasing their own performance.
Use
To this end, a new cache mechanism has been implemented on the operations of the APIs managed by the platform. This cache uses the platform's existing cache module to store the information temporarily.
When an API is defined in platform from the control panel, a cache interval (in minutes) can be input as a configuration attribute:
This will be the time during which the records will be valid in the cache. Once this period has finished, the information from the data source will be retrieved again.
Operation flow
The operation flow will be as follows
When an API invocation is made, it is checked whether it has a result cache time defined. If it does not, then the invocation is performed in the normal way, without taking into account the cache.
If the API has a cache time configured, from the invocation parameters (API, version, operation, query params, user,...), it tries to retrieve the result from the cache.
If the entry exists and is still valid (its validity period has not expired), the cached result is returned.
If there is no entry for the invocation, or if it is no longer valid, the invocation is performed as normal and its result is cached for future invocations.
This way ensures that the data is retained for the configured period.
Important considerations
Ther are certain considerations that you must bear in mind:
It is important to take into account the data access restrictions that the user may have set, as well as the different invocation parameters to the API operation. The cache system checks these restrictions when accessing and storing records.
A special case can be data sources that are immutable (data catalogues). In order to keep the records in the cache as long as possible, there is the option to set the cache timeout to "0". Then, the records, once stored in the cache, will be kept indefinitely.
This configuration can be set both for ontology APIs and for APIs that are external to the platform.
Besides, considering that the size of the cache must be limited, the policy (LRU) for the extraction of records must be established in its configuration. That is to say: when the cache reaches its maximum capacity, it will proceed to delete the entries that have been accessed the least (Less recent use). This maximum size limit shall be configurable at deployment time.
Sometimes it may be necessary to modify the information in the data sources. This could lead to inconsistencies with results that may be cached (either because they have not been removed by timeout or because they have been configured as permanent). To avoid this scenario, two services have been implemented.
Clear the cache at API level: All the records corresponding to the API - Version will be removed from the cache. To do this, click on the corresponding control of the record in the list of APIs:
General emptying of the API Manager cache (Only users with Administrator ROLE): All the records in the cache will be deleted. To do this, click on the "Empty cache" option on the API List screen:
All these operations are available in the platform’s REST API:
In the Control Panel's REST API: Configuration of API caching when registering or modifying APIS.
In the API Manager management's REST API: Clear cache operations.