Caching frequently used application data is a highly effective strategy. However, ensuring that the cached data stays synchronized with your Oracle database is a dilemma. A common practice is to set data expirations by estimating the TTL (Time to Live) for cached items. While this approach is widely used, it is based on approximations that may not always be accurate. For applications that rely on precise data, these inaccuracies can be costly.
A better solution is to use NCache’s database synchronization mechanism, which automatically updates or removes cache entries when the corresponding data in your database changes, ensuring data consistency and reliability. For applications using Oracle databases, NCache offers a specialized feature called Oracle Dependency. This mechanism integrates with your Oracle database to ensure that changes are immediately reflected in the cache, keeping your data accurate and improving application performance.
Using NCache to Sync with Oracle Server
NCache synchronizes cached data with Oracle Server using OracleDependency for notification-based dependencies. When registered, NCache is notified of any changes in the Oracle database, allowing it to keep cache data updated. It maps cached items to their corresponding database entries, invalidating and updating them when a change is detected. For this to work correctly, the query registered with OracleDependency must include the ROWID to ensure notifications are only triggered for relevant changes. Without ROWID, notifications could be fired for any row modification, leading to unnecessary cache invalidations. This synchronization mechanism ensures that the cache remains consistent with the database, maintaining data accuracy and reliability.
For more information on how to set up the Oracle database environment, refer to the NCache documentation.
Adding Data with Oracle Dependency using API
When you modify data with an Oracle Dependency, it is removed from the cache. By using the OracleCacheDependency, you can define the dependency, and then add the item to the cache with the Add or Insert method. The example below demonstrates adding data with a OracleCacheDependency using the Insert method. If the data already exists in the cache, the Insert method updates its properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Creating connection string to get connected with database. string connectionString = "your_connection_string_here"; // Getting products from database. List<Product> products = FetchProductFromDB(); foreach (Product product in products) { string productKey = $"Product: {product.ProductID}"; // Creating an Oracle dependency on the UnitPrice of product. Whenever the UnitPrice changes, the product is removed from the cache. string query = $"SELECT ROWID, UnitPrice FROM Products WHERE ProductID = {product.ProductID}"; // Creating dependency on fetched products. OracleCacheDependency dependency = new OracleCacheDependency(connectionString, query); CacheItem productItem = new CacheItem(product); // Adding Dependency to product item productItem.Dependency = dependency; // Adding CacheItem in cache cache.Add(productKey, productItem); } |
Stored Procedure Based Oracle Dependency
NCache supports Stored Procedure based Oracle Dependency, ideal if you want to keep all your Oracle queries within the database. These stored procedures are precompiled on the Oracle server, offering significantly faster execution than dynamic queries. You can specify an Oracle Dependency on a cache item using the OracleCacheDependency method, which allows for passing parameters to the stored procedure. The example below shows how to add items to the cache with Oracle Dependency using the Insert method, which updates existing items in the cache if they already exist.
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 |
// Creating connection string to get connected with database string connectionString = "your_connection_string_here"; string spGetUnitPriceByProductID = "sp_GetUnitPriceByProductID"; // Getting products from database List<Product> products = FetchProductFromDB(); foreach (Product product in products) { string productKey = $"Product: {product.ProductID}"; // Creating Param to be passed in stored procedure dictionary OracleCmdParams paramProductID = new OracleCmdParams { Type = OracleCmdParamsType.Int32, Value = product.ProductID }; // Creating stored procedure params Dictionary<string, OracleCmdParams> parameters = new Dictionary<string, OracleCmdParams>(); parameters.Add("@ProductID", paramProductID); CacheItem productItem = new CacheItem(product); // Creating an Oracle dependency on the UnitPrice of product. Whenever the UnitPrice changes, the product is removed from the cache OracleCacheDependency dependency = new OracleCacheDependency(connectionString, spGetUnitPriceByProductID, OracleCommandType.StoredProcedure, parameters); // Adding Dependency to product item productItem.Dependency = dependency; // Adding CacheItem in cache cache.Add(productKey, productItem); } |
Maintaining Data Integrity with NCache Oracle Dependency
In a .NET Core-based e-commerce application, accuracy is crucial for success, with thousands of users shopping online every minute. The application data is cached to ensure quick access, but this can lead to issues when the data changes in the database without the cache being updated. For example, if Customer A buys the last Xbox Series X, the database reflects this, but the cache does not, leading to a situation where Customer B also buys the same item, pays, and receives a confirmation, only to find out that the product is unavailable, resulting in frustration. To prevent such data integrity issues, NCache’s Oracle Dependency ensures the cache is automatically synchronized with the Oracle database whenever changes occur, maintaining consistency and avoiding customer dissatisfaction.
Conclusion
Ensuring your application remains reliable and accurate is crucial, and outdated data can lead to significant business setbacks. With NCache’s Oracle Dependency, you can maintain synchronization between your cache and Oracle database, protecting your application from the risks of stale data. This solution helps safeguard your business and reputation, ensuring your cache consistently supports, rather than undermines, your application’s performance.