Retrieve Cache Data from Streams
Note
This feature is only available in NCache Enterprise Edition.
The GetCacheStream API with overload is used to open a stream. It returns the handle of the opened stream as an instance of CacheStream class. In order to read data with streams, the streaming mode should be set to read mode.
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.
Prerequisites
- To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
- For API details, refer to: ICache, CacheStream, StreamMode, CacheStreamAttributes, GetCacheStream, Read.
Read from Stream
This mode is used for acquiring read-only access to the stream. With the help of the example given below, a stream can be opened with read-only mode for the provided key.
Warning
GetCacheStream
with Read mode throws StreamNotFoundException if the specified key does not exist in the cache.
try
{
// Pre-condition: Cache is already connected
// Generate a unique cache key
// Generate a unique cache key
string key = "StreamKey";
// Set StreamMode object to Read mode to read and write data with lock
// StreamMode.Read allows only simultaneous reads
// StreamMode.ReadWithoutLock allows both read and write
StreamMode streamMode = StreamMode.ReadWithoutLock;
// Provide the streamMode object to CacheStreamAttributes object
var streamAttributes = new CacheStreamAttributes(streamMode);
// Use GetCacheStream to initialize CacheStream object
using (var cacheStream = cache.GetCacheStream(key, streamAttributes))
{
// Specify buffer to store data read from stream
byte[] buffer = new byte[4096];
while (true)
{
// Read data from stream
int bytesRead = cacheStream.Read(buffer, 0, buffer.Length);
// You can also write the incomming video data from the ongoing live event
// Use the buffer data to stream the video live
// Stop if all data is read from stream
if (bytesRead == 0)
break;
}
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// handle exception
// This includes StreamException
}
Additional Resources
NCache provides sample application for streaming on GitHub.