Clear Cache Data
NCache allows clients to empty the cache in one call completely, without relying on data invalidation strategies like expiration and eviction. Emptying the cache results in the cache going into a "fresh" state, as all keys and associated items and metadata are removed from the cache. In case of client cache configured, NCache provides separate methods to clear the client cache, irrespective of the remote cache.
Note
- To use Maven packages for NCache Professional Edition, change the
<artifactId>
as shown below:<artifactId>ncache-professional-client</artifactId>
- To use Node.js API in NCache Professional Edition, install and include the
ncache-professional-client
npm package in your Node.js application.
Pre-requisites
- Install the following NuGet package in your application.
- Include the following namespaces in your application:
Alachisoft.NCache.Client
.Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- For API details refer to: ICache, Clear(), ClearClientCache()
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
Remote Cache
The following example initializes a remote cache myPartitionedCache, performs some operations and uses the Clear()
method to empty the cache synchronously. For Java, use the clear()
method to clear the cache contents.
try
{
ICache cache = CacheManager.GetCache("myPartitionedCache");
// clear the cache
cache.Clear();
}
catch (Exception e)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Client Cache
A client cache is a local cache synchronized with the remote clustered cache. All operations on the remote cache are synchronized with the client cache. However, you can independently clear the client cache, which will then synchronize itself with the current state of the remote cache.
The following example initializes an existing client cache myClientCache, performs some operations and uses the ClearClientCache()
method to empty the client cache synchronously. For Java, NCache provides the clearClientCache()
method for this purpose.
try
{
ICache cache = CacheManager.GetCache("myClientCache");
// clear the client cache
cache.ClearClientCache();
}
catch (Exception e)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Additional Resources
NCache provides sample application for Basic Operations on GitHub.
See Also
Add Data to Cache
Update Existing Data in Cache
Retrieve Existing Cache Data
Remove Data from Cache