A cache is of no use without basic CRUD operations. Since the cache is a key-value store, you need to add, retrieve and remove cached data all the time. Hence, these key-based CRUD (Create, Remove, Update, Delete) or basic operations make your cache easy to access and use.
NCache has been widely adopted for distributed data caching in applications owing to its linear scalability and high availability. NCache provides various CRUD APIs to perform basic operations on caching data. We will see how easily you can use these operations in your e-commerce application using NCache to store frequently used data.
NCache Details Basic Cache Operations Cache Keys and Data
Basic CRUD Operations
The basic purpose of a cache is to act like a temporary data store for your application, so whatever data you need, you ping the cache instead of making a full trip to the database. Let’s take a tour of the basic CRUD operations that NCache supports. Here’s a brief list of what NCache offers:
API | Description | Syntax |
Add | Adds new data to the cache | Add(key, value) |
Insert | Updates the value if the key already exists in the cache, or adds the item as new otherwise | Insert(key, value) |
Remove | Removes the specified key from the cache and returns the value | Remove(key) |
Get | Retrieves the value of provided key | Get(key) |
But wait, it’s just not a simple key-value store. Besides, NCache also allows you to cache custom objects, JSON objects, data structures and perform operations on them. Hence, you have complete storage and a replication platform to perform basic CRUD operations on all sorts of data with the utmost flexibility using NCache.
NCache supports both synchronous and asynchronous operations. Synchronous operations are crucial for sensitive updates where you have to ensure that the operation you performed has taken place before you proceed further. In other words, such operations are sequential. On the other hand, Asynchronous operations are more flexible and performed in the background while the control is sent back to the client immediately.
It is important to note that, all NCache SDK client objects and APIs are thread-safe and independent of each other. This ensures that reusing client instances is always safe, even across threads.
Let’s go into a little depth of the different kinds of operations NCache supports for you to perform on your data seamlessly. Here, I will show you how to play around with basic operations on custom objects.
NCache Details Basic Cache Operations Cache Keys and Data
Types of Operations in NCache
NCache allows CRUD operations on a single item or a bulk of items. Meanwhile, supported operation modes are synchronous and asynchronous. NCache supports both synchronous and asynchronous operations. Let’s discuss different types of operations in little depth that NCache supports, for you to perform on your data seamlessly.
Atomic Operations
All the operations performed on single key-value pair are categorized as atomic operations. Hence, such operations require one cache call per item. Assuming that you have an e-commerce application, and you want to add a new product to the cached stock, fetch and update its value, and then remove it from the cache with NCache CRUD APIs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Pre-condition: Cache is already connected // Get product from database var product = GetProductFromDB(1001); string key = $"Product:{product.ProductID}"; // Add item in the cache. Add can be replaced with Insert depending on your requirement. CacheItemVersion version = cache.Add(key, product); // Retrieve cached item from the cache var retrievedItem = cache.Get(key); // Update the price of retrieved product retrievedItem.Price = 500; cache.Insert(key, retrievedItem); // Remove item from the cache cache.Remove(key); |
It is important to note that, the same key cannot be added again if it already exists in the cache. For that, you can use Insert which overwrites the value for the existing key.
NCache Details Basic Cache Operations Cache Keys and Data
Bulk Operations
Since you may need to perform multiple operations simultaneously in your application, NCache allows you to perform CRUD operations on a chunk of data in one call. These operations are designed to achieve better performance as they reduce network calls to the remote server by getting the most done in a single cache call.
For instance, if you want to add, update or delete 100 product items in the cache, you can do so via a single bulk call to add, update or remove these items instead of calling the relevant APIs 100 times. Let’s see how you can use a single bulk call to achieve this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Pre-condition: Cache is already connected // Get products from database Product[] products = FetchProductsFromDB(); IDictionary<string, CacheItem> dictionary = new Dictionary<string, CacheItem>(); foreach (var prod in products) { string key = $"Product:{prod.ProductID}"; var cacheItem = new CacheItem(prod); dictionary.Add(key, cacheItem); } // Inserting items in the cache IDictionary result = cache.InsertBulk(dictionary); // Retrieve items in bulk IDictionary<string, Product> retrievedItems = cache.GetBulk(keys); // Perform business logic here // Remove a chunk of keys from the cache cache.RemoveBulk(keys); |
NCache Details Basic Cache Operations Cache Keys and Data
Synchronous Operations
Synchronous operations are significant for sensitive updates where you have to ensure that the operation you performed has taken place before you proceed further. In other words, such operations are sequential. For example, you would use synchronous operations to update prices on your stock before a big sale to ensure that the prices have been successfully updated.
These operations work as a blocking call, where the client has to wait for the server response on the performed operation before further processing. What happens is that, until the time the item is added (or an exception is thrown), your application waits for the operation to be performed so that it gets the control back. The basic CRUD operations are synchronous by default if not stated otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Pre-condition: Cache is already connected // Get product from database var product = GetProductFromDB(1001); string key = $"Product:{product.ProductID}"; // Add CacheItem to cache CacheItemVersion version = cache.Add(key, cacheItem); // Retrieve the cached item from the cache CacheItem retrievedItem = cache.GetCacheItem(key); // Update the price of retrieved product product.Price = 500; retrievedItem = new CacheItem(product); cache.Insert(key, retrievedItem); // Remove item from the cache cache.Remove(key); |
Asynchronous Operations
Asynchronous operations are more flexible and performed in the background while the control is sent back to the client immediately. You can use this functionality in your e-commerce application to the fullest where lesser critical operations like updating the description of a product need to be performed. Changing the basic information doesn’t necessarily need to be tracked, not on high priority anyway.
NCache supports multiple asynchronous overloads to perform atomic operations. Let’s see how you can use AddAsync
, InsertAsync
, and RemoveAsync
APIs to add, update and remove your application’s data asynchronously.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Pre-condition: Cache is already connected // Get product from database var product = GetProductFromDB(1001); string key = $"Product:{product.ProductID}"; // Add item in the cache. AddAsync can be replaced with InsertAsync depending on your requirement. Task task = cache.AddAsync(key, product); // Wait before fetching data // Retrieve cached item from the cache var retrievedItem = cache.Get(key); // Update the price of retrieved product retrievedItem.Price = 500; // Add Product object to cache cache.InsertAsync(key, retrievedItem); // Remove item from the cache cache.RemoveAsync(key); |
Conclusion
Since you are now familiar with basic CRUD operations by NCache, you can easily start using them to enhance the performance of your distributed applications. What you were doing with your database can now be replicated to your cache simply and the result is enhanced user experience due to significantly reduced response time. You can see the advanced features of NCache for extensive usage in your web apps, microservices, libraries, and console applications. So, download NCache now with its free trial to explore more!