--- title: "Insert Data into Cache" url: https://docs.catalyst.zoho.com/en/sdk/java/v1/cloud-scale/cache/insert-data-into-cache/ description: "This page describes the method to insert data into the cache in your Java application with sample code snippets." last_updated: 2025-11-19T15:07:41Z service: Cloud Scale type: general related_links: - name: "Insert Data into the Cache - API" url: /en/api/code-reference/cloud-scale/cache/insert-key-value-in-segment/#InsertKey-ValueinCacheSegment - name: "Cache" url: /en/cloud-scale/help/cache/introduction --- # 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. #### Sample Code Snippet **Package Imports** ```java import com.zc.component.cache.ZCCache; import com.zc.component.cache.ZCCacheObject; import com.zc.component.cache.ZCSegment; ``` ```java //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. #### Sample Code Snippet **Package Imports** ```java import com.zc.component.cache.ZCCache; import com.zc.component.cache.ZCCacheObject; import com.zc.component.cache.ZCSegment; ``` ```java //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. #### Sample Code Snippet **Package Imports** ```java import com.zc.component.cache.ZCCache; import com.zc.component.cache.ZCCacheObject; import com.zc.component.cache.ZCSegment; ``` ```java //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); ```