Read-Through Provider Configuration and Implementation
Note
This feature is only available in NCache Enterprise Edition.
To use Read-Through caching, you need to implement the IReadThruProvider
interface for .NET and ReadThruProvider
interface for Java. NCache will use this custom implementation to read data from the data source. Please refer to Configuring Read-Through in Administrators' Guide for more details.
Warning
If a Read-Through provider is specified in client.ncconf in the default-readthru-provider
tag and also through API, then the provider in API will overwrite the one specified in client.ncconf.
Important
For Java, before adding Maven dependencies, you need to make sure that:
- JDK 11 is installed.
- Environment variable for Java is set.
Prerequisites
- To learn about the standard prerequisites required to work with all NCache server-side features please refer to the given page on Server Side API Prerequisites.
- This should be a class library project using Microsoft Visual Studio.
- Make sure to Deploy ReadThrough Provider using NCache Manager.
- For API details refer to: IReadThruProvider, LoadFromSource, LoadDataTypeFromSource.
Note
In NCache, Read-Through providers are configured on cache level, i.e. for clustered caches all nodes will contain provider configurations and their deployed assemblies.
Sample Implementation
The following code sample provides sample implementation to configure a Read-Through provider using the IReadThruProvider
class for .NET and ReadThruProvider
class for Java. The LoadFromSource
and LoadDataTypeFromSource
methods contain logic to load an object or data type respectively from the configured data source, if the object is not found in the cache. Also, the GetConnectionString
returns the connection string for the configured data source.
public class SampleReadThruProvider : IReadThruProvider
{
private SqlConnection _connection;
//Perform tasks like allocating resources or acquiring connections
public void Init(IDictionary parameters, string cacheId)
{
object server = parameters["server"];
object userId = parameters["username"];
object password = parameters["password"];
object database = parameters["database"];
string connString = GetConnectionString(server.ToString(), database.ToString(), userId.ToString(), password.ToString());
if (connString != "")
_connection = new SqlConnection(connString);
try
{
_connection.Open();
}
catch (Exception ex)
{
//handle exception
}
}
// Responsible for loading an item from the external data source
public ProviderCacheItem LoadFromSource(string key)
{
// LoadFromDataSource loads data from data source
object value = LoadFromDataSource(key);
var cacheItem = new ProviderCacheItem(value);
return cacheItem;
}
//Responsible for loading bulk of items from the external data source
public IDictionary<string, ProviderCacheItem> LoadFromSource(ICollection<string> keys)
{
var dictionary = new Dictionary<string, ProviderCacheItem>();
try
{
foreach (string key in keys)
{
// LoadFromDataSource loads data from data source
dictionary.Add(key, new ProviderCacheItem(LoadFromDataSource(key)));
}
return dictionary;
}
catch (Exception exp)
{
// Handle exception
}
return dictionary;
}
// Adds ProviderDataTypeItem with enumerable data type
public ProviderDataTypeItem<IEnumerable> LoadDataTypeFromSource(string key, DistributedDataType dataType)
{
IEnumerable value = null;
ProviderDataTypeItem<IEnumerable> dataTypeItem = null;
switch (dataType)
{
case DistributedDataType.List:
value = new List<object>()
{
LoadFromDataSource(key)
};
dataTypeItem = new ProviderDataTypeItem<IEnumerable>(value);
break;
case DistributedDataType.Dictionary:
value = new Dictionary<string, object>()
{
{ key , LoadFromDataSource(key) }
};
dataTypeItem = new ProviderDataTypeItem<IEnumerable>(value);
break;
case DistributedDataType.Counter:
dataTypeItem = new ProviderDataTypeItem<IEnumerable>(1000);
break;
}
return dataTypeItem;
}
//Perform tasks associated with freeing, releasing, or resetting resources.
public void Dispose()
{
if (_connection != null)
{
_connection.Close();
}
}
private object LoadFromDataSource(string key)
{
object retrievedObject = null;
// Load item from your data source and populate retrieved Object
return retrievedObject;
}
private string GetConnectionString(string server, string database, string userName, string password)
{
string connectionString = null;
try
{
if (!string.IsNullOrEmpty(server))
{
connectionString = "Server=" + server + ";";
}
else
{
//Server name is empty
}
if (!string.IsNullOrEmpty(database))
{
connectionString = connectionString + "Database=" + database + ";";
}
else
{
//Database is empty;
}
if (!string.IsNullOrEmpty(userName))
{
connectionString = connectionString + "User ID=" + userName + ";";
}
else
{
connectionString = connectionString + "User ID=" + "" + ";";
}
if (!string.IsNullOrEmpty(password))
{
connectionString = connectionString + "Password=" + password + ";";
}
else
{
connectionString = connectionString + "Password=" + "" + ";";
}
}
catch (Exception exp)
{
// Handle exception
}
return connectionString;
}
// Deploy this class on cache
}
Note
To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Additional Resources
NCache provides sample application for Read-Through on GitHub.
See Also
Using Read-Through with Cache Operations
Configuring Read-Through Provider
Write-Through Caching
Custom Cache Dependencies