Efficient data management requires the ability to quickly search for application data based on specific attributes. As the volume of data grows, this need becomes even more crucial. Typically, databases already have fields that facilitate in the categorization of data, and these fields can be used as indexes when caching data. However, there may be instances where you want to group data based on attributes that are not present in the cache, such as tags or other custom attributes.
With NCache, you can not only categorize application data based on custom attributes, but you can also perform searches based on these attributes. In addition to traditional indexing, NCache offers the option to categorize data using Groups, Tags, and Named Tags, allowing for greater flexibility in data categorization and retrieval.
Cache Data Categorization via Groups
For a collection of items that have been cached and can be identified by a Group name, you can create a Group. This name can help retrieve any item that pertains to a Group. For instance, you can create a Group with all the products associated with a certain customer or a certain product category. Simply calling the name will cause NCache to return all the cached items associated with it. Please note there can only be a single item associated with a single Group. The example given below shows how customers from a different region, (East Coast in this case) are grouped in an online store.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Pre-conditions: Cache is already connected. // Create a unique cache key for this customer. string customerKey = $"Customer:ALFKI"; // Get customer from cache Customer customer = cache.Get<Customer>(customerKey); // Get customer from database if not found in cache if (customer == null) { customer = FetchCustomerFromDB("ALFKI"); // Create a new CacheItem var cacheItem = new CacheItem(customer); // Specify the group cacheItem.Group = "East Coast Customers"; // Add customer object to cache with group cache.Add(customerKey, cacheItem); } |
You can use SQL queries to search for cache objects contained in Groups using the keyword $Group$. The code example that follows, demonstrates how to use a SQL query to view all of the customers from a particular region—in this instance, the West Coast.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// Pre-conditions: Items with groups exist in the cache // Custom class is query indexed through the NCache Manager or config.ncconf // Create an SQL Query with the specified criteria // Make sure to use the Fully Qualified Name (FQN) string query = "SELECT CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE $Group$ = ? "; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); // Providing parameters for query queryCommand.Parameters.Add("$Group$", "West Coast Customers"); // Executing the Query ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Read results if result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set string customerID = reader.GetValue<string>("CustomerID"); string customerName = reader.GetValue<string>("ContactName"); Console.WriteLine($"Customer '{customerName}' having ID '{customerID}' belongs to West Coast."); } } else { Console.WriteLine($"No customers from West Coast found"); } |
In addition to increasing efficiency, Groups make it simple for users to retrieve or delete data based on these logical categories that increases the scalability of your application. Additionally, Groups can cache commonly used data that matches certain search criteria, avoiding the cost of running database searches repeatedly.
Cache Data Categorization via Tags
Tags in NCache provide a way to categorize cached items using multiple Tags. A cached object can have one or more Tags assigned to it, which can be used to retrieve the item. For instance, you can categorize items according to their region, language, or product features. Calling the name of a tag will cause NCache to return all items that have that tag and are cached. Tags let you link metadata to your cached objects, easing searching and data manipulation. The following example shows how a customer who has logged from the East Coast region will be added to the cache.
1 2 3 4 5 6 7 8 9 10 11 |
// Create an array of tags assigned to customer Tag[] tags = new Tag[2]; tags[0] = new Tag("East Coast Customers"); tags[1] = new Tag("VIP Customers"); // Setting the tag property of the cacheItem cacheItem.Tags = tags; cache.Add(customerKey, cacheItem); // CacheItem added successfully |
Similarly, you can query an item having a specific tag keyword $Tag$. The SQL query in the example below returns every item linked to the tag VIP Customers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Create a query for search string query = "Select CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE $Tag$ = ?"; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); queryCommand.Parameters.Add("$Tag$", "VIP Customers"); // Executing Query ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Read results if result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set string customerID = reader.GetValue<string> ("CustomerID"); string contactName = reader.GetValue<string>("ContactName"); Console.WriteLine($"Customer '{contactName}' having ID '{customerID}' is a VIP Customer."); } } else { Console.WriteLine($"No VIP Customers found"); } |
Using Tags give the user a high level of relevancy with each specific type. To know the other CRUD operations offered by Tags, please refer to the NCache documentation.
Cache Data Categorization via Named Tags
A cached object may be given multiple Named Tags, via a list, which may then be used to retrieve the item. Each named tag has two parameters: a key that is a string representing the tag’s name and a value that is any primitive type. For example, you can assign Named Tags to items based on their price range or availability. Calling the tag name and value will cause NCache to return all the stored items that have the specified named tag when you need to get all the items with that tag.
Similar to groups and tags, you can retrieve items from the cache with Named Tags using object queries. When certain Named Tags are already added to items in the cache, NCache offers the user the opportunity to update those Named tags. Using NCache’s custom class, i.e., CacheItem, you can add data to the cache. As a property of the NamedTags class, CacheItem additionally enables you to specify additional specifications related to an item. In the example given below, a customer with VIP membership (VIP members are offered 10% discount), the Named Tags are set by assigning them as a property of CacheItem.
1 2 3 4 5 6 7 8 9 |
// Creating a Named Tags Dictionary var namedTags = new NamedTagsDictionary(); // Adding Named Tags to the Dictionary where Keys are the names of the tags as string type and Values are of primitive type namedTags.Add("VIP_Membership_Discount", 0.10); // Setting the named tag property of the cacheItem cacheItem.NamedTags = namedTags; cache.Add(customerKey, cacheItem); |
When an item is added to the cache with the Named Tags, you can easily retrieve it through the SQL query. In the following example, the user wishes to see all of the customers who have a 12% VIP membership discount.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Create an SQL Query with the specified criteria string query = "SELECT CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE VIP_Membership_Discount = 0.12 "; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); // Executing the Query ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Read results if the result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set string customerID = reader.GetValue<string>("CustomerID"); string customerName = reader.GetValue<string>("ContactName"); Console.WriteLine($"Customer '{customerName}' with ID '{customerID}' has VIP membership discount."); } } else { Console.WriteLine($"No VIP members found"); } |
Conclusion
Clearly NCache offers a variety of strong capabilities for effective data caching and searching through Groups, Tags, and Named Tags to categorize cached data based on specific attributes and retrieve them using NCache APIs or SQL-like queries. These features make them an even more compelling option for caching since they enable faster data access, decreased database load, and increased application performance. Take advantage of these powerful features offered by NCache and start your free trial today!