Este conteúdo não está disponível no idioma selecionado.

10.2. Use the Default Cache


10.2.1. Add and Remove Data from the Cache

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 10.2. Add and Remove Data from the Cache

  1. Add an entry, replacing key and value with the desired key and value:
    cache.put("key", "value");
    Copy to Clipboard Toggle word wrap
  2. Confirm that the entry is present in the cache:
    assertEquals(1, cache.size());
    assertTrue(cache.containsKey("key"));
    Copy to Clipboard Toggle word wrap
  3. Remove the entry from the cache:
    Object v = cache.remove("key");
    Copy to Clipboard Toggle word wrap
  4. Confirm that the entry is no longer present in the cache:
    assertEquals("value", v);
    assertTrue(cache.isEmpty());
    Copy to Clipboard Toggle word wrap

10.2.2. Adding and Replacing a Key Value

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 10.3. Adding and Replacing a Key Value

  • Add an entry key with value as the key's value.
    cache.put("key", "value");
    Copy to Clipboard Toggle word wrap

Procedure 10.4. Replacing a Key Value

  1. The following code searches for keys (named key and key2). 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");
    Copy to Clipboard Toggle word wrap
  2. 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");
    Copy to Clipboard Toggle word wrap

10.2.3. Adjust Data Life

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 10.5. Adjust the Data Life

  1. Alter the key's lifespan value:
    cache.put("key", "value", 5, TimeUnit.SECONDS);
    Copy to Clipboard Toggle word wrap
  2. Check if the cache contains the key:
    assertTrue(cache.containsKey("key"));
    Copy to Clipboard Toggle word wrap
  3. After the allocated lifespan time has expired, the key is no longer in the cache:
    Thread.sleep(10000);
    assertFalse(cache.containsKey("key"));
    Copy to Clipboard Toggle word wrap

10.2.4. Default Data Mortality

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.

10.2.5. Register the Named Cache Using XML

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.
Voltar ao topo
Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2025 Red Hat