Insert Data in Cache

You can insert a cache element using the put() method. This enables you to insert a key-value pair in an existing cache segment in your Catalyst project. The key name and key value are of the String type and are passed as arguments to the method.

You can also pass the expiry time for the cache element optionally. If you do not pass that value, the expiry time will be set to 48 hours by default.

The segment reference used in the code snippet below is the segment instance created earlier. The promise returned here will be resolved to a JSON object.

    
copy
//Insert Cache by passing the key-value pair let cache = app.cache(); let segment = cache.segment(); let cachePromise = segment.put('Name', 'Linda McCartney',1); //Expiry time for cache in hours cachePromise.then((entity) => { console.log(entity); });

A sample response that you will receive for each version is shown below:

    
Node JS
copy
{
cache_name: "Last_Name",
cache_value: "Smith",
project_details: { project_name: "AlienCity", id: "2136000000007733" },
segment_details: { segment_name: "DataStore", id: "2136000000008572" },
expires_in: "Aug 18, 2021 06:46 PM",
expiry_in_hours: "48",
ttl_in_milliseconds: "172800000"
}
{
cache_name: "Last_Name",
cache_value: "Smith",
project_details: { project_name: "AlienCity", id: 2136000000007733 },
segment_details: { segment_name: "DataStore", id: 2136000000008572 },
expires_in: "Aug 18, 2021 06:45 PM",
expiry_in_hours: 48,
ttl_in_milliseconds: 172800000
}

Last Updated 2024-02-23 17:29:25 +0530 +0530

ON THIS PAGE