With AppFabric’s market withdrawal, the .NET industry remains in the lurch for an in-memory distributed cache to emerge as a contender. While a few alternatives have come forward, none offer as complete of a solution as NCache.
NCache is an open-source in-memory distributed cache that provides users with an exceedingly fast and linearly scalable distributed cache that caches application data and reduces expensive database trips. NCache is a proven market commodity and predates App Fabric by about five years. And there are numerous advantages that await you at the end of your migration process, as we discuss below.
NCache Details AppFabric End AppFabric Migration
1. Keeping the Cache Fresh
Database synchronization is a crucial feature for a quality in-memory distributed cache. Since you usually find distributed caches in the middle tier between applications and a database, there is a high probability that data directly updates to the database or the cache independently. This issue calls for a mechanism to prevent stale data.
Therefore, such distributed caches must allow you to specify a relation or dependency between the item cached and its corresponding item in the database. So, whenever the database changes, its changes are reflected in the cache through either invalidation or updates.
For example, NCache provides SqlDependency support for SQL Server. You can create a dependency of a cached item with an SQL statement-based dataset. Whenever that specific dataset changes or is modified, the SQL Server sends an event to NCache, and the cache item is invalidated. For example, CacheItem item = newCacheItem(myObject);
1 2 3 4 5 6 7 8 9 10 11 |
// Get orders against customerID from DB var order = FetchOrderByCustomerID("ALFKI"); // Generate a unique cache key for the order string key = $"Order:{order.OrderID}"; CacheItem item = new CacheItem(order); // Create SQL query to select the data from the database string query = "SELECT CustomerID, Address, City FROM dbo.Customers WHERE CustomerID = 'ALFKI';"; //Specify the database connection string and the query to create and set SQLCacheDependency. item.Dependency = new SqlCacheDependency(dbConnString, query); // Insert the item into the cache cache.Insert(key, item); |
So, the cache will synchronize with the database. NCache also supports the same feature with Oracle, OleDB, and even non-relational data sources like FileSystems via our Persistence Store.
NCache Details Oracle Database OleDB
2. SQL Queries
Given NCache’s synchronization capability, it is easy to consider keeping all your data in the cache. Unfortunately, this would present a slight problem as searching is only possible through your cached data if fetching through Key is the only option. NCache offers a solution to this through SQL queries. This is possible by indexing selected .NET object attributes and performing search queries.
1 2 3 4 5 6 7 8 9 |
//string query = "SELECT * FROM FQN.Product WHERE UnitsInStock > ?"; var queryCommand = new QueryCommand(query); queryCommand.Parameters.Add("UnitsInStock",0); ICacheReader reader = cache.ExecuteReader(queryCommand); while (reader.Read()) { string ID = reader.GetValue("ProductID"); string Name = reader.GetValue("ProductName"); } |
To enhance the developers’ experience, NCache also lets you assign groups, tags, and named tags to your cached items which you can later use in your SQL queries to fetch items quickly.
3. LINQ Query
Similarly, NCache offers users the opportunity to traverse their data through LINQ queries, as demonstrated below.
1 2 3 4 |
IQueryable products = new NCacheQuery(cache); var result = from prod in products where prod.ProductID > 1001 select prod; |
Additionally, you can tailor these queries further using the identified Logical, Aggregation, and Wildcard Operations.
NCache Details SQL Queries LINQ Queries
4. Server-Side Code
Often, distributed caches just help applications fetch data from the database and insert it in the cache. However, another approach is where your application delegates this responsibility to the distributed cache. Essentially, when your application requests an item from the cache, it determines if the item in question exists in the cache or not and loads the corresponding data from the database. This feature is called read-through. NCache offers a similar write-through option, which allows write operations directly on the data source through the cache.
And NCache’s server-side functionality goes much further with the Cache Loader & Refresher. This feature successfully overcame the initial performance gap all caches face when starting up, as they are empty to begin with. You can use the loader to identify the data you need and preload it. The refresher simply exists to ensure this data doesn’t go stale.
NCahttps://www.alachisoft.com/resources/docs/ncache/prog-guide/server-side-api-programming.htmlche also provides a complete MapReduce framework where you can perform analysis on your cached data and produce actionable insights in real-time. MapReduce code is written in .NET and therefore runs within the NCache process.
NCache Details Map Reduce NCache Loader & Refresher
5. Client Cache (Near Cache)
A Client Cache, is local to your web server or application and lets you cache frequently used data within the same process or server machine. So, a Client Cache is an affinity cache, giving your application a further performance boost by decreasing network bandwidth costs without compromising data integrity. A Client Cache works with all NCache clustered caching topologies (Mirrored, Partitioned, and Partition-Replica Cache).
It does this by staying connected to the cache cluster and synchronizing by replicating the changes in the main cache cluster. The difference between AppFabric local cache and the NCache client cache is that AppFabric Local cache is stand-alone and does not create a connection with the main cluster.
6. Multi-Datacenter Support
WAN replication is another feature critical for high-caliber in-memory distributed caches that AppFabric lacks. It is most useful with geo-distributed data centers – for disaster recovery or load balancing for localized traffic.
NCache provides Bridge topologies to handle WAN latencies. The NCache Bridge replicates and maintains consistency across different caches across the globe. You either have the option to keep your cache cluster passive to mimic a cluster replica for disaster recovery or synchronize two separate caches at different data centers. Data asynchronously replicates over secure Internet connections.
NCache Details Client Cache WAN Replication
7. Dynamic Cluster
As all AppFabric customers are likely aware, their cluster is not fully dynamic. Microsoft’s dependency on its lead-hosts-majority-rule means a very fallible cluster, that goes down fairly easily if even one of the lead hosts goes down. These lead host nodes also resemble the master-and-slave architecture and are not fully peer-to-peer either.
Alternatively, NCache is highly dynamic and lets you add or remove cache servers at runtime without interruption. Data is automatically rebalanced (or state-transferred) at runtime without any performance degradation. NCache clients automatically maintain the communication channel with the cache servers, independent of the server state. Additionally, its cache clusters ensure the execution of client operations even when data balancing (state transfer) is in process.
This means no master or slave nodes exist in the cluster. There is a primary coordinator node which is the senior node. And if it goes down, the next most senior node automatically becomes the primary coordinator. All of this happens without any interruption to the client’s operations.
8. Third-Party Integrations
To top it off, NCache officially supports providers for different important third-party libraries, such as:
- Entity Framework Core Cache
- Entity Framework Cache
- NHibernate 2nd level Cache
- ASP Session State & View State Caching
Conclusion
In short, NCache presents a wide range of features that businesses use and require in their .NET Applications. Particularly, ones that AppFabric didn’t get around to introducing at any point in their lifecycle. So, if you want to put your best foot forward and invest in a distributed caching system that really caters to your needs, download NCache for a painless migration.
NCache Details Download NCache Edition Comparison