Alachisoft NCache 4.1 - Online Documentation

Implementing ReadThruProvider

 
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 cache is initialized and finds that read-through caching is enabled, it calls start 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. 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 releasing the resources etc. When cache is stopped it calls the stop() method to notify the client that the cache is stopped now and you can free the resources related to your datasource.
 
public void stop() throws Exception
    {
            connection.close();
    }
 
  • void loadFromSource(String key, ProviderCacheItem cacheItem);
 
This method contains logic to load an object from the master datasource. Whenever a request for an item comes and item is not present in the cache, then cache calls loadFromSource method and passes the 'key' as an argument. It is the responsibility of a cache to return 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 the cache item if required. If ResyncItemOnExpiration is enabled and cache item expires, it passes the key of the expired item to reload fresh item 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, ProviderCacheItem provideritem) throws Exception
{
    Statement command = connection.createStatement();
    String query = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE ProductID = '" + key + "'";
    ResultSet reader = command.executeQuery(query);
 
    if (reader.next())
    {
        Product product = new Product();
        product.ProductID = reader.getInt("ProductID");
        product.ProductName = reader.getString("ProductName");
        product.UnitsAvailable = reader.getInt("UnitPrice");
 
        provideritem.setDependency(new KeyDependency("Product:1"));
 
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 30);
        provideritem.setAbsoluteExpiration(calendar.getTime());
 
        provideritem.setValue(product);
    }
    reader.close();
}
 
  • public HashMap <String, ProviderCacheItem> loadFromSource(String [] keys);
 
This method contains the logic to load array of objects from the master datasource. Whenever a request for items comes and items are not found in the cache, cache calls the loadFromSource method and passes array of keys as argument.
 
public HashMap<String, ProviderCacheItem> loadFromSource(String[] keys) throws Exception
    {  
            HashMap <String, ProviderCacheItem> source = new HashMap <String, ProviderCacheItem>();
            Statement command = connection.createStatement();
            String query;
            ResultSet reader;
            for ( int i = 0; i < keys.length; i++)
            {
                query = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE ProductID = '" + keys[i] + "'";
                reader = command.executeQuery(query);
 
                if (reader.next())
                {
                    Product product = new Product();
                    product.ProductID = reader.getInt("ProductID");
                    product.ProductName = reader.getString("ProductName");
                    product.UnitsAvailable = reader.getInt("UnitPrice");
 
                    ProviderCacheItem provideritem = new ProviderCacheItem(product);
 
                    provideritem.setDependency(new KeyDependency("Product:1"));
 
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.SECOND, 30);
                    provideritem.setAbsoluteExpiration(calendar.getTime());
                
                    source.put(keys[i], provideritem);
                }
                reader.close();
            }
            return source;
    }
 
Using Read-Thru Cache 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 a datasource. The code to access the cache should look like;
 
Cache mycache = NCache.initializeCache("myreplicatedcache");
object product = mycache.get("ALFKI", "XmlReadThruProvider", DSReadOption.ReadThru);
 
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.