Read-through/Write-through and Write-behind for Cache
The main purpose of caching is to cache data for achieving maximum performance. While working with cache, client applications may need to perform operations on the data source. For this purpose, NCache supports transparent read/write operations on the data source through Read-through and Write-through/Write-behind caching. Using this feature, client applications can read or write data to the data source through the cache, if needed.
If your provider code is .NET-based, it will be executed in the same process where your cache is running. But, for Java-based provider, the NCache Execution Service will be responsible for code execution.
Read-through and Write-through caching facilitates the user in the following cases:
- The user needs to fetch data from the data source in case it is not present in the cache store (Read-through).
- The user needs to update data in the data source synchronously after updating the cache store (Write-through).
- The user needs to update data in the data source asynchronously after updating the cache store (Write-behind).
Using this feature in NCache has the following advantages:
- NCache provides pluggable architecture for Read-through and Write-through caching, so the client application code does not need to contain data source logic.
- The data source may change at any time, so this pluggable architecture can configure different data sources while keeping the application code intact.
- Your application only needs to communicate with NCache and NCache will take care of any backend communication with the data source.
To use Read-through or Write-through caching, the IReadThruProvider
or the IWriteThruProvider
interface needs to be implemented. Runtime parameters can also be provided to the Read-through/Write-through provider while configuring it through the
NCache Management Center or Command Line Tools. These parameters will be passed to the Read-through/Write-through provider on its initialization. These initialization parameters can be used in many ways, e.g., the connection string of a data source can be specified as a parameter eliminating the need to hardcode it in the provider code.
In NCache, the Read-through/Write-through providers are configured by name, and provider-specific API can be used by using the provider name. In case of multiple providers, one of the providers will be configured by default. This default provider will be called if a provider name is not mentioned explicitly through the API. Providers other than the default can be used by utilizing provider-specific overloads of the given API.
In This Section
Read-through Caching
Explains how to read data from the data source in NCache.
Write-through Caching
Explains how to write data to the data source in NCache.