Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Basic Cache Operations

Perform basic GridGain cache operations, including creating and destroying caches, atomic operations, asynchronous execution, and resource injection.

To preclude issues with cache names, explicitly prohibit slashes, backslashes, line separators, and null characters in these names by setting the IGNITE_VALIDATE_CACHE_NAMES system property to true.

Getting an Instance of a Cache

All operations on a cache are performed through an instance of IgniteCache. You can obtain IgniteCache for an existing cache, or you can create a cache dynamically.

Ignite ignite = Ignition.ignite();

// Obtain an instance of the cache named "myCache".
// Note that different caches may have different generics.
IgniteCache<Integer, String> cache = ignite.cache("myCache");
IIgnite ignite = Ignition.Start();

// Obtain an instance of cache named "myCache".
// Note that generic arguments are only for your convenience.
// You can work with any cache in terms of any generic arguments.
// However, attempt to retrieve an entry of incompatible type
// will result in exception.
ICache<int, string> cache = ignite.GetCache<int, string>("myCache");
IgniteConfiguration cfg;
cfg.springCfgPath = "/path/to/configuration.xml";

Ignite ignite = Ignition::Start(cfg);

// Obtain instance of cache named "myCache".
// Note that different caches may have different generics.
Cache<int32_t, std::string> cache = ignite.GetCache<int32_t, std::string>("myCache");

Creating Caches Dynamically

You can also create a cache dynamically:

Ignite ignite = Ignition.ignite();

CacheConfiguration<Integer, String> cfg = new CacheConfiguration<>();

cfg.setName("myNewCache");
cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);

// Create a cache with the given name if it does not exist.
IgniteCache<Integer, String> cache = ignite.getOrCreateCache(cfg);

Refer to the Cache Configuration section for the list of cache parameters.

IIgnite ignite = Ignition.Start();

// Create cache with given name, if it does not exist.
var cache = ignite.GetOrCreateCache<int, string>("myNewCache");
IgniteConfiguration cfg;
cfg.springCfgPath = "/path/to/configuration.xml";

Ignite ignite = Ignition::Start(cfg);

// Create a cache with the given name, if it does not exist.
Cache<int32_t, std::string> cache = ignite.GetOrCreateCache<int32_t, std::string>("myNewCache");

The methods that create a cache throw an org.apache.ignite.IgniteCheckedException exception when called while the baseline topology is being changed.

You may want to retry the operation if you catch this exception.

Clearing Caches

To clear data from cache, use the clear() or removeAll() method. The sections below will help you choose the correct method for your environment.

Using Clear Method

The clear method removes data from the cache without notifying any listeners (for example, CacheEntryRemovedListener). As a result, calling this method does not delete data from external cache storage, and deletion is not replicated via data center replication.

This method is optimized for performance for bulk data removal.

The example below shows how you can call this method:

unsupported

Using removeAll Method

The removeAll method methodically removes all data from the cache, calling all listeners for each key removed. Data removal will be propagated to external cache storage, and other clusters via data center replication.

Due to invoking listeners and callbacks, this is potentially an expensive operation.

Destroying Caches

To delete a cache from all cluster nodes, call the destroy() method.

unsupported

Atomic Operations

Once you get the instance of a cache, you can start performing get/put operations on it.

Bulk operations such as putAll() or removeAll() are executed as a sequence of atomic operations and can partially fail. If this happens, a CachePartialUpdateException is thrown and contains a list of keys for which the update failed.

To update a collection of entries within a single operation, consider using transactions.

Below are more examples of basic atomic operations:

Asynchronous Execution

Most of the cache operations have asynchronous counterparts that have the "Async" suffix in their names.

unsupported

The asynchronous operations return an object that represents the result of the operation. You can wait for the completion of the operation in either blocking or non-blocking manner.

To wait for the results in a non-blocking fashion, register a closure using the IgniteFuture.listen() or IgniteFuture.chain() method. The closure is called when the operation is completed.

unsupported

Closures Execution and Thread Pools

If an asynchronous operation is completed by the time the closure is passed to either the IgniteFuture.listen() or IgniteFuture.chain() method, then the closure is executed synchronously by the calling thread. Otherwise, the closure is executed asynchronously when the operation is completed.

Depending on the type of operation, the closure might be called by a thread from the system pool (asynchronous cache operations) or by a thread from the public pool (asynchronous compute operations). Therefore, you should avoid calling synchronous cache and compute operations from inside the closure, because it may lead to a deadlock due to pools starvation.

To achieve nested execution of asynchronous compute operations, you can take advantage of custom thread pools.

Resource Injection

Ignite allows dependency injection of pre-defined Ignite resources, and supports field-based as well as method-based injection. Resources with proper annotations will be injected into the corresponding task, job, closure, or SPI before it is initialized.

You can inject resources by annotating either a field or a method. When you annotate a field, Ignite simply sets the value of the field at injection time (disregarding an access modifier of the field). If you annotate a method with a resource annotation, it should accept an input parameter of the type corresponding to the injected resource. If it does, then the method is invoked at injection time with the appropriate resource passed as an input argument.

Below is an example of a field injection.

And this is an example of a method-based injection:

There are a number of pre-defined resources that you can inject:

Resource
Description

CacheNameResource

Injects the cache name provided via CacheConfiguration.getName().

CacheStoreSessionResource

Injects the current CacheStoreSession instance.

IgniteInstanceResource

Injects the current instance of Ignite.

JobContextResource

Injects an instance of ComputeJobContext. A job context holds useful information about a particular job execution. For example, you can get the name of the cache containing the entry for which a job was colocated.

LoadBalancerResource

Injects an instance of ComputeLoadBalancer that can be used by a task to do the load balancing.

ServiceResource

Injects the service specified by the given name.

SpringApplicationContextResource

Injects Spring's ApplicationContext resource.

SpringResource

Injects resource from Spring's ApplicationContext. Use it whenever you would like to access a bean specified in Spring's application context XML configuration.

TaskContinuousMapperResource

Injects an instance of ComputeTaskContinuousMapper. Continuous mapping allows emitting jobs from the task at any point, even after the initial map phase.

TaskSessionResource

Injects an instance of the ComputeTaskSession resource, which defines a distributed session for a particular task execution.

This page is: Copyright © 2026 MariaDB. All rights reserved.

Last updated

Was this helpful?