Using Write-Through with Basic Operations
This section will explain the use of Write-Through Provider after configuring and deploying it. NCache supports multiple Write-Through Providers with applications.
To utilize the
IWriteThruProvider
, include the following namespace in your application:
Alachisoft.NCache.Web.Caching
Alachisoft.NCache.Runtime.DatasourceProviders
Alachisoft.NCache.Runtime
NCache provides Alachisoft.NCache.Web.Caching.DSWriteOption
enum to specify
Write thru/Write behind options in APIs.
Multiple Write-Through Providers can be configured through NCache. Default provider will be called if a specific provider name is not mentioned through API. You can also use providers other than default by using provider specific overloads of APIs.
Adding with Write-Through
Member | Description |
---|---|
CacheItemVersion Add(string key, CacheItem item, DSWriteOption dsWriteOption, DataSourceItemsAddedCallback onDataSourceItemAdded) |
Adds item in cache using the default provider |
CacheItemVersion Add(string key, CacheItem item, DSWriteOption dsWriteOption, string providerName, DataSourceItemsAddedCallback onDataSourceItemAdded) |
Adds item in cache using the specified provider |
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
CacheItem cacheItem = new CacheItem(product);
string key = "Product:" + product.ProductID ;
try{
CacheItemVersion itemVersion = cache.Add(key, cacheItem, DSWriteOption.WriteThru, null);
}
catch (OperationFailedException exp){
// handle exception
}
Updating with Write-Through
Member | Description |
---|---|
CacheItemVersion Insert(string key, CacheItem item, DSWriteOption dsWriteOption, DataSourceItemsAddedCallback onDataSourceItemAdded) |
Inserts item in cache using the default provider |
CacheItemVersion Insert(string key, CacheItem item, DSWriteOption dsWriteOption, string providerName, DataSourceItemsUpdatedCallback onDataSourceItemUpdatedCallback) |
Inserts item in cache using the specified provider |
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
CacheItem cacheItem = new CacheItem(product);
string key = "Product:" + product.ProductID;
CacheItemVersion updatedItemVersion;
try{
updatedItemVersion = cache.Insert(key, cacheItem, DSWriteOption.WriteThru, "XmlWriteThruProvider", null);
if (updatedItemVersion.Version > 1)
{
//Item updated in cache. Perform further tasks.
}
}
catch (OperationFailedException exp){
//handle exception
}
Deleting an Existing Item with Write-Through
Member | Description |
---|---|
Delete(string key, DSWriteOption dsWriteOption, DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback) |
Deletes item from cache using the default provider |
Delete(string key, DSWriteOption dsWriteOption, string providerName, DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback); |
Deletes item from cache using the specified provider |
string key = "Product:1001";
try{
cache.Delete(key, DSWriteOption.WriteThru, null);
}
catch (OperationFailedException exp){
// handle exception
}
Removing an Existing Item with Write-Through
Member | Description |
---|---|
object Remove(string key, DSWriteOption dsWriteOption, DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback) |
Removes item from cache using the default provider |
object Remove(string key, DSWriteOption dsWriteOption, string providerName, DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback); |
Removes item from cache using the specified provider |
string key = "Product:1001";
Product product = null;
try{
Object returnObject = cache.Remove(key, DSWriteOption.WriteThru, null);
//Verify the removed item from both cache and data source
if (returnObject != null)
{
product = (Product)returnObject;
}
}
catch (OperationFailedException exp){
// handle exception
}
See Also
Configuring WriteThru Provider
Using Write-Behind with Basic Operations