• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

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

  • .NET/.NET Core
  • Java
  • 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.
  • 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: Cache, CacheStream, StreamMode, CacheStreamAttributes, getCacheStream, canRead, read, closed.

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.

  • .NET/.NET Core
  • Java
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
}
try
{  
    // Pre-condition: Cache is already connected
    // Generate a unique cache key
    int readByteCount = 0;
    byte[] readBuffer = new byte[5000];

    // Initialize CacheStream object
    CacheStream cacheStream = null;

    // StreamMode.Read allows only simultaneous reads but no writes!
    // StreamMode.ReadWithoutLock allows simultaneous reads and also let the stream be writeable!
    CacheStreamAttributes cacheStreamAttributes = new CacheStreamAttributes(StreamMode.Read);

    using (CacheStream cacheStream = cache.getCacheStream(key, cacheStreamAttributes))
    {
        //int bytesRead = -1;
        while (cacheStream.CanRead)
        {
            var bytesRead = cacheStream.read(readBuffer, 0, readBuffer.length);
          readByteCount += bytesRead;

            // ReadBuffer is available for further processing/manipulation
            // Valid readBuffer contents (readBuffer[0] to readBuffer[bytesRead])
        }
    }

    Console.Writeline(key + " file content read as CacheStream. Total bytes: " + readByteCount);

    // Close stream
    cacheStream.Close();

}

catch (OperationFailedException exception)
{
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
}
catch (Exception exception)
{
    // Generic exception like IllegalArgumentException or NullPointerException
}

Additional Resources

NCache provides sample application for streaming on GitHub.

See Also

Add or Update Data with Streams
Basic Operations

Back to top Copyright © 2017 Alachisoft