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.

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.

publicvoid 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,

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.

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;