Multi-tier .NET applications are the real trend now-a-days. These .NET applications make heavy use of the database. But as you know the database trips are very expensive and it quickly becomes a scalability bottleneck. Therefore, many people are starting to use in-memory distributed cache in their .NET applications because a distributed cache can scale linearly and boost application performance.
With caching comes the issue of creating a copy of the data outside of the database that is in the cache. This data must be kept synchronized with the database in order to avoid data integrity issues.
If your .NET applications are using Oracle database either on Windows or Unix and you need to synchronize your distributed cache with the database, then you can use the Oracle Notification feature.
Oracle supports change notification on the basis of row-id and query result set. When any change happens to that row id or result set notification is fired. For using this functionality you have to enable change notifications on your database and register queries with handler functions. Than on any kind of change in the result set of that query registered notification handlers will be triggered. You can than implement how you will update your cache accordingly.
This is all good but it would be great if your in-memory distributed cache could take care of all this dependency change notifications and you didn’t have to implement this explicitly in your application. A good distributed cache like NCache provides you OracleCacheDependency. You need to enable notification on your Oracle database and add your cached item with OracleCacheDependency. NCache OracleCacheDependency is created with connection string of database and query on which change notification will be registered.
Here are the simple steps to use OracleCacheDependency in your application:
- Enable database change notifications to your user by executing this query. You just have to execute it once.
[SQL] "grant change notification to scott"
- Add following assemblies references in your application.
using Alachisoft.NCache.Web.Caching; using Alachisoft.NCache.Runtime.Dependencies; using Alachisoft.NCache.Runtime;
- Now initialize your cache, create OracleCacheDependency and add it with your item.
Cache _cache = NCache.InitializeCache("oracleDependencyCache"); _cache.Clear(); String connectionString = "User Id=scott;Password=test;Data Source=oracle"; String query = "SELECT CustomerID FROM Customers WHERE CustomerID = 006"; CacheDependency orclSync = new OracleCacheDependency(connectionString, query); _cache.Insert("Customer:006", "myValue", orclSync, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
When you add a cache item with OracleCacheDependency NCache will register it with your database. When any change to the registered query result set is triggered dependent cache item is removed from the cache. So for next time when needed, your application will reload the updated data in cache.
You can use NCache and try it yourself to see how easily NCache solves your database synchronization challenges.