Desktop Cache
Class finesse.utilities.DesktopCache
Allows gadgets to cache their data locally in the IndexedDB of the browser. When a third-party gadget makes any REST API call to retrieve data from the server (which does not change frequently), it can be cached using Desktop Cache. This helps to load the gadget faster after failover by avoiding a REST request to the server. The cached data is a key-value pair where the key is a string, and value is an object.
Methods
clearData(callback)
Clears all the records in the database.
Example
finesse.utilities.DesktopCache.clearData(function(err) {
if (!err) {
// Successfully cleared the records!
}
});
Parameters
Name |
Type |
Description |
Required |
---|---|---|---|
callback |
Function |
An asynchronous callback function that is invoked after all the records in the IndexedDB is cleared |
No |
deleteData(key, callback)
Deletes the record that corresponds to the key passed from the database.
Example
// inside the gadget somewhere
if (userClickedOk) {
finesse.utilities.DesktopCache.delete('someKey', function(err, data) {
if (!err) {
// Successfully deleted! Now do something else.
}
});
}
Parameters
Name |
Type |
Description |
Required |
---|---|---|---|
key |
String |
The unique identifier which is used to delete a record from the IndexedDB. |
Yes |
callback |
Function |
An asynchronous callback function that is invoked after the attempt to delete the record (success or fail). |
No |
-->err |
Object |
The JavaScript error message. |
No |
fetchData(key, callback)
Retrieves the records corresponding to the key passed.
Example
// fetching all the records
finesse.utilities.DesktopCache.fetchData(null, function(err, data) {
if (!err)
console.log(data); // will print something like [{key: someKey, data: someData}, {key: otherKey, data: otherData}.......]
});
Parameters
Name |
Type |
Description |
Required |
---|---|---|---|
key |
String |
Unique identifier and it is the primary key to retrieve a single record from the IndexedDB. |
Yes |
callback |
Function |
An asynchronous callback function that is invoked after the data retrieval is successful or failed. |
No |
-->err |
Object |
The JavaScript error message. |
No |
-->data |
Object |
The JavaScript array of objects. |
No |
saveOrUpdateData(records, callback)
Creates or updates the records. The parameters passed are an array of key-value pairs.
Example
finesse.utilities.DesktopCache.saveOrUpdateData({
[
key: 'someKey'
data: 'someData'
]
}, function(err, data) {
if (!err) // do something
});
Parameters
Name |
Type |
Description |
Required |
---|---|---|---|
records |
Array |
The data to be saved or updated as an array of key-value pairs. |
Yes |
callback |
Function |
An asynchronous callback function that is invoked after the save or updating of the record is completed (success or fail). |
No |
-->err |
Object |
The JavaScript error message. |
No |
setGroup(groupName)
Reduces redundancy in the data that are stored when different gadgets from the same vendor (for example, cuic) retain the same group name.
Note | Having the same group name for the gadgets which share the same data reduces the server load and improves the performance. This API must be called before any other desktop cache APIs. |
For example, consider vendor1 has two gadgets (gadget1 and gadget2), and both gadgets require the same token to be accessed from the server. Then the group name for these two gadgets can be set as vendor1. The gadget which loads first makes the server call, fetches the token, and then saves it in the desktop cache. The second gadget fetches this token from the desktop cache without making the server call.
Example
// Set this once and you don't need to send this group name with any other subsequent requests from this gadget.
finesse.utilities.DesktopCache.setGroup('cuic');
Parameters
Name |
Type |
Description |
Required |
---|---|---|---|
groupName |
String |
The name of the group for the gadgets which share the same data. |
Yes |