NCache 4.6 - Online Documentation

Sample Usage of Read-Through With Basic Provider

 
This section will explain the use of Read-Through provider after configuring and deploying it. NCache supports multiple Read-Through providers with an application.
 
NCache provides Alachisoft.NCache.Web.Caching.DSReadOption enum to specify Read thru option in APIs.
 
Multiple Read-Through providers can be configured through NCache. Default Read-Through provider will be called if specific provider name is not mentioned through API. You can also use providers other than default by using provider specific overloads of APIs.
 
      To utilize Read-Through Caching, include the following namespaces in your application:
  • Alachisoft.NCache.Runtime.DatasourceProviders
  • Alachisoft.NCache.Web.Caching
  • Alachisoft.NCache.Runtime
 
  • Get Method
 
Member
Description
Get(string key, DSReadOption dsReadOption)
Gets item from cache and uses default provider
Get(string key, string providerName, DSReadOption dsReadOption)
Gets item from cache and uses specified provider
 
            Product product = null;
            try
            {
                string key = "Product:1001";
 
                object data = cache.Get(key, DSReadOption.ReadThru);
 
                if (data != null)
                {
                    product = (Product)data;
                }
            }
            catch (OperationFailedException exp)
            {
                //handle exception
            }
 
 
  • Bulk Method
 
For better understanding of the these operations review  Bulk Operations. In this scenario, use the following API
 
Member
Description
IDictionary GetBulk(string[] keys, DSReadOption dsReadOption)
Gets bulk items from cache and uses default provider
IDictionary GetBulk(string[] keys, string providerName, DSReadOption dsReadOption)
Gets bulk items from cache and uses specified provider
 
            try
            {
                String[] keys = { "Product:1001", "Product:1002", "Product:1003", "Product:1004" };
 
                IDictionary resultSet = cache.GetBulk(keys, DSReadOption.ReadThru);
                //IDictionary contains cached keys and values
            }
            catch (OperationFailedException exp)
            {
                //handle exception
            }
 
 
  • Using Read-Through Provider with CacheItem
 
For this scenario, use the following API.
 
Member
Description
CacheItem GetCacheItem(string key DSReadOption dsReadOption)
Gets CacheItem from cache and uses default provider
GetCacheItem(string key, string providerName, DSReadOption dsReadOption)
Gets CacheItem from cache and uses specified provider
 
 
            string key = "Product:1001";
            Product product = null;
 
            try
            {
                CacheItem data = cache.GetCacheItem(key, DSReadOption.ReadThru);
 
                if (data != null)
                {
                    product = (Product)data.Value;
                }
            }
            catch (Exception exp)
            {
                //handle exception
            }
 
Please note that you can specify default provider through NCache Manager or through client.ncconf placed in config folder of NCache installation directory. If provider name is not provided in both API and client.ncconf, default provider will automatically be used.
 
<cache id="mycache" default-readthru-provider="defaultProviderName" client-cache-id="" client-cache-syncmode="optimistic" default-writethru-provider="" load-balance="True" server-runtime-context="NCACHE">
  ...
</cache>
 
 
CacheInitParam can also be used to specify providers.
NCache logs the warnings in Application event log in case of an exception during loading the assemblies.
 
See Also