10.2. Use the Default Cache
10.2.1. Add and Remove Data from the Cache
Procedure 10.2. Add and Remove Data from the Cache
- Add an entry, replacing key and value with the desired key and value:
cache.put("key", "value"); - Confirm that the entry is present in the cache:
assertEquals(1, cache.size()); assertTrue(cache.containsKey("key")); - Remove the entry from the cache:
Object v = cache.remove("key"); - Confirm that the entry is no longer present in the cache:
assertEquals("value", v); assertTrue(cache.isEmpty());
10.2.2. Adding and Replacing a Key Value
DefaultCacheQuickstart.java file does:
Procedure 10.3. Adding and Replacing a Key Value
- Add an entry
keywithvalueas the key's value.cache.put("key", "value");
Procedure 10.4. Replacing a Key Value
- The following code searches for keys (named
keyandkey2). If the two specific keys beings searched for are not found, JBoss Data Grid creates two new keys with the specified key names and values.cache.putIfAbsent("key", "newValue"); cache.putIfAbsent("key2", "value2"); - The following code confirms that the value of the stored key equals the value we wanted to store.
assertEquals(cache.get("key"), "value"); assertEquals(cache.get("key2"), "value2");
See Also:
10.2.3. Adjust Data Life
DefaultCacheQuickstart.java file does:
Procedure 10.5. Adjust the Data Life
- Alter the key's
lifespanvalue:cache.put("key", "value", 5, TimeUnit.SECONDS); - Check if the cache contains the key:
assertTrue(cache.containsKey("key")); - After the allocated
lifespantime has expired, the key is no longer in the cache:Thread.sleep(10000); assertFalse(cache.containsKey("key"));
10.2.4. Default Data Mortality
10.2.5. Register the Named Cache Using XML
infinispan.xml file.
infinispan.xml file is located in https://github.com/infinispan/infinispan-quickstart in the infinispan-quickstart/embedded-cache/src/main/resources folder.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.