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

Clear Cache

You can clear the cache contents from a cache using the Clear method. You can clear the cache asynchronously or with registered callback.

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>
  • Import the following packages in your application:
    • import javax.cache.CacheManager;
    • import javax.cache.Caching;
    • import javax.cache.spi.CachingProvider;
  • Make sure that the data is serailized or registered with NCache Compact Serialization format.
  • Make sure that the cache is running.

Clear Cache Asynchronously

To clear cache completely in background, you need to call the clearAsync method.

try 
{
    cache.clearAsync(null);
} 
catch (Exception ex) 
{
    // handle exception
}

Clear Cache with Registered Callback

In order to get async clear notification, you have to implement the AsynccacheClearedCallback interface. Include the following package in order to register callback method.

import com.alachisoft.ncache.web.caching.AsyncCacheClearedCallback;

try
{
    EventListener listener = new EventListener();
    cache.clearAsync((AsyncCacheClearedCallback) listener);
}
catch(Exception ex)
{
    // Handle Exception
}

Create a callback to register the events for the cache cleared asynchronously.

public class EventListener implements AsyncCacheClearedCallback
{
    @Override
     public void asyncCacheCleared(Object o) 
     {

        if(o.toString().equals("Success"))
        {
          // do something
        }
        if(o.toString().equals("Failure"))
        {
          // do something
        }

        if(o instanceof Exception)
        {
          // do something
        }    
     }
}       
Back to top Copyright © 2017 Alachisoft