In the high-traffic applications prevalent these days, distributed caching is essential for achieving performance and scalability. By serving cached data from an in-memory store, distributed caches alleviate the burden on your database servers, offering faster data access and reduced latency. Some distributed caches even support SQL-Like queries, enabling you to query cached data like traditional databases, such as using SELECT employee WHERE employee.city = ‘New York’.
While many caches allow simple search criteria, they often lack support for more complex operations like aggregate functions. For example, queries like “SELECT COUNT(employee) WHERE salary > 1000” or “SELECT SUM(salary) WHERE employee.city = ‘New York’” require additional processing, as the cache may only support basic querying and not complex aggregations. To achieve this, you have to first query the distributed cache and then process the aggregate function on the data fetched from the cache.
Why use NCache to Query Distributed Cache Using Aggregate Functions
Traditional methods for querying distributed caches often require fetching large datasets, which can be costly in terms of time and network bandwidth—especially when most of the data isn’t needed after performing aggregate operations. Secondly, aggregate functions typically require custom programming. This not only increases development time but also limits the system’s ability to handle more complex scenarios effectively.
NCache addresses these challenges by directly supporting aggregate functions within its SQL-like (Object Query) querying language. With NCache Object Query Language (OQL), you can execute queries involving aggregate functions like:
- COUNT: Returns the total count of entries matching the criteria.
- SUM: Adds up values of a specified field.
- MIN: Retrieves the minimum value of a field.
- MAX: Retrieves the maximum value.
- AVG: Calculates the average value.
This built-in support eliminates the need to fetch unnecessary data, reduces network traffic, and improves performance by performing all the resulting operations within the cache. Here is a sample code to search NCache using OQL aggregate queries:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
var cache = CacheManager.GetCache("myPartitionReplicaCache"); var queryCommand = new QueryCommand("SELECT COUNT(Business.Product) WHERE this.ProductID > ? AND this.Price < ?"); // Providing parameters for Query query.Parameters.Add("ProductID", 100); query.Parameters.Add("Price", 50); // Executing QueryCommand through ICacheReader ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Check if the result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { string result = reader.GetValue<string>(0); // Perform operations using the retrieved keys } } else { // Null query result set retrieved } |
NCache uses parallel processing to distribute SQL-like queries across all cache servers, ensuring that queries are processed concurrently for faster results. Additionally, NCache’s OQL supports aggregate queries for both .NET and Java applications, providing flexible and efficient querying capabilities.
Conclusion
NCache offers both scalability and performance with the added flexibility of SQL-like aggregate functions for efficient data querying within the cache. This minimizes network traffic and enhances performance, allowing you to handle complex data operations seamlessly. Download NCache today to elevate your application’s speed and efficiency.