Retrieve Data from Cache with Tags
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.
Pre-Requisites for Retrieving Items with Tags
- Include the following namespace in your application:
Alachisoft.NCache.Web.Caching
Alachisoft.NCache.Runtime
- The application must be connected to cache before performing the operation.
- Cache must be running.
- 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.
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.
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.
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 keys = cache.GetKeysByTag(tags[0]);
// Check if any keys are failed to retrieve
foreach (var key in keys)
{
// Perform operation according to your logic
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
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
Starting with NCache 4.9.1, NCache now provides support for retrieving keys from the cache using tags with wildcard expressions. The special characters supported in wild search by NCache are:
*
: Used as a substitute for zero or more characters in the string.?
: Used as a substitute for a single character in the string.
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 keys = cache.GetKeysByTag("*Customers");
// Retrieves all the keys associated with the tags
// 'East Coast Customers' and 'West Coast Customers'
ICollection keyscollection = cache.GetKeysByTag("??st Customers");
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
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
GetKeysByAnyTag is a method where multiple tags are provided and the key(s) matching ANY of the provided tags will be retrieved.
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.
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 keys = cache.GetKeysByAnyTag(tags);
// Check if any keys are failed to retrieve
foreach (var key in keys)
{
// Perform operation according to your logic
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
GetKeysByAllTags
GetKeysByAllTags is a method where multiple tags are provided and key(s) matching ALL of the provided tags will be retrieved.
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.
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 all of the Tags in the list 'tags' are retrieved
// Retrieved Cache keys are then stored in an ICollection
ICollection keys = cache.GetKeysByAllTag(tags);
// Check if any keys are failed to retrieve
foreach (var key in keys)
{
// Perform operation according to your logic
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
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.
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.
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 and values with the Tag 'Important Customers' are retrieved
// Retrieved Cached items are then stored in a Hashtable where keys are the keys
// of the cache item and values are the values inserted against the keys
Hashtable result = cache.GetByTag(tags[0]);
// Check if any items are failed to retrieve
foreach (Hashtable entry in result)
{
if (entry.Values is Customer)
{
Customer customer = entry.Values as Customer;
// Perform operations according to business logic
}
else
{
// Object not of Customer type
}
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
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 4.9.1 now provides support for retrieving keys from the cache using tags with wildcard expressions. The special characters supported in wild search by NCache are:
*
: Used as a substitute for zero or more characters in the string.?
: Used as a substitute for a single character in the string.
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'
Hashtable result = cache.GetByTag("*Customers");
// Retrieves all the data associated with the tags
// 'East Coast Customers' and 'West Coast Customers'
Hashtable dataResult = cache.GetByTag("??st Customers");
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
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
GetByAnyTag is a method 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.
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.
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 and values with any of the Tags in the tags list are retrieved
// Retrieved Cached items are then stored in a Hashtable where keys are the keys
// of the cache item and values are the values inserted against the keys
Hashtable result = cache.GetByAnyTag(tags);
// Check if any items are failed to retrieve
foreach (Hashtable entry in result)
{
if (entry.Values is Customer)
{
Customer customer = entry.Values as Customer;
// Perform operations according to business logic
}
else
{
// Object not of Customer type
}
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
GetByAllTags
GetByAllTags is a method 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.
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.
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 and values with all of the Tags in the tags list are retrieved
// Retrieved Cached items are then stored in a Hashtable where keys are the keys
// of the cache item and values are the values inserted against the keys
Hashtable result = cache.GetByAllTags(tags);
// Check if any items are failed to retrieve
foreach (Hashtable entry in result)
{
if (entry.Values is Customer)
{
Customer customer = entry.Values as Customer;
// Perform operations according to business logic
}
else
{
// Object not of Customer type
}
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
See Also
Using Groups
Add/Update Data in Cache with Tags
Remove Data from Cache with Tags
Search Data in Cache with SQL and Tags
Delete Data in Cache with SQL and Tags
Named Tags