Alachisoft NCache 4.1 - Online Documentation

Implementing Cache Startup Loader Interface

 
Implementing CacheLoader
 
Following is a list of methods needs to be defined by the class implementing interface and a brief description of their purpose.
 
  • void init(HashMap parameters);
 
This method performs tasks like allocating resources, acquiring connections etc. When the cache is initialized and Cache Startup Loader 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. 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.
 
  • void dispose();
 
This method performs tasks like releasing resources etc. When Cache is stopped or loadNext returns false, it calls the dispose() method to notify the client that the task is completed or cache is stopped now and you can free resources related to your datasource.
 
  • public bool loadNext(LinkedHashMap data, LoaderState state);
 
This method should contain logic to load object(s) from the master datasource. After init call, framework calls loadNext method in a loop till loadNext returns false. This method receives a LinkedHashMap that contains respective data that needs to be populated. Another variable is LoaderState which takes reference of an already stored object. This function allows user to add data in chunks, or add it as a single operation. If user wants to add data in chunks, he must return "true" back to caller framework, indicating that this method should be called again. User should return "false", to end Startup loading of data by the framework.
 
 
Using CacheLoader:
 
Here is how your class should look like for CacheLoader:
 
public class CacheLoaderImpl implements CacheLoader
{
    private Connection connection;
    private String connectionString = "jdbc:sqlserver://localhost:1433;databaseName=Northwind;user=xxxx;password=xxxx";
 
    public void init(HashMap parameters) throws Exception {
        connection = DriverManager.getConnection(connectionString);
    }
 
    public boolean loadNext(LinkedHashMap data, LoaderState state) throws Exception {
 
            int nextIndex = 0;
            if(state.index != null)
            {
                nextIndex = (Integer)state.index;
            }
 
            Statement command = connection.createStatement();
            String query = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE ProductID > '" + nextIndex + "' AND ProductID < '" + (nextIndex + 10) + "'";
            ResultSet reader = command.executeQuery(query);
            boolean gotSomeRows = false;
            while (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());
 
                data.put("Product:" + product.ProductID, provideritem);
                gotSomeRows = true;
            }
 
            if(gotSomeRows == false)
                return true;
 
            state.index = nextIndex + 10;
            reader.close();
        return false;
    }
 
    public void dispose() throws Exception {
        connection.close();
    }
}
 
 
NCache logs the warnings in Application event log in case of an exception during loading the libraries.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.