Alachisoft NCache 4.1 - Online Documentation

Implementing IWriteThruProvider

 
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 class implementing interface and a brief description of their purpose.
 
  • void Start(IDictionary parameters);
 
This method is responsible for performing tasks like allocating resources, acquiring connections etc. When the cache is initialized and read-through caching is enabled, it calls Start method to notify the client that cache has initialized and you might 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. 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 Start(System.Collections. IDictionary parameters)
{
String connectionString = "Data Source=Localhost//SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True" ;
connection = new SqlConnection (connectionString);
connection.Open();
}
 
  • void Stop();
 
This method is responsible for performing tasks like releasing the resources etc. When Cache is stopped it calls Stop() method to notify the client that the cache has stopped now and you can free the resources related to your datasource.
 
public void Stop()
{
connection.Close();
}
 
  • bool AddToSource(string key, object value);
 
This method is responsible for adding an object to the datasource. Both the key and the value are passed as parameters. Object will not be added to cache if it fails to add in the data source.
 
public bool AddToSource(string key, object value)
{
Products product = ( Products )values;
SqlCommand command = new SqlCommand ( "INSERT INTO Products values " + "('" + keys + "'," + product.SupplierID + "," + product.CategoryID + ",null,null,null,null, null, 1, connection " );
command.ExecuteReader();
}
 
  • Hashtable AddToSource(string[] keys, object[] vals);
 
This method is responsible for adding array of objects to the datasource. Arrays of keys and values are passed as parameters. Objects that fail to add, will not be added to the data source.
 
public System.Collections.Hashtable AddToSource(string[] keys, object[] vals)
{
for (int i = 0; i < keys.Length; i++)
{
Products product = (Products)vals[i];
SqlCommand command = new SqlCommand("INSERT INTO Products values " + "('" + keys[i] + "'," + product.Supplier + "," + product.Category + ",null,null,null,null, null, 1, connection ");
command.ExecuteReader();
}
}
 
  • bool UpdateSource(string key, object val);
 
This method is responsible for updating an object in the datasource. Both, keys and values are passed as parameters. Object will not be updated in cache if it fails to update in the data source.
 
public bool UpdateSource( string key, object value)
{
Product product = ( Product )vals;
SqlCommand command = new SqlCommand ( "UPDATE Products SET ProductName = '" + product.ProductName + "' WHERE ProductName = '" + keys + "'" );
command.ExecuteReader();
return true;
}
 
  • Hashtable UpdateSource(string[] keys, object[] vals);
 
This method is responsible for updating array of objects in the datasource. Arrays of keys and values are passed as parameters. Object will not be updated in cache if it fails to update in the data source.
 
public System.Collections.Hashtable UpdateSource( string [] keys, object [] vals)
{
for ( int i = 0; i < keys.Length; i++)
{
Product product = (Product )vals[i];
SqlCommand command = new SqlCommand ( "UPDATE Products SET ProductName = '" + product.ProductName + "' WHERE ProductName = '" + keys[i] + "'" );
command.ExecuteReader();
}
}
 
  • bool RemoveFromSource(string key);
 
This method is responsible for removing an object from datasource. Key is passed as parameter. Object will not be removed from cache if it fails to remove from the data source.
 
public bool RemoveFromSource( string key)
{
SqlCommand command = new SqlCommand ( "DELETE FROM Products WHERE ProductName = " + keys);
command.ExecuteReader();
}
 
  • Hashtable RemoveFromSource(string[] keys);
 
This method is responsible for removing array of objects from datasource. Array of keys is passed as parameter. Object will not be removed from cache if it fails to remove from the data source.
 
public System.Collections.Hashtable RemoveFromSource(string[] keys)
{
for (int i = 0; i < keys.Length; i++)
{
SqlCommand command = new SqlCommand("DELETE FROM Products WHERE ProductName = " + keys[i]);
command.ExecuteReader();
}
}
 
  • bool Clear();
 
This method is responsible for clearing datasource. Cache will not be cleared if it fails to clear the data source.
 
public bool Clear()
{
SqlCommand command = new SqlCommand ( "DELETE * From Products" );
command.ExecuteReader();
}
 
 
Using Write-Thru Provider:
 
The code to access the cache should look like:
 
Cache mycache = NCache.InitializeCache("myreplicatedcache");
Product product = new Product();
CacheItem citem = new CacheItem (product);
cache.Add(textBox.Text, citem, DSWriteOption.WriteThru, null);
 
If you do not want to give provider name in 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.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.