通读缓存提供程序配置和实现
要使用直读缓存,您需要实现 IReadThruProvider
.NET 的接口和 ReadThruProvider
Java的接口。 NCache 将使用此自定义实现从数据源读取数据。 请参阅 配置 管理员指南中的重新配置读取 以获得更多细节。
警告
如果在中指定了 Read-Through 缓存提供程序 客户端.ncconf ,在 default-readthru-provider
标记并通过 API,然后 API 中的提供者将覆盖中指定的提供者 客户端.ncconf.
重要
对于 Java,在添加 Maven 依赖项之前,您需要确保:
- JDK 17 安装。
- Java 的环境变量已设置。
先决条件
- 了解与所有人员合作所需的标准先决条件 NCache 服务器端功能,包括通读缓存,请参阅给定页面 服务器端 API 先决条件.
- 这应该是一个使用 Microsoft Visual Studio 的类库项目。
- 确保 部署 ReadThrough 提供程序 使用 NCache 管理中心。
- 有关 API 详细信息,请参阅: IReadThruProvider, 从源加载, 从源加载数据类型.
备注
In NCache,读取缓存提供程序在缓存级别上配置,即,对于集群缓存,所有节点都将包含提供程序配置及其部署的程序集。
ReadThrough 缓存提供程序的示例实现
以下代码示例提供了一个示例实现,用于使用以下命令配置 Read-Through 缓存提供程序: IReadThruProvider
.NET 类和 ReadThruProvider
Java 类。 这 LoadFromSource
和 LoadDataTypeFromSource
方法包含逻辑,如果在缓存中找不到对象,则分别从配置的数据源加载对象或数据类型。 另外, GetConnectionString
返回已配置数据源的连接字符串。
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
}
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
更多资讯
NCache 提供了一个用于 Read-Through 的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache。运行 命名空间。
Java的: COM。alachisoft.ncache.runtime.datasourceprovider 命名空间。