Setup OleDb Database Dependency Environment [Deprecated]
              
Polling-based dependency enables you to keep the cache items synchronized with any of the databases. SQL and Oracle dependencies need SQL/Oracle databases to notify NCache about any change in the database. They use database notifications to notify of any modification. However, if the database in question does not provide a change notifications feature, NCache provides users with the facility to synchronize the cache with the database using polling-based dependency.
OleDb Dependency relies on polling to detect data changes. To use it, ensure that your data source supports triggers. Then, configure the environment for OleDb dependency by following the steps below.
Step 1: Create a Table in the Database
Create a table named ncache_db_sync in the database, which is then used by NCache to synchronize the database with the cache store. There are 4 columns in this table which are as follows:
cache_keycache_idmodifiedwork_in_progress
As soon as an item is added to the cache with a dependency, a row will be created in table ncache_db_sync for this cache key.
In case of a data change in the table, corresponding triggers will set a modified flag to true. On cache clean-up interval, NCache polls the ncache_db_sync table for modified keys, collects them, sets their work_in_progress flag to true, and finally removes all corresponding keys from the cache. After successfully removing keys from the cache, all those rows where the work_in_progress flag is true are removed from the ncache_db_sync table.
The following script is used to create the table:
CREATE TABLE ncache_db_sync(
cache_key VARCHAR(256),
cache_id VARCHAR(256),
modified BIT DEFAULT(0),
work_in_progress BIT DEFAULT(0),
PRIMARY KEY(cache_key, cache_id)
);
Important
The syntax of SQL scripts specified (on this page) is for the Oracle database. Please change the syntax according to the database being used in your application.
Step 2: Create a Trigger in the Database
Create an UPDATE and DELETE triggers for every table on which notification is required. They set the modified field of the corresponding row in the ncache_db_sync table. To carry out the task, see the following sample script that creates a trigger on the Products table:
CREATE TRIGGER myTrigger
ON dbo.Products
FOR DELETE, UPDATE
AS
UPDATE ncache_db_sync
SET modified = 1
FROM ncache_db_sync
INNER JOIN Deleted old ON cache_key = (Cast((old.ProductID) AS VARCHAR)+ ':dbo.Products' );
We will have to make sure that the format of the cache key while adding into the cache is the same as defined in the corresponding trigger. For example, in the trigger explained above, the cache key for product id 10 should be 10:dbo.Products.
On the clean interval, NCache DBCacheDependency does the following:
Sets the
work_in_progressflag for those rows where themodifiedflag is set and fetches all those rows.Removes all keys from the cache as they are all expired now.
After successfully removing all the keys from the cache, all those rows where the
work_in_progressflag is set are removed from thencache_db_synctable.
Once the environment is set, use the OleDb dependency using Sync Cache with OleDb in the Programmer's Guide.
See Also
Setup SQL Server Environment
Setup Oracle Database Environment
Setup SQL Server for CLR Procedures
Monitor Caches