Efficient data management necessitates the ability to quickly search for application data based on specific attributes, a need that becomes increasingly crucial as the data volume grows. Typically, databases use predefined fields for categorizing data which can also serve as indexes when caching data. However, there are times when grouping data is required based on attributes that are not explicitly present in the cache, such as tags or custom attributes.
NCache allows you to categorize and search your application data based on custom attributes. Along with 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 cached items that can be identified by a Group name, you can create a Group. This name can help retrieve any item associated with that Group. For instance, you can create a Group with all the products associated with a certain customer or product category. Simply calling the name will cause NCache to return all the cached items affiliated with it. Please note that each item can only be associated with one Group. The example given below shows how customers from a specific 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 |
// Create a unique cache key for this customer. string customerKey = $"Customer:ALFKI"; // Get customer from cache Customer customer = cache.Get(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); } |
Moreover, you can also use SQL queries to search for cache objects contained in Groups using the keyword $Group$. The following code example demonstrates how to use a SQL query to view all 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 |
// 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("CustomerID"); string customerName = reader.GetValue("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, boosting application scalability. Furthermore, Groups can cache commonly used data that match 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 associated cached items. Tags help relate metadata to the appropriate objects. For example, you can add a customer from the East Coast region and categorize this customer further using a VIP Customer tag. This process enhances data search and retrieval.
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 using the 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 ("CustomerID"); string contactName = reader.GetValue("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 can be assigned 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, the CacheItem enables you to specify additional specifications related to an item. In the example given below, a customer with VIP membership (with a 10% discount) use Named Tags that are set by assigning them as a property of the CacheItem.
1 2 3 4 5 6 7 8 9 10 |
// 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 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.10 "; // 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("CustomerID"); string customerName = reader.GetValue("ContactName"); Console.WriteLine($"Customer '{customerName}' with ID '{customerID}' has VIP membership discount."); } } else { Console.WriteLine($"No VIP members found"); } |
Conclusion
NCache offers robust capabilities for effective data caching and searching through Groups, Tags, and Named Tags. This helps 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, as they enable faster data access, reduce database load, and improve the overall application performance. Experience the full benefits of these features and download NCache today!