Modern problems require modern solutions, right? And fast performance has always been a problem – call it trivial or modern. NCache is a quick fix for your critical problems such as performance and scalability. It works efficiently and covers all the problematic areas in your application, however, caching has its primary purpose for applications that are more read intensive. This blog thoroughly explains how NCache is the best fit for your read intensive applications.
The Need of Caching
Let’s start with the beginning and see why is caching needed in the first place? Its simple answer is the databases’ response time. It is a given that the primary storage (your database) containing all the data at one place can choke due to a higher response rate. What solves it? Caching, as it comes as an in-memory storage and provides you a faster response time.
Secondly, cache being distributed in nature, has no single point of failure unlike database. Your data is distributed and that increases the scalability. The load is divided and thus less chances of requests making the servers choke.

Figure 1: Read Intensive Applications with NCache
Given the above facts, we have established that NCache is an in-memory storage and your data is a part of the cache. So, caching is the best fit for situations where your write intensity is not very high and the read intensity is relatively higher. Let us take an example of a website containing information about the transport system. The bus fares or the timings are usually static and there is a very rare chance of fares being updated. Such applications having more read intensive data have a rich set of features provided by NCache for performing faster read operations.
NCache as a Read Intensive Cache Store
Applications having a read intensive nature have NCache to perform all the read operations at a much faster rate. NCache has a rich set of features that cater to any kind of read operations albeit fetching a single item or fetching items in bulk. Once the data is added to the cache from the database, all the fetch operations go directly to the cache for higher performance and scalability.
Get Operations in NCache
NCache provides various overloads of Get() method to fetch data from the cache against a specific key. These operations are synchronous in nature and returns a type object casted according to your custom logic. In case no item exists in the cache, it returns null. Look at the code below to show the normal Get() method fetching a single item from the cache:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // Get key to fetch from cache string key = $"Product:{product.ProductID}"; // Get item against key from cache in template type var retrievedItem = cache.Get(key); if (retrievedItem != null) { if (retrievedItem is Product) { // Perform operations according to business logic } } else { // Null returned; no key exists } |
Get Bulk items from Cache
Similarly bulk items can also be fetched from the cache using the GetBulk() method. Using this, you can fetch a bulk number of items from the cache using a single call. You can then cast the returned template as your custom type. Look at the code below to see how can items be fetched in bulk. Items are fetched from the database and then all the further read operation calls are sent directly to the cache.
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 33 34 35 36 | // Pre-condition: Cache is already connected // The command below loads data into cache from database Product[] products = FetchProductsFromDB(); // Get keys to fetch from cache string[] keys = new string[products.Length]; int index = 0; foreach (var product in products) { keys[index] = $"Product:{product.ProductID}"; index++; } // Get bulk from cache IDictionary<string, Product> retrievedItems = cache.GetBulk(keys); // Check if any keys have failed to be retrieved if (retrievedItems.Count == keys.Length) { foreach (KeyValuePair<string, Product> entry in retrievedItems) { if (entry.Value is Product) { // Perform operations according to business logic } else { // Object not of Product type } } } else { // Not all of the keys are present in cache } |
Fetch Items using Keywords
You can also fetch items using keywords attached to them for an effective search. You mark your data with certain string-based keywords and retrieve the items with the help of these keywords. For example, you want to group certain customers using the region they belong to such as East Coast or West Coast. The certain feature set provided by NCache for this purpose is:
Let us look at the code below to see how you add named tags with an item and then fetch the items using those named tags. This particular example fetched items with a flash discount on them.
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 | // Items are already present in the cache with named tags string query = "SELECT $Value$ FROM FQN.Product WHERE FlashSaleDiscount = ?"; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); queryCommand.Parameters.Add("FlashSaleDiscount", 0.5); // Executing query ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Check if result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set Product result = reader.GetValue(1); // reader.GetValue(0) = Cache key // reader.GetValue(1) = Product object } } else { // Null query result set retrieved } |
Search Cache using SQL
When it comes to fetching items from a particular database, queries are an effective way to search with the certain criteria. NCache fully understanding the need of queries lets you fetch items from the cache using SQL like queries. You need to index the particular item in the cache as cache does not create indexes on its own unlike database. But you are good to go with SQL search once data is indexed. Following are the things supported in NCache queries:
- Basic query operators (=, ==, !=. LIKE, NOT LIKE)
- Logical operators (AND, OR, NOT)
- Aggregate Functions (SUM, COUNT, AVG, MIN, MAX)
- Miscellaneous
Look at the code below to see how SQL like queries are used to search through the cache. The particular example shows the GROUP BY command where the products with price greater than 10 and the category Beverages are retrieved and grouped.
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 | // Specify the query with the specified criteria // Use the Fully Qualified Name (FQN) of your own custom class string query = "SELECT Category, COUNT(*) FROM Product.Products WHERE UnitPrice > 5 Group By Category"; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); // Executing QueryCommand through ICacheReader ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Check if result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set int result = reader.GetValue(1); string category = reader.GetValue(0); } } else { // Null query result set retrieved } |
Search Cache using LINQ
Similar to SQL, NCache provides support for writing queries in LINQ format to search the cache. NCache LINQ provider converts LINQ related query into NCache’s extended SQL format and returns the results accordingly after transforming it in LINQ format. There are two types of syntax for writing LINQ query:
- Lambda Expression
- Simple LINQ query expression
However, just like SQL you need to define indexes for the searchable cache data for your query to run smoothly. Look at the code example below to see how LINQ query is used to fetch an object from the cache.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // Create custom class LINQ object IQueryable products = new NCacheQuery(cache); // LINQ query to search cache var result = from prod in products where prod.ProductID == 1001 select prod; if (result != null) { foreach (Product product in result) { // Perform operations } } else { // No result in the cache related to query } |
Concluding the Blog
Learning about read intensive application and increasing their performance, NCache comes off as a powerful caching solution. It boosts the speed, enhancing the performance, scalability and reliability with a rich set of features. Please check out other NCache features from our website.