Ce contenu n'est pas disponible dans la langue sélectionnée.
9.2. Use the Default Cache
9.2.1. Add and Remove Data from the Cache Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
Red Hat JBoss Data Grid offers an interface that is similar to the proposed JSR-107 API to access and alter data stored in a cache.
The following procedure is an example that defines what each line entered into the DefaultCacheQuickstart.java file does:
Procedure 9.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());
9.2.2. Adding and Replacing a Key Value Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
Red Hat JBoss Data Grid offers a thread-safe data structure.
The following procedure is an example that defines what each line entered into the
DefaultCacheQuickstart.java file does:
Procedure 9.3. Adding and Replacing a Key Value
- Add an entry
keywithvalueas the key's value.cache.put("key", "value");
Procedure 9.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:
9.2.3. Adjust Data Life Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
Red Hat JBoss Data Grid entries are immortal by default, but these settings can be altered.
The following procedure is an example that defines what each line entered into the
DefaultCacheQuickstart.java file does:
Procedure 9.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"));
9.2.4. Default Data Mortality Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
As a default, newly created entries do not have a life span or maximum idle time value set. Without these two values, a data entry will never expire and is therefore known as immortal data.
9.2.5. Register the Named Cache Using XML Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
To configure the named cache declaratively (using XML) rather than programmatically, configure the
infinispan.xml file.
The
infinispan.xml file is located in https://github.com/infinispan/infinispan-quickstart in the infinispan-quickstart/embedded-cache/src/main/resources folder.