• 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.

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.

Prerequisites

  • .NET/.NET Core
  • Java
  • Install the following NuGet package in your application:
    • Enterprise: Alachisoft.NCache.SDK
  • 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.
  • For API details, refer to: ICache, CacheStream, StreamMode, CacheStreamAttributes.
  • To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
  • To handle any unseen exceptions, refer to the Troubleshooting section.
  • Add the following Maven dependencies in your pom.xml file:
<dependency>
    <groupId>com.alachisoft.ncache</groupId>
    <artifactId>ncache-client</artifactId>
    <version>x.x.x</version>
</dependency>
  • Import the following packages in your application:
    • import com.alachisoft.ncache.client.*;
    • import com.alachisoft.ncache.runtime.exceptions.*;
  • Cache must be running.
  • The application must be connected to cache before performing the operation.
  • Make sure that the data being added is serializable.
  • For API details, refer to: Cache, CacheStream, StreamMode, CacheStreamAttributes.
  • To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
  • To handle any unseen exceptions, refer to the Troubleshooting section.
  • .NET/.NET Core
  • Java
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
}
try 
{
    // Precondition: 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
    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 = (int) cacheStream.length();

    // Specify buffer size
    byte[] dataToRead = new byte[dataSize];

    // Read data from buffer
    cacheStream.read(dataToRead, 0, dataSize);

    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

Continuous Query
Read/Write Streams
Add and Update Data with Streams
Closing a Stream
Security and Encryption

Back to top Copyright © 2017 Alachisoft