Remove Data from Cache
NCache provides a Remove and a Delete method for removal of an object from the cache. The difference between the Delete
method and Remove
method is stated below:
Delete
returns void after deletion of the item from the cache.Remove
deletes the data from the cache and returns the deleted item.
For Delete/Remove API calls, if a backing source is configured and Write-through is enabled, all the keys passed onto the Cache via the API will first be looked into and deleted from the Cache and irrespective of the key mapping, the call will be forwarded to the backing source.
Pre-Requisites
- Add the following Maven dependencies in your
pom.xml
file:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-client</artifactId>
<version>5.2.0</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.
Using Remove Method
The Remove method deletes the key from the cache and returns the deleted object to the application, where NCache’s JCache API provider remove method behaves as per JCache’s specifications.
try
{
String key = "Product:1001";
Product product = null;
// False is returned if key does not exist in cache
Boolean result = cache.remove(key);
if (result != true)
{
//Deleted.
}
else
{
//Failed.
}
}
catch(Exception ex)
{
// Handle Exception
}
Using Delete Method
The Delete
method deletes the key from the cache. You will need to get NCache API’s cache instance in order to perform this operation.
try
{
// Specify the key to be deleted
String key = "Product:1001";
// Delete the key from the cache
cache.delete(key);
}
catch(Exception ex)
{
// Handle Exception
}
Remove Bulk Data from Cache
Just like the atomic remove operation, removeAll
also returns removed items. Here the removed items are returned in the form of a HashMap, however NCache's JCache API’s removeAll
method can also be used for deleting items in bulk, which behaves as Oracle documents it.
For both, the atomic and the bulk Delete/Remove API calls, if a backing source is configured and Write-through is enabled, all the keys passed onto the Cache via the API will first be looked into and deleted from the Cache and irrespective of the key mapping, the call will be forwarded to the backing source.
try
{
Product product1 = null, product2 = null;
String[] keys = {"Product:1001", "Product:1002"};
CacheItem[] items = new CacheItem[2];
items[0] = new CacheItem(product1);
items[1] = new CacheItem(product2);
HashMap map = new HashMap();
for (int i = 0; i < 2; i++)
{
map.put(keys[i], items[i]);
}
jCache.removeAll(map.keySet());
}
catch(Exception ex)
{
// Handle Exception
}
Remove Items from Cache Asynchronously
NCache provides the AsyncItemRemovedCallback
interface to be used in order to receive notification upon completion of item removal. Add the following package in your application to utilize this API.
try
{
String key = "Product:1001";
EventListener listener = new EventListener();
cache.removeAsync(key, (AsyncItemRemovedCallback) listener, DSWriteOption.None, null);
}
catch(Exception ex)
{
// Handle Exception
}
Create a callback to register the events for the items removed from the cache asynchronously.
public class EventListener implements AsyncItemRemovedCallback
{
@Override
public void asyncItemRemoved(String string, Object o)
{
if(o.toString().equals("Success"))
{
// do something
}
if(o.toString().equals("Failure"))
{
// do something
}
if(o instanceof Exception)
{
// do something
}
}
}
See Also
Initialize Cache
Add/Update in Cache
Clear Cache
Hibernate Caching
NCache Java Session Module