Remove Data from Cache
NCache provides a Remove' and a
Delete` method for the removal of an object from the cache. The difference between these methods 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.
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 serialized or registered with NCache Compact Serialization format.
- Make sure that the cache is running.
Using Remove Method
The Remove
method removes the key from the cache and returns the removed object to the application, where NCache’s JCache API provider remove method behaves as per JCache’s specifications.
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.
}
Using Delete Method
The Delete
method deletes the key from the cache. You will need to get NCache API’s cache instance to perform this operation.
// Specify the key to be deleted
String key = "Product:1001";
// Delete the key from the cache
cache.delete(key);
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 pass onto the cache via the API will first be checked into and deleted from the Cache and irrespective of the key mapping, the call will be forwarded to the backing source.
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());
Remove Items from Cache Asynchronously
NCache provides the AsyncItemRemovedCallback
interface to be used to receive notification upon completion of item removal. Add the following package to your application to utilize this API.
String key = "Product:1001";
EventListener listener = new EventListener();
cache.removeAsync(key, (AsyncItemRemovedCallback) listener, DSWriteOption.None, null);
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