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.
Key Takeaways
Logical Data Grouping: NCache provides Groups to create a 1-to-many relationship, allowing you to partition cached items into a single logical category (e.g., by region) for simplified retrieval or deletion.
Flexible Metadata Tagging: Tags allow for a many-to-many relationship where a single cached item can be associated with multiple keywords (e.g., “VIP” and “East Coast”), enhancing searchability across overlapping categories.
High-Precision Filtering: Named Tags extend tagging by supporting key-value pairs with primitive data types (int, string, date, etc.), enabling advanced SQL-like filtering based on specific attributes like discount rates or dates.
SQL-Powered Retrieval: All three categorization methods support SQL-like queries (using keywords like $Group$ and $Tag$), allowing developers to search the cache with familiar syntax without needing to know the exact cache keys.
Performance Optimization: Categorizing data in-memory reduces the load on backend databases by enabling complex searches directly within the cache, leading to faster data access and improved application scalability.
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"); } |
Choosing the Right Categorization
To help you decide which method fits your specific architectural needs, here is a quick comparison:
| Feature | Relationship | Search Method | Value Type | Use Case |
|---|---|---|---|---|
| Groups | 1 : Many | API / SQL | String Name | Regional Partitioning |
| Tags | Many : Many | API / SQL | String Label | Customer Personas |
| Named Tags | Many : Many | SQL Only | Key-Value (Typed) | Dynamic Filtering |
Conclusion
By utilizing these categorization features, NCache allows applications to perform complex searches in-memory, reducing expensive database trips by up to 80% and significantly decreasing latency for data-heavy operations. 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!
Frequently Asked Questions (FAQ)
Q: Can a single cache item belong to multiple Groups?
A: No. NCache enforces a strict one-level hierarchy for Groups, meaning each cache item can be associated with only one Group at a time. If you need an item to have multiple identifiers, you should use Tags or Named Tags.
Q: Is it possible to use Groups, Tags, and Named Tags together on the same item?
A: Yes. NCache allows you to assign a Group, an array of Tags, and a dictionary of Named Tags to a single CacheItem. This provides maximum flexibility for complex organizational and searching requirements.
Q: What happens if I try to add a Named Tag with a non-primitive data type?
A: Named Tags specifically support primitive data types (such as int, string, bool, decimal, DateTime, etc.). Attempting to use complex custom objects as a value in a Named Tag dictionary is not supported; for such cases, you should store the object as the cache value itself or use standard string Tags.
Q: Are these categorization attributes case-sensitive during a search?
A: In NCache, Groups are case-sensitive, meaning “EastCoast” and “eastcoast” would be treated as different groups. However, Tags and Named Tags are case-insensitive, offering more flexibility during query execution.
Q: Do I need to define these Tags or Groups in a configuration file before using them?
A: No. Unlike traditional database indexes that require predefined schemas, Groups, Tags, and Named Tags are “runtime indexes.” You define them programmatically within your application code when adding or updating items in the cache.






