Ce contenu n'est pas disponible dans la langue sélectionnée.
10.2. Use the Default Cache
10.2.1. Add and Remove Data from the Cache Copier lienLien copié sur presse-papiers!
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");cache.put("key", "value");Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Confirm that the entry is present in the cache:
assertEquals(1, cache.size()); assertTrue(cache.containsKey("key"));assertEquals(1, cache.size()); assertTrue(cache.containsKey("key"));Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Remove the entry from the cache:
Object v = cache.remove("key");Object v = cache.remove("key");Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Confirm that the entry is no longer present in the cache:
assertEquals("value", v); assertTrue(cache.isEmpty());assertEquals("value", v); assertTrue(cache.isEmpty());Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.2.2. Adding and Replacing a Key Value Copier lienLien copié sur presse-papiers!
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");cache.put("key", "value");Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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");cache.putIfAbsent("key", "newValue"); cache.putIfAbsent("key2", "value2");Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 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");assertEquals(cache.get("key"), "value"); assertEquals(cache.get("key2"), "value2");Copy to Clipboard Copied! Toggle word wrap Toggle overflow
See Also:
10.2.3. Removing Entries Copier lienLien copié sur presse-papiers!
All of the following methods are found on org.infinispan.Cache and its subclasses.
remove(key)- remove a single key from the cache.removeAsync(key)- remove a single key from the cache, asynchronously.clear()- removes all of the mappings from the cache, leaving it empty once the call completes.clearAsync()- asynchronously remove all of the mappings from the cache, leaving it empty once the call completes.cache.evict(key)- remove the entry from the cache, moving it to the cache store if one is defined. With no cache store defined the entry is removed from the cache and is lost.
All of the following methods are found on org.infinispan.client.hotrod.RemoteCache and its subclasses.
remove(key)- remove a single key from the cache.removeAsync(key)- remove a single key from the cache, asynchronously.clear()- removes all of the mappings from the cache, leaving it empty once the call completes.clearAsync()- asynchronously remove all of the mappings from the cache, leaving it empty once the call completes.removeWithVersion(key, version)- remove a single key from the cache only if its current version matches the supplied version.removeWithVersionAsync(key, value)- asynchronously remove a single key from the cache only if its current version matches the supplied version.
10.2.4. Placing and Retrieving Sets of Data Copier lienLien copié sur presse-papiers!
AdvancedCache and RemoteCache interfaces include methods to either put or get a Map of existing data in bulk. These operations are typically much more efficient than an equivalent sequence of individual operations, especially when using them in server-client mode, as a single network operation occurs as opposed to multiple transactions.
get or put operation must accommodate for the full Map in a single execution.
AdvancedCache:Map<K,V>getAll(Set<?> keys)- returns aMapcontaining the values associated with the set of keys requested.- void
putAll(Map<? extends K, ? extends V> map, Metadata metadata)- copies all of the mappings from the specified map to this cache, which takes an instance ofMetadatato provide metadata information such as the lifespan, version, etc. on the entries being stored.
RemoteCache:Map<K,V>getAll(Set<? extends K> keys)- returns aMapcontaining the values associated with the set of keys requested.- void
putAll(Map<? extends K, ? extends V> map)- copies all of the mappings from the specified map to this cache. - void
putAll(Map<? extends K, ? extends V> map, long lifespan, TimeUnit unit)- copies all of the mappings from the specified map to this cache, along with a lifespan before the entry is expired. - void
putAll(Map<? extends K, ? extends V> map, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit)- copies all of the mappings from the specified map to this cache, along with both a timespan before the entries are expired and the maximum amount of time the entry is allowed to be idle before it is considered to be expired.
10.2.5. Adjust Data Life Copier lienLien copié sur presse-papiers!
DefaultCacheQuickstart.java file does:
Procedure 10.5. Adjust the Data Life
- Alter the key's
lifespanvalue:cache.put("key", "value", 5, TimeUnit.SECONDS);cache.put("key", "value", 5, TimeUnit.SECONDS);Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Check if the cache contains the key:
assertTrue(cache.containsKey("key"));assertTrue(cache.containsKey("key"));Copy to Clipboard Copied! Toggle word wrap Toggle overflow - After the allocated
lifespantime has expired, the key is no longer in the cache:Thread.sleep(10000); assertFalse(cache.containsKey("key"));Thread.sleep(10000); assertFalse(cache.containsKey("key"));Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.2.6. Default Data Mortality Copier lienLien copié sur presse-papiers!
10.2.7. Register the Named Cache Using XML Copier lienLien copié sur presse-papiers!
infinispan.xml file.
infinispan.xml file is located in https://github.com/jboss-developer/jboss-jdg-quickstarts/ within the secure-embedded-cache/src/main/resources/ folder, and the full schema is available in the docs/schema/ directory of the Red Hat JBoss Data Grid Library distribution.