Retrieve Cache Data from Streams
Note
This feature is only available in NCache Enterprise Edition.
To read data from streams, open stream in any read mode, and simply read data from it. Read mode can be acquired either with lock or without lock. In read with lock mode, multiple read operations can be performed simultaneously on a stream but no write operation is allowed in this mode. While in read without lock, write operations can be done parallel to read operations.
Pre-Requisites
- Include the following namespaces in your application:
Alachisoft.NCache.Runtime.Caching
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Exceptions
- Cache must be running.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
try
{
// Pre-condition: Cache is already connected
// Generate a unique cache key
string key = "StreamKey";
// Initialize CacheStream object
CacheStream cacheStream = null;
// Set StreamMode object to Read mode
StreamMode streamMode = StreamMode.Read;
// Provide the streamMode object to cachestreamAttributes object
var streamAttributes = new CacheStreamAttributes(streamMode);
// Use GetCacheStream to set streamAttributes against the specified key
cacheStream = cache.GetCacheStream(key, streamAttributes);
// Specify cacheStream's length
int dataSize = Convert.ToInt32(cacheStream.Length);
// Specify buffer size
byte[] dataToRead = new byte[dataSize];
// Read data from buffer
cacheStream.Read(dataToRead, 0, dataSize);
//close stream
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (StreamException ex)
{
// handle exception
}
Additional Resources
NCache provides sample application for streaming at:
- GitHub
- Shipped with NCache: %NCHOME%\samples\dotnet\Streaming
See Also
Continuous Query
Read/Write Streams
Add and Update Data with Streams
Closing a Stream
Security and Encryption