Insert data into the cache
In addition to retrieving cache information, the following putCache() variants also support inserting cache object elements.
Insert a key-value pair
The following code inserts a key-value pair to a cache segment through putCacheValue() method.
Note:
The expiry time is set to 48 hours by default.
Ensure the following packages are imported:
copyimport com.zc.component.cache.ZCCache; import com.zc.component.cache.ZCCacheObject; import com.zc.component.cache.ZCSegment;
copy//Get a Cache Instance ZCCache cacheobj=ZCCache.getInstance(); //Get an instance of a specific segment with segment ID ZCSegment segment = cacheobj.getSegment(1510000000054091L); //Put Value in Cache as key-value pair (with a default Expiry Time of 48 hours) ZCCacheObject cache = segment.putCacheValue("Name", "Amelia Burrows");
Insert a key-value pair with an expiry time
Similar to the previous case, along with key and value parameters, the optional parameter expiry time is used in this variant.
Note:
The value of the expiry time must be passed as a long value in hours.
Ensure the following packages are imported:
copyimport com.zc.component.cache.ZCCache; import com.zc.component.cache.ZCCacheObject; import com.zc.component.cache.ZCSegment;
copy//Get a Cache Instance ZCCache cacheobj=ZCCache.getInstance(); //Get an instance of a specific segment with segment ID ZCSegment segment = cacheobj.getSegment(1510000000054091L); //Put Value in Cache as key-value pair with specified expiry time. (Time in hours) ZCCacheObject cache = segment.putCacheValue("LastName", "S", 1L);
Insert a key-value pair through a cache object
The following code inserts a key-value pair to a cache segment through putCacheObject() method.
Note:
If the key name already exists in a cache segment, it will be replaced with the new value inserted.
Ensure the following packages are imported:
copyimport com.zc.component.cache.ZCCache; import com.zc.component.cache.ZCCacheObject; import com.zc.component.cache.ZCSegment;
copy//Get a Cache Instance ZCCache cacheobj=ZCCache.getInstance(); //Get an instance of a specific segment with segment ID ZCSegment segment = cacheobj.getSegment(1510000000054091L); //Create a CacheObject and set cache segment attributes ZCCacheObject cacheDetails = ZCCacheObject.getInstance(); cacheDetails.setKeyName("ObjectKey"); cacheDetails.setValue("ObjectValue"); cacheDetails.setExpiryInHours(1L); //Create the cache using the CacheObject ZCCacheObject cache = segment.putCacheObject(cacheDetails);
Last Updated 2023-09-03 01:06:41 +0530 +0530
Yes
No
Send your feedback to us
Skip
Submit