Alachisoft NCache 4.1 - Online Documentation

Implementing WriteThruProvider

 
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(HashMap parameters);
 
This method performs 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(HashMap parameters) throws Exception
    {
        String connectionString =
  "jdbc:sqlserver://localhost:1433;databaseName=Northwind;user=xxxx;password=xxxx";
        connection = DriverManager.getConnection(connectionString);
    }
 
  • 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() throws Exception
    {
      connection.close();
    }
 
  • bool addToSource(String key, Object value);
 
This method is responsible for adding an object to the datasource. Both the key and value are passed as parameters. Object will not be added to cache if it fails to add in the data source.
 
public boolean addToSource(String key, Object value) throws Exception {
        try
        {
            Statement command = connection.createStatement();
 
            // Prepare a statement to update a record
            Product product = (Product) value;
      String sql = "INSERT INTO Products values " + "('" + key + "'," + product.Supplier +","
      + product.Categories + ",null,null,null,null, null, 1 ";
 
            // Execute the insert statement
            int count= command.executeUpdate(sql);
            // count contains the number of updated rows
            if(count > 0)
                return true;
        }
        catch (SQLException sqlException)
        {
            throw sqlException;
        }
        return false;
    }
 
  • HashMape 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 HashMap addToSource(String[] keys, Object[] vals) throws Exception {
 
            HashMap mapException = new HashMap();
 
            for(int index=0; index < keys.length; index++)
            {
                try
                {
                    Statement command = connection.createStatement();
                    // Prepare a statement to update a record
                    Product product = (Product) vals[index];
                    String sql = "INSERT INTO Products values " + "('" + keys[index] + "'," +
product.Supplier + "," + product.Categories + ",null,null,null,null, null, 1 ";
 
                    command.executeUpdate(sql);
                }
                catch (SQLException sqlException)
                {
                    mapException.put(keys[index], sqlException);
                }
            }
        return mapException;
    }
 
  • bool updateSource(String key, Object value);
 
This method is responsible for updating an object in the datasource. Both the key and value are passed as parameters. Object will not be updated in cache if it fails to update in the data source.
 
public boolean updateSource(String key, Object value) throws Exception {
        try
        {
            Statement command = connection.createStatement();
 
            // Prepare a statement to update a record
            Product product = (Product) value;
            String sql = "UPDATE Products SET ProductName = '" + product.ProductName + "' WHERE             ProductName = '" + key + "'";
 
            // Execute the insert statement
            int count= command.executeUpdate(sql);
            // count contains the number of updated rows
            if(count > 0)
                return true;
        }
        catch (SQLException sqlException)
        {
            throw sqlException;
        }
        return false;
    }
 
  • HashMap 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 HashMap updateSource(String[] keys, Object[] vals) throws Exception {
        HashMap mapException = new HashMap();
 
            for(int index=0; index < keys.length; index++)
            {
                try
                {
                    Statement command = connection.createStatement();
 
                    // Prepare a statement to update a record
                    Product product = (Product) vals[index];
                    String sql = "UPDATE Products SET ProductName = '" + product.ProductName + "'                     WHERE ProductName = '" + keys[index] + "'";
 
                    command.executeUpdate(sql);
                }
                catch (SQLException sqlException)
                {
                    mapException.put(keys[index], sqlException);
                }
            }
        return mapException;
    }
 
  • 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 boolean removeFromSource(String key) throws Exception {
        try
        {
            Statement command = connection.createStatement();
 
            String sql = "DELETE FROM Products WHERE ProductName = '" + key + "'";
 
            // Execute the insert statement
            int count= command.executeUpdate(sql);
            // count contains the number of updated rows
            if(count > 0)
                return true;
        }
        catch (SQLException sqlException)
        {
            throw sqlException;
        }
        return false;
    }
 
  • HashMap 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 HashMap removeFromSource(String[] keys) throws Exception {
        HashMap mapException = new HashMap();
 
            for(int index=0; index < keys.length; index++)
            {
                try
                {
                    Statement command = connection.createStatement();
 
                    String sql = "DELETE FROM Products WHERE ProductName = '" + keys[index] +"'";
 
                    command.executeUpdate(sql);
                }
                catch (SQLException sqlException)
                {
                    mapException.put(keys[index], sqlException);
                }
            }
        return mapException;
    }
 
 
  • bool clear();
 
This method is responsible for clearing datasource. Cache will not be cleared if it fails to clear the data source.
 
public boolean clear() throws Exception {
        try
        {
            Statement command = connection.createStatement();
 
            String sql = "DELETE * From Products";
 
            // Execute the insert statement
            int count= command.executeUpdate(sql);
            // count contains the number of updated rows
            if(count > 0)
                return true;
 
        }
        catch (SQLException sqlException)
        {
            throw sqlException;
        }
        return false;
    }
 
Using Write-Thru Cache 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, then 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.