Note
This feature is only available in NCache Enterprise Edition.
Once the data is tagged with one or multiple tags, the data can be retrieved using
these tags. Data can be retrieved by a specified tag or one or all tags.
Prerequisites
- Install the following NuGet package in your application:
- Include the following namespaces in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Caching
Alachisoft.NCache.Runtime.Exceptions
- Cache must be running.
- The application must be connected to cache before performing the operation.
- Make sure that the data being added is serializable.
- For API details refer to: ICache, Tag, GetKeysByTag, GetKeysByTags, GetByTag
- 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.
- Add the following Maven dependencies in your
pom.xml
file:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-client</artifactId>
<version>x.x.x</version>
</dependency>
- Import the following packages in your application:
import com.alachisoft.ncache.runtime.exceptions.*;
import com.alachisoft.ncache.runtime.caching.*;
import com.alachisoft.ncache.client.*;
- Cache must be running.
- The application must be connected to cache before performing the operation.
- Make sure that the data being added is serializable.
- For API details refer to: Cache, Tag, getKeysByTag, getKeysByTags, getByTag, getByTags
- 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.
- Install and include the following module in your application:
- Enterprise:
const ncache = require('ncache-client')
- Cache must be running.
- The application must be connected to cache before performing the operation.
- For API details refer to: Cache, Tag, getKeysByTag, getKeysByTags, getByTag, getByTags
- 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.
- Install the NCache Python client by executing the following command:
# Enterprise Client
pip install ncache-client
- Import the NCache module in your application.
- Cache must be running.
- 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.
- Add the following Maven dependencies in your
pom.xml
file:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-scala-client</artifactId>
<version>x.x.x</version>
</dependency>
- Import the following packages in your application:
import com.alachisoft.ncache.scala.client.*;
- Cache must be running.
- The application must be connected to cache before performing the operation.
- Make sure that the data being added is serializable.
- 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.
Retrieve Keys Associated with a Tag
Keys can be retrieved from the cache by tags either by GetKeysByTag
, GetKeysByAnyTag
or GetKeysByAllTags
. All these methods are explained below.
GetKeysByTag
GetKeysByTag
is a method which retrieves the keys by one specified tag. The specified tag is the search criteria for the data in the cache. For Java, the method used for retrieving keys by tag is getKeysByTag
.
Following example retrieves the keys of the items associated with the specified tag from the cache.
Warning
Providing Null
tag array will throw an ArgumentNullException or NullPointerException.
try
{
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys with the Tag 'Important Customers' are retrieved
// Retrieved Cache keys are then stored in an ICollection
ICollection<string> retrievedKeys = cache.SearchService.GetKeysByTag(tags[0]);
if (retrievedKeys != null && retrievedKeys.Count > 0)
{
// Iterate over key result
foreach (var key in retrievedKeys)
{
// Perform operations
}
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
try
{
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys with the Tag 'Important Customers' are retrieved
// Retrieved Cache keys are then stored in an ICollection
Collection retrievedKeys = cache.getKeysByTag(tags[0]);
// Iterate over key result
if (retrievedKeys != null)
{
for (Object keys : retrievedKeys)
{
// Perform operations
}
}
}
catch (Exception ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
// Any generic exception
}
// This is an async method
try
{
// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
// Keys with the Tag 'Important Customers' are retrieved
// Retrieved Cache keys are then stored in an ICollection
var searchService = await this.cache.getSearchService();
var retrievedKeys = await searchService.getKeysByTags(tags[0]);
if (retrievedKeys != null && retrievedKeys.size() > 0)
{
// Iterate over key result
retrievedKeys.forEach(key => {
// Perform operations
});
}
}
catch(error)
{
// Handle errors
}
try:
# Following tags are created and item is added in the cache with these tags
tags = [
ncache.Tag("Important Customers"),
ncache.Tag("East Coast Customers")
]
# Keys with the Tag 'Important Customers' are retrieved
# Retrieved Cache keys are then stored in a list
search_service = cache.get_search_service()
retrieved_keys = search_service.get_keys_by_tag(tags[0])
if retrieved_keys is not None and len(retrieved_keys) > 0:
# Iterate over key result
for key in retrieved_keys:
# Perform operations
print(key)
except Exception as exp:
# Handle errors
try {
// Following tags are created and item is added in the cache with these tags
var tags: List[Tag] = List()
tags = Tag("Important Customers") :: tags
tags = Tag("East Coast Customers") :: tags
// Keys with the Tag 'Important Customers' are retrieved
// Retrieved Cache keys are then stored in a Collection
val retrievedKeys = cache.getSearchService.getKeysByTag(tags.head)
// Iterate over key result
if (retrievedKeys != null) {
for (keys <- retrievedKeys) {
// Perform operations
}
}
}
catch {
case exception: Exception => {
// Handle any errors
}
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
GetKeysByTag with Wildcard expressions
NCache also provides support for retrieving keys from the cache using tags with wildcard expressions using the method getKeysByTag(Wildcard). The special characters supported in wild search by NCache are:
The following example retrieves the keys of the items associated with tags along with wild card expressions.
try
{
// Pre-condition: Cache is already connected
// Following tags are created and items are added in the cache with these tags
Tag[] tags = new Tag[3];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
tags[2] = new Tag("West Coast Customers");
// Retrieves all the keys associated with customers
// containing any prefix before customer
// i.e 'Important Customers' as well as 'East Coast Customers' and 'West Coast Customers'
ICollection<string> retrieveKeys = cache.SearchService.GetKeysByTag("*Customers");
if (retrieveKeys != null && retrieveKeys.Count > 0)
{
// Iterate over key result
foreach (var key in retrieveKeys)
{
// Perform operations
}
}
// Retrieves all the keys associated with the tags
// 'East Coast Customers' and 'West Coast Customers'
ICollection<string> retrieveCoastCustomersKeys = search.GetKeysByTag("??st Customers");
if (retrieveCoastCustomersKeys != null && retrieveCoastCustomersKeys.Count > 0)
{
// Perform operations as done for 'Customers'
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
try
{
// Pre-condition: Cache is already connected
// Following tags are created and items are added in the cache with these tags
Tag[] tags = new Tag[3];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
tags[2] = new Tag("West Coast Customers");
// Retrieves all the keys associated with customers
// containing any prefix before customer
// i.e 'Important Customers' as well as 'East Coast Customers' and 'West Coast Customers'
Collection<string> retrieveKeys = cache.getSearchService().getKeysByTag("*Customers");
if (retrieveKeys != null )
{
// Iterate over key result
foreach (var key : retrieveKeys)
{
// Perform operations
}
}
// Retrieves all the keys associated with the tags
// 'East Coast Customers' and 'West Coast Customers'
ICollection<string> retrieveCoastCustomersKeys = cache.getKeysByTag("??st Customers");
if (retrieveCoastCustomersKeys != null)
{
// Perform operations as done for 'Customers'
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like NullPointerException or IllegalArgumentException
}
// This is an async method
try
{
// Pre-condition: Cache is already connected
// Following tags are created and items are added in the cache with these tags
var tags = new ncache.Tag[3];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
tags[2] = new ncache.Tag("West Coast Customers");
// Retrieves all the keys associated with customers
// containing any prefix before customer
// i.e 'Important Customers' as well as 'East Coast Customers' and 'West Coast Customers'
var searchService = await this.cache.getSearchService();
var retrieveKeys = await searchService.getKeysByTag("*Customers");
if (retrieveKeys != null && retrieveKeys.Count > 0)
{
// Iterate over key result
retrieveKeys.forEach(key => {
// Perform operations
});
}
// Retrieves all the keys associated with the tags
// 'East Coast Customers' and 'West Coast Customers'
var searchService = await this.cache.getSearchService();
var retrieveCoastCustomersKeys = await searchService.getByTags("??st Customers");
if (retrieveCoastCustomersKeys != null && retrieveCoastCustomersKeys.size() > 0)
{
// Perform operations as done for 'Customers'
}
}
catch(error)
{
// Handle errors
}
try:
# Following tags are created and item is added in the cache with these tags
tags = [
ncache.Tag("Important Customers"),
ncache.Tag("East Coast Customers"),
ncache.Tag("West Coast Customers")
]
# Keys with the Tag 'Important Customers' are retrieved
# Retrieved Cache keys are then stored in a list
search_service = cache.get_search_service()
retrieved_keys = search_service.get_keys_by_tag(None, "*Customers")
if retrieved_keys is not None and len(retrieved_keys) > 0:
# Iterate over key result
for key in retrieved_keys:
# Perform operations
print(key)
# Retrieve all the keys associated with the tags 'East Coast Customers' and 'West Coast Customers'
retrieved_coast_customers_keys = search_service.get_by_tag(None, "??st Customers")
if retrieved_coast_customers_keys is not None and len(retrieved_coast_customers_keys) > 0:
# Iterate over key result
for key in retrieved_coast_customers_keys:
# Perform operations
print(key)
except Exception as exp:
# Handle errors
try {
// Pre-condition: Cache is already connected
// Following tags are created and items are added in the cache with these tags
var tags: List[Tag] = List()
tags = Tag("Important Customers") :: tags
tags = Tag("East Coast Customers") :: tags
tags = Tag("West Coast Customers") :: tags
// Retrieves all the keys associated with customers
// containing any prefix before customer
// i.e 'Important Customers' as well as 'East Coast Customers' and 'West Coast Customers'
var retrievedKeys = cache.getSearchService.getKeysByTag("*Customers");
if (retrievedKeys != null )
{
// Iterate over key result
for (key <- retrievedKeys)
{
// Perform operations
}
}
// Retrieves all the keys associated with the tags
// 'East Coast Customers' and 'West Coast Customers'
var retrieveCoastCustomersKeys = cache.getSearchService.getKeysByTag("??st Customers");
if (retrieveCoastCustomersKeys != null)
{
// Perform operations as done for 'Customers'
}
}
catch {
case exception: Exception => {
// Handle any errors
}
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
GetKeysByAnyTag
GetKeysByTag
is a method with the search option as ByAnyTag
where multiple tags are provided and the key(s) matching ANY of the provided tags will be retrieved. In case of Java, getKeysByAnyTag
is used to retrieve keys matching any of the tags provided.
Following example retrieves the keys which matches ANY of the tag provided in the list tags
.
Warning
Providing Null
tag list will throw an ArgumentNullException or NullPointerException.
try
{
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys associated with any of the Tag in the list 'tags' are retrieved
// Retrieved Cache keys are then stored in an ICollection
ICollection<string> keys = cache.SearchService.GetKeysByTags(tags, TagSearchOptions.ByAnyTag);
// Check if any keys are failed to retrieve
if (keys != null && keys.Count > 0)
{
// Iterate over key result
foreach (var key in keys)
{
// Perform operations
}
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
try
{
// Following tags are created and items are added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys associated with any of the Tag in the list 'tags' are retrieved
// Retrieved Cache keys are then stored in a Collection
Collection retrievedKeys = cache.getKeysByAnyTag(productTags[0]);
// Iterate over key result
if (retrievedKeys != null)
{
for (Object key : retrievedKeys)
{
// Perform operations
}
}
}
catch (Exception ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
// Any generic exception
}
// This is an async method
try
{
// Pre-condition: Cache is already connected
// Following tags are created and items are added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
// Keys associated with any of the Tag in the list 'tags' are retrieved
// Retrieved Cache keys are then stored in an ICollection
var searchService = await this.cache.getSearchService();
var keys = await searchService.getByTags(tags,ncache.TagSearchOptions.ByAnyTag);
// Check if any keys are failed to retrieve
if (keys != null && keys.size() > 0)
{
// Iterate over key result
keys.forEach(key => {
// Perform operations
});
}
}
catch(error)
{
// Handle errors
}
try:
# Pre-condition: Cache is already connected
# Following tags are created and items are added in the cache with these tags
tags = [
ncache.Tag("Important Customers"),
ncache.Tag("East Coast Customers")
]
# Keys associated with any of the Tag in the list 'tags' are retrieved
# Retrieved Cache keys are then stored in an ICollection
search_service = cache.get_search_service()
keys = search_service.get_by_tags(tags, ncache.TagSearchOptions.BY_ANY_TAG)
# Check if any keys failed to be retrieved
if keys is not None and len(keys) > 0:
# Iterate over key result
for key in keys:
# Perform operations
print(key)
except Exception as exp:
# Handle errors
try {
// Following tags are created and items are added in the cache with these tags
var tags: List[Tag] = List()
tags = Tag("Important Customers") :: tags
tags = Tag("East Coast Customers") :: tags
// Keys associated with any of the Tag in the list 'tags' are retrieved
// Retrieved Cache keys are then stored in a Collection
val retrievedKeys = cache.getSearchService.getByTags(tags, TagSearchOptions.ByAnyTag)
// Iterate over key result
if (retrievedKeys != null) {
for (key <- retrievedKeys) {
// Perform operations
}
}
}
catch {
case exception: Exception => {
// Handle any errors
}
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
GetKeysByTag
is a method with the search option as ByAllTags
where multiple tags are provided and key(s) matching ALL of the provided tags will be retrieved. For Java, getKeysByAllTags
is used to search for items matching all the tags provided.
The following example retrieves the keys of items in the cache containing ALL the tags in the list tags
.
Warning
Providing Null
tag list will throw an ArgumentNullException or NullPointerException.
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys associated with all of the Tags in the list 'tags' are retrieved
// Retrieved Cache keys are then stored in an ICollection
ICollection<string> keys = cache.SearchService.GetKeysByTags(tags, TagSearchOptions.ByAllTags);
// Check if any keys are failed to retrieve
if (keys != null && keys.Count > 0)
{
// Iterate over key result
foreach (var key in keys)
{
// Perform operations
}
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
try
{
// Following tags are created and items are added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys associated with all of the Tags in the list 'tags' are retrieved
// Retrieved Cache keys are then stored in an ICollection
Collection retrievedKeys = cache.getKeysByAllTags(tags[0]);
// Iterate over key result
if (retrievedKeys != null)
{
for (Object key : retrievedKeys)
{
// Perform operations
}
}
}
catch (Exception ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
// Any generic exception
}
// This is an async method
try
{
// Pre-condition: Cache is already connected
// Following tags are created and items are added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
// Keys associated with any of the Tag in the list 'tags' are retrieved
// Retrieved Cache keys are then stored in an ICollection
var searchService = await this.cache.getSearchService();
var keys = await searchService.getByTags(tags,ncache.TagSearchOptions.ByAllTags);
// Check if any keys are failed to retrieve
if (keys != null && keys.size() > 0)
{
// Iterate over key result
keys.forEach(key => {
// Perform operations
});
}
}
catch(error)
{
// Handle errors
}
try:
# Pre-condition: Cache is already connected
# Following tags are created and items are added in the cache with these tags
tags = [
ncache.Tag("Important Customers"),
ncache.Tag("East Coast Customers")
]
# Keys associated with any of the Tag in the list 'tags' are retrieved
# Retrieved Cache keys are then stored in an ICollection
search_service = cache.get_search_service()
keys = search_service.get_by_tags(tags, ncache.TagSearchOptions.BY_ALL_TAGS)
# Check if any keys failed to be retrieved
if keys is not None and len(keys) > 0:
# Iterate over key result
for key in keys:
# Perform operations
print(key)
except Exception as exp:
# Handle errors
try {
// Following tags are created and items are added in the cache with these tags
var tags: List[Tag] = List()
tags = Tag("Important Customers") :: tags
tags = Tag("East Coast Customers") :: tags
// Keys associated with all of the Tag in the list 'tags' are retrieved
// Retrieved Cache keys are then stored in a Collection
val retrievedKeys = cache.getSearchService.getByTags(tags, TagSearchOptions.ByAllTags)
// Iterate over key result
if (retrievedKeys != null) {
for (key <- retrievedKeys) {
// Perform operations
}
}
}
catch {
case exception: Exception => {
// Handle any errors
}
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Retrieve Cached Data from Cache Containing Tag
Similar to keys, cached items (keys and values) can also be retrieved from the cache
either by GetByTag
, GetByAnyTag
or GetByAllTags
. All these methods are explained below.
GetByTag
GetByTag
is a method in which a single tag is provided and all the ""keys and values"" associated with that tag are retrieved. In java section of the code, the method used for this purpose is getByTag
.
Following example retrieves the keys and values against these keys, associated with the tag Important Customers.
Warning
Providing Null
tag array will throw an ArgumentNullException or NullPointerException.
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys and values with the Tag 'Important Customers' and 'East Coast Customers' are retrieved
IDictionary<string, Customer> retrievedDictionary = cache.SearchService.GetByTags<Customer>(tags, TagSearchOptions.ByAllTags);
if (retrievedDictionary != null)
{
foreach (KeyValuePair<string, Customer> keyValuePair in retrievedDictionary)
{
//perform operations
}
}
else
{
// Object not of Customer type
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys and values with the Tag 'Important Customers' and 'East Coast Customers' are retrieved
HashMap result = cache.getByTag(tags[0]);
// Iterate over result
if (result != null)
{
for (KeyValuePair<string, Customer> keyValuePair : result)
{
// Perform operations
}
}
}
catch (Exception ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
// Any generic exception
}
// This is an async method
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
// Keys and values with the Tag 'Important Customers' and 'East Coast Customers' are retrieved
var searchService = await this.cache.getSearchService();
var retrievedList = await searchService.getByTags(tags,ncache.TagSearchOptions.ByAllTags);
if (retrievedList != null)
{
retrievedList.forEach(keyValuePair => {
// Perform operations
});
}
else
{
// Object not of Customer type
}
}
catch(error)
{
// Handle errors
}
try:
# Following tags are created and item is added in the cache with these tags
tags = [
ncache.Tag("Important Customers"),
ncache.Tag("East Coast Customers")
]
# Keys with the Tag 'Important Customers' are retrieved
# Retrieved Cache keys are then stored in a list
search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tag(tags[0])
if retrieved_items is not None and len(retrieved_items) > 0:
# Iterate over the result
for item in retrieved_items:
# Perform operations
print(retrieved_items[item])
except Exception as exp:
# Handle errors
try {
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
var tags: List[Tag] = List()
tags = Tag("Important Customers") :: tags
tags = Tag("East Coast Customers") :: tags
// Keys and values with the Tag 'Important Customers' and 'East Coast Customers' are retrieved
val result = cache.getSearchService.getByTag(tags.head)
// Iterate over result
if (result != null) {
for (keyValuePair <- result) {
// Perform operations
}
}
}
catch {
case exception: Exception => {
// Handle any errors
}
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
GetByTag with Wildcard expressions
NCache also provides support for retrieving keys from the cache using tags with wildcard expressions using the method getByTag(Wildcard). The special characters supported in wild search by NCache are:
The following example retrieves the data of the items associated with tags along with wild card expressions.
try
{
// Pre-condition: Cache is already connected
// Following tags are created and items are added in the cache with these tags
Tag[] tags = new Tag[3];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
tags[2] = new Tag("West Coast Customers");
// Retrieves all the data associated with customers
// containing any prefix before customer
// i.e 'Important Customers' as well as 'East Coast Customers' and 'West Coast Customers'
IDictionary<string, Customer> retrievedDictionary = cache.SearchService.GetByTag<Customer>("*Customers");
if (retrievedDictionary != null)
{
foreach (KeyValuePair<string, Customer> keyValuePair in retrievedDictionary)
{
//perform operations
}
}
else
{
// Object not of Customer type
}
// Retrieves all the data associated with the tags
// 'East Coast Customers' and 'West Coast Customers'
IDictionary<string, Customer> retrievedDictionaryEW = search.GetByTag<Customer>("??st Customers");
if (retrievedDictionary != null)
{
foreach (KeyValuePair<string, Customer> keyValuePair in retrievedDictionary)
{
//perform similar operations as done for "customers"
}
}
else
{
// Object not of Customer type
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
try
{
// Pre-condition: Cache is already connected
// Following tags are created and items are added in the cache with these tags
Tag[] tags = new Tag[3];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
tags[2] = new Tag("West Coast Customers");
// Retrieves all the data associated with customers
// containing any prefix before customer
// i.e 'Important Customers' as well as 'East Coast Customers' and 'West Coast Customers'
java.util.Map<String, Customer> retrievedDictionary = cache.getSearchService().<Customer>getByTag("*Customers");
Collection<String> keys = cache.getSearchService().<Customer>getKeysByTag("*Customers");
if (keys != null)
{
if(keys.size()>0)
{
retrievedDictionary = cache.removeBulk(keys,null);
}
for (java.util.Map.Entry<String, Customer> customer: retrievedDictionary.entrySet())
{
//perform operations
}
}
else
{
// Object not of Customer type
}
// Retrieves all the data associated with the tags
// 'East Coast Customers' and 'West Coast Customers'
java.util.Map<String, Customer> retrievedDictionaryEW = cache.getSearchService().<Customer>getByTag("??st Customers");
if (retrievedDictionary != null)
{
for (java.util.Map.Entry<String, Customer> customer : retrievedDictionaryEW.entrySet())
{
//perform similar operations as done for "customers"
}
}
else
{
// Object not of Customer type
}
}
catch (Exception ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
// This is an async method
try
{
// Pre-condition: Cache is already connected
// Following tags are created and items are added in the cache with these tags
var tags = new ncache.Tag[3];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
tags[2] = new ncache.Tag("West Coast Customers");
// Retrieves all the data associated with customers
// containing any prefix before customer
// i.e 'Important Customers' as well as 'East Coast Customers' and 'West Coast Customers'
var searchService = await this.cache.getSearchService();
var retrievedList = await searchService.getByTag("*Customers");
if (retrievedList != null)
{
retrievedList.forEach(keyValuePair => {
// Perform operations
});
}
else
{
// Object not of Customer type
}
// Retrieves all the data associated with the tags
// 'East Coast Customers' and 'West Coast Customers'
var searchService = await this.cache.getSearchService();
var retrievedListEW = await searchService.getByTag("??st Customers");
if(retrievedList != null)
{
retrievedList.forEach(keyValuePair => {
// Perform similar operations as done for "customers"
});
}
else
{
// Object not of Customer type
}
}
catch(error)
{
// Handle errors
}
try:
# Following tags are created and item is added in the cache with these tags
tags = [
ncache.Tag("Important Customers"),
ncache.Tag("East Coast Customers"),
ncache.Tag("West Coast Customers")
]
# Retrieves all the data associated with customers containing any prefix before customer i.e.
# 'Important Customers' as well as 'East Coast Customers' and 'West Coast Customers'
search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tag(None, "*Customers")
if retrieved_items is not None:
for item in retrieved_items:
# Perform operations
print(item)
else:
# Object not of Customer type
print("Object not found")
# Retrieve all the data associated with the tags 'East Coast Customers' and 'West Coast Customers'
retrieved_items_ew = search_service.get_by_tag(None, "??st Customers")
if retrieved_items_ew is not None:
for item in retrieved_items_ew:
# Perform operations
print(item)
else:
# Object not of Customer type
print("No object found")
except Exception as exp:
# Handle errors
try {
// Pre-condition: Cache is already connected
// Following tags are created and items are added in the cache with these tags
var tags: List[Tag] = List()
tags = Tag("Important Customers") :: tags
tags = Tag("East Coast Customers") :: tags
tags = Tag("West Coast Customers") :: tags
// Retrieves all the data associated with customers
// containing any prefix before customer
// i.e 'Important Customers' as well as 'East Coast Customers' and 'West Coast Customers'
var retrievedDictionary = cache.getSearchService.getByTag("*Customers")
if (retrievedDictionary != null) {
for (customer <- retrievedDictionary) {
//perform operations
}
}
else {
// Items not found
}
// Retrieves all the data associated with the tags
// 'East Coast Customers' and 'West Coast Customers'
val retrievedDictionaryEW = cache.getSearchService.getByTag("??st Customers")
if (retrievedDictionary != null) {
for (customer <- retrievedDictionaryEW) {
//perform similar operations
}
}
else {
}
}
catch {
case exception: Exception => {
// Handle any errors
}
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
GetByAnyTag
GetByTag
is a method and ByAnyTag
is the search option in which a list of tags is provided and all the ""keys and values"" associated with ANY of the tags in the tag list are retrieved. For Java, getByAnyTag
is used for retrieving keys and their values matching any of the tags provided.
Following example retrieves the keys and values against these keys, associated with any of the tags in the list tags
.
Warning
Providing Null
tag list will throw an ArgumentNullException or NullPointerException.
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys and values with any of the Tags in the tags list are retrieved
IDictionary<string, Customer> retrievedDictionary = cache.SearchService.GetByTags<Customer>(tags, TagSearchOptions.ByAnyTag);
if (retrievedDictionary != null)
{
foreach (KeyValuePair<string, Customer> keyValuePair in retrievedDictionary)
{
//perform operations
}
}
else
{
// Object not of Customer type
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys and values with any of the Tags in the tags list are retrieved
HashMap result = cache.getByAnyTag(tags);
// Iterate over result
if (result != null)
{
// Perform operations
}
}
catch (Exception ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
// Any generic exception
}
// This is an async method
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
// Keys and values with any of the Tags in the tags list are retrieved
var searchService = await this.cache.getSearchService();
var retrievedList = await searchService.getByTags(tags,ncache.TagSearchOptions.ByAnyTag);
if (retrievedList != null)
{
retrievedList.forEach(keyValuePair => {
// Perform operations
});
}
else
{
// Object not of Customer type
}
}
catch(error)
{
// Handle errors
}
try:
# Pre-condition: Cache is already connected
# Following tags are created and item is added in the cache with these tags
tags = [
ncache.Tag("Important Customers"),
ncache.Tag("East Coast Customers")
]
# Keys and values with any of the Tags in the tags list are retrieved
search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tags(tags, ncache.TagSearchOptions.BY_ANY_TAG)
if retrieved_items is not None:
for item in retrieved_items:
# Perform operations
print(item)
else:
# Object not of Customer type
print("Object not found")
except Exception as exp:
# Handle errors
try {
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
var tags: List[Tag] = List()
tags = Tag("Important Customers") :: tags
tags = Tag("East Coast Customers") :: tags
// Keys and values with any of the Tags in the tags list are retrieved
val result = cache.getSearchService.getByTags(tags, TagSearchOptions.ByAnyTag)
// Iterate over result
if (result != null) {
// Perform operations
}
}
catch {
case exception: Exception => {
// Handle any errors
}
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
GetByTag
is a method and ByAllTags
is the search option in which a list of tags is provided and all the ""keys and values"" associated with ALL of the tags in the tag list are retrieved. getByAllTags
is used to get they keys and values matching all the tags in case of Java.
Following example retrieves the keys and values against these keys, associated with all of the tags in the list tags
.
Warning
Providing Null
tag list will throw an ArgumentNullException or NullPointerException.
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys and values with all of the Tags in the tags list are retrieved
IDictionary<string, Customer> retrievedDictionary = cache.SearchService.GetByTags<Customer>(tags, TagSearchOptions.ByAllTags);
if (retrievedDictionary != null)
{
foreach (KeyValuePair<string, Customer> keyValuePair in retrievedDictionary)
{
// Perform operations
}
}
else
{
// Object not of Customer type
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys and values with all of the Tags in the tags list are retrieved
HashMap result = cache.getByAllTags(tags);
// Iterate over result
if (result != null)
{
// Perform operations
}
}
catch (Exception ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
// Any generic exception
}
// This is an async method
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
// Keys and values with any of the Tags in the tags list are retrieved
var searchService = await this.cache.getSearchService();
var retrievedList = await searchService.getByTags(tags,ncache.TagSearchOptions.ByAllTags);
if (retrievedList != null)
{
retrievedList.forEach(keyValuePair => {
// Perform operations
});
}
else
{
// Object not of Customer type
}
}
catch(error)
{
// Handle errors
}
try:
# Pre-condition: Cache is already connected
# Following tags are created and item is added in the cache with these tags
tags = [
ncache.Tag("Important Customers"),
ncache.Tag("East Coast Customers")
]
# Keys and values with any of the Tags in the tags list are retrieved
search_service = cache.get_search_service()
retrieved_items = search_service.get_by_tags(tags, ncache.TagSearchOptions.BY_ALL_TAGS)
if retrieved_items is not None:
for item in retrieved_items:
# Perform operations
print(item)
else:
# Object not of Customer type
print("Object not found")
except Exception as exp:
# Handle errors
try {
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
var tags: List[Tag] = List()
tags = Tag("Important Customers") :: tags
tags = Tag("East Coast Customers") :: tags
// Keys and values with any of the Tags in the tags list are retrieved
val result = cache.getSearchService.getByTags(tags, TagSearchOptions.ByAllTags)
// Iterate over result
if (result != null) {
// Perform operations
}
}
catch {
case exception: Exception => {
// Handle any errors
}
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Additional Resources
NCache provides sample application for Tags on GitHub.
See Also
Use Groups for Logical Data Grouping
Add/Update Cache Data with Tags
Remove Cache Data with Tags
Search Tag Data in Cache with SQL
Delete Tag Data from Cache with SQL
Named Tags with Cache Data