NCache 4.3 - Online Documentation

Implementing IReadThruProvider

 
NOTE: This feature is not available in NCache Express and Professional edition.
 
Implementing Interface:
 
Following is a list of methods needs to be defined by the classes implementing interface and a brief description of their purpose.
 
  • void Init(System.Collections.IDictionary parameters, String cacheId);
 
This method is responsible for performing tasks like allocating resources, acquiring connections etc. When cache is initialized and read-through caching is enabled, it calls Init method to notify the client that cache has initialized and you may initialize your data source too. The parameters passed as an argument contains all the parameters (if any) that were specified using NCache Manager cache/cluster views along with cache name. These parameters can be utilized in many ways. For example, connection string of a datasource can be specified as a parameter through NCache Manager. Whenever cache will be initialized, the connection to the datasource could be made using this connection string passed as an argument. Thus, it provides a flexible way to change the datasource dynamically without requiring code changes.
 
public void Init(System.Collections.IDictionary parameters, String cacheId);
{
String connectionString = "Data Source=Localhost//SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True" ;
connection = new SqlConnection (connectionString);
connection.Open();
}
 
  • void Dispose();
 
This method is responsible for performing tasks like releasing the resources etc. When Cache is stopped it calls Dispose() method to notify the client that the cache has stopped now and you can free the resources related to your datasource.
 
public void Dispose()
{
connection.Close();
}
 
  • void LoadFromSource(string key, out Alachisoft.NCache.Runtime.Caching.ProviderCacheItem cacheItem);
 
This method should contain logic to load an object from the master datasource. Whenever a request for an item comes but item is not found in the cache, then cache calls the LoadFromSource method and passes the 'key' as an argument. It is the responsibility of a cache to return the object against the passed key from the master datasource. The other argument of this method is ProviderCacheItem.
ProviderCacheItem contains the following parameters:
 
  • AbsoluteExpiration/SlidingExpiration value for an item,
  • Dependency for cached item,
  • Group/SubGroup identifier for a cached item,
  • ItemPriority for an item to be evicted,
  • ResyncItemOnExpiration is a Boolean parameter, used for automatic reload of a cache item if required. If ResyncItemOnExpiration is enabled and cache item expires, it passes keys of the expired items to reload fresh items from the master datasource. In this way the required item remains in cache with updated values,
  • ResyncProviderName is a provider name which reloads the item,
  • Tags for a cached item and
  • Value for a cached item.
     
public void LoadFromSource(string key, out ProviderCacheItem provideritem)
{
    SqlCommand command = new SqlCommand("SELECT * FROM Products WHERE ProductName = " + key);
    SqlDataReader reader = command.ExecuteReader();
    reader.Read();
    Product product = new Product();
    product.ProductName = reader["ProductName"];
    product.Supplier = reader["Supplier"];
    product.Categories = reader["Categories"];
 
    ProviderCacheItem provideritem = new ProviderCacheItem(product);
 
    provideritem.Dependency = new KeyDependency("Product:1");
    provideritem.AbsoluteExpiration = System.DateTime.Now.AddSeconds(30);
 
    provideritem.Value = product;
}
 
  • Dictionary<string, ProviderCacheItem> LoadFromSource(string[] keys);
 
This method should contain logic to load array of objects from the master datasource. Whenever a request for items comes but items are not present in the cache, then cache calls LoadFromSource method and passes array of keys as argument.
 
      public Dictionary<string, ProviderCacheItem> LoadFromSource(string[] keys)
      {
          Dictionary<string, ProviderCacheItem> source = new Dictionary<string, ProviderCacheItem>();
             
          for (int i = 0; i < keys.Length; i++)
          {
              SqlCommand command = new SqlCommand("SELECT * FROM Products WHERE ProductName = " + keys[i]);
              SqlDataReader reader = command.ExecuteReader();
              reader.Read();
              Product product = new Product();
              product.ProductName = reader["ProductName"];
              product.Supplier = reader["Supplier"];
              product.Categories = reader["Categories"];
 
              provideritem = new ProviderCacheItem(product);
              provideritem.Dependency = new KeyDependency("Product:1");
              provideritem.AbsoluteExpiration = System.DateTime.Now.AddSeconds(30);
 
              source.Add(keys[i], provideritem));
          }
      }
 
Using Read-Thru Provider:
 
After implementing the above interface, you can make direct calls to NCache to get required items. If NCache does not have already loaded the items,   it will use "LoadFromSource" method in specified data access class to load items from datasource. The code to access the cache should look like;
 
Cache _cache = NCache.InitializeCache("myreplicatedcache");
object product = _cache.Get("ALFKI", "XmlReadThruProvider", DSReadOption.ReadThru);
 
If you do not want to give the provider name in the API, you can provide it in client.ncconf. If provider name is not provided in both API and client.ncconf, default provider will automatically be used.
 
<cache id = "myreplicatedcache" client-cache-id = "" default-readthru-provider = "XmlReadThruProvider" default-writethru-provider = "XmlWriteThruProvider">
<server name = "20.200.20.5">
</cache>
 
NCache logs the warnings in Application event log in case of an exception during loading the assemblies.
For debuging your providers see How to Debug Providers.
 
 
See Also
 
© Copyright  2014 Alachisoft. All rights reserved.