NCache 4.6 - Online Documentation

Sample Implementation of IReadThruProvider Interface

 
      To utilize the IReadThruProvider, include the following namespace in your application: Alachisoft.NCache.Runtime.DatasourceProviders.
 
    // Contains methods used to read an object by its key from the master data source.
    public class SampleReadThruProvider : IReadThruProvider
    {
        private SqlConnection _connection;
        //Perform tasks like allocating resources or acquiring connections
 
        public void Init(IDictionary parameters, string cacheId)
        {
            object connStringObject = parameters["connstring"];
            string connString = connStringObject == null ? "" : connStringObject.ToString();
            if (connString != "")
                _connection = new SqlConnection(connString);
            try
            {
                _connection.Open();
            }
            catch (Exception ex)
            {
                //handle exception
            }
        }
 
        //Responsible for loading an item from the external data source.
        public void LoadFromSource(string key, out ProviderCacheItem cacheItem)
        {
            //where LoadFromDataSource is the dummy method to load data from data source.
            object value = LoadFromDataSource(key);
 
            cacheItem = new ProviderCacheItem(value);
            cacheItem.ResyncItemOnExpiration = true;
        }
 
        //Perform tasks associated with freeing, releasing, or resetting resources.
        public void Dispose()
        {
            if (_connection != null)
                _connection.Close();
        }
 
        //Responsible for loading bulk of items from the external data source.
        public Dictionary<string, ProviderCacheItem> LoadFromSource(string[] keys)
        {
 
            Dictionary<string, ProviderCacheItem> dictionary = new Dictionary<string, ProviderCacheItem>();
            try
            {    
                string key = null;
                for (int index = 0; index < keys.Length; index++)
                {
                    key = keys[index];
 
                    //where LoadFromDataSource is the dummy method to load data from data source.
                    dictionary.Add(key, new ProviderCacheItem(LoadFromDataSource(key)));
                }
                return dictionary;
            }
            catch (Exception exp)
            {
                //handle exception
            }
 
            return dictionary;
        }
        private object LoadFromDataSource(string key)
        {
            object retrievedObject = null;
            // load item from your data source and populate retrieved Object
            return retrievedObject;
        }
    }
 
 
Make sure to deploy the Read-Through Provider after implementation using NCache Manager.
 
After implementing and deploying this provider, you can make direct calls to NCache to get the required items. If the item is not available in the cache store, NCache will use "LoadFromSource" method in the specified data access class to load items from data source.
 
 
See Also