• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Initialize Cache

After successfully plugging in NCache in your JCache application, a cache instance is needed to be created to perform functions on the cache. In Java, applications can also interact with NCache by using provider for JCache API included in NCache. The JCache API provider provides a convenient method to initialize the cache instance.

Prerequisites

  • Add the following Maven dependencies in your pom.xml file:
<dependency>
    <groupId>com.alachisoft.ncache</groupId>
    <!--for NCache Enterprise Edition--> 
    <artifactId>ncache-client</artifactId>
    <!--for NCache Professional Edition-->
    <artifactId>ncache-professional-client</artifactId>
    <version>x.x.x</version>
</dependency>
  • Include the following packages and their libraries in your application from NCache install directory.
    • import javax.cache.CacheManager
    • import javax.cache.Caching;
    • import javax.cache.spi.CachingProvider;

Initialize Cache

The most convenient method of getting a cache’s instance in Java is to use getCache method of JCache CacheManager’s provider by NCache. You can get an instance of already configured cache by using its name (unique identifier) as an argument to getCache method.

try
{
    //Create an instance of JCache's caching provider to get JCacheManager's instance by NCache
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager manager = provider.getCacheManager();

    //Get a cache from manager via its string name.
    javax.cache.Cache jCache = manager.getCache("mycache");
}
catch(Exception ex)
{
    // handle exception
}

Obtaining NCache API's Cache Instance

NCache's API provides more flexible and convenient caching features as compared to JCache's API, so to utilize them you need to have an instance of NCache API’s cache.

In this example JCache API’s cache instance is used to get NCache's cache instance by means of simply using its unwrap method.

try
{
    //Create an instance of JCache's caching provider to get JCacheManager's instance by NCache.
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager manager = provider.getCacheManager();

    //Get a cache from manager via its string name.
    javax.cache.Cache jCache = manager.getCache("mycache");

   com.alachisoft.ncache.client.Cache cache = (com.alachisoft.ncache.client.Cache) jCache.unwrap(com.alachisoft.ncache.client.Cache.class);

}
catch(Exception ex)
{
    // Handle exception
}

Initialize Multiple Caches in a Single Application

For this exercise it is necessary for both the caches to have been configured previously and also be in the running state.

try
{
    //Create an instance of JCache's caching provider to get JCacheManager's instance by NCache
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager manager = provider.getCacheManager();

    //Get a cache from manager via its string name.
    javax.cache.Cache jCache1 = manager.getCache("mycache1");
    javax.cache.Cache jCache2 = manager.getCache("mycache2");
}
catch(Exception ex)
{
    // Handle Exception
}

Initialize In-Proc Cache

The operation used to initialize an in-proc cache is similar to that of an out-proc cache except for the fact that the in-proc cache is not in a running state, however an in-proc NCache cache (local/clustered) can also be created by the means of using JCache API’s provider by NCache, where the created cache will not be saved in any configuration file.

Make sure to add the following package to your caching application in order to create aN in-proc cache:

import javax.cache.configuration.*;

try
{   
    //Create an instance of JCache's caching provider to get JCacheManager's instance by NCache.
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager manager = provider.getCacheManager();

    javax.cache.Cache jCache = null;

    //For creating a cache with management enabled.
    CompleteConfiguration<Object, Object> cacheConfig = new MutableConfiguration().setManagementEnabled(true);

    //For creating an in-proc cache.
    jCache = manager.createCache("mycache", cacheConfig);

    //For already existing inproc cache in NCache. 
    jCache = manager.getCache("mycache");
}
catch(Exception ex)
{
    // Handle Exception
}

Initialize Cache Using CacheInitParams

CacheInitParams lets you edit the values of the properties at runtime. In the following example, we change the RetryInterval and ConnectionRetries properties. Subsequently, the application will use the values specified in CacheInitParams rather than the ones specified in the client configuration files.

try
{
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager manager = provider.getCacheManager();

    //Get a cache from manager via its string name.
    javax.cache.Cache jCache = manager.getCache("mycache");
    com.alachisoft.ncache.web.caching.Cache cache = null;
    cache = (Cache) jCache.unwrap(Cache.class);

    // Initialize cache using CacheInitParams
    CacheInitParams ciParam = new CacheInitParams();
    ciParam.setRetryInterval(5);
    ciParam.setConnectionRetries(4);

    // Initialize the cache
    cache = NCache.initializeCache("mycache", ciParam);
}
catch(Exception ex)
{
    // Handle Exception
}

Initialize Remote and Client Cache simultaneously

If separate calls are made for both caches the local cache does not behave as a client cache instead it functions as an independent local cache.
In order to use the existing local cache as a client cache we recommend that the following method is used instead of making separate initialization calls. This API would return a handler of the remote cache synchronizing all operations of the client cache.

try
{
    cache = (Cache) jCache.unwrap(Cache.class);
    cache = NCache.initializeCache("myRemoteCache", "myClientCache");
}
catch(Exception ex)
{
    // Handle Exception
}

See Also

Add/Update in Cache
Fetch Items from Cache
Remove Operations in Cache
Hibernate Caching
NCache Java Session Module

Back to top Copyright © 2017 Alachisoft