Streams Processing: Retrieve Data
In Streams Processing, 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. To read data with streams, the streaming mode should be set to read mode.
In Streams Processing, the Read mode can be acquired either with a lock or without a 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 including Streams Processing, 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
The GetCacheStream
with Read mode throws StreamNotFoundException
if the specified key does not exist in the cache.
// Precondition: 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;
}
}
Additional Resources
NCache provides sample application for Streaming on GitHub.
See Also
.NET: Alachisoft.NCache.Client namespace.
Java: com.alachisoft.ncache.client namespace.