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

Close Cache Stream to Release Lock

Note

This feature is only available in NCache Enterprise Edition.

The stream should be closed after being used in any mode in order to release the lock. This section explains the closing of a stream in NCache.

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>
  • Include the following namespaces in your application:
    • import com.alachisoft.ncache.runtime.exceptions.*;
    • import com.alachisoft.ncache.client.*;
  • 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, 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
    cacheStream.Close();
}
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 {
    // 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);

    // 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
}

Reading or writing a stream that was previously in use throws exception i.e. StreamAlreadyLockedException, if it was not closed properly.

Additional Resources

NCache provides sample application for streaming on GitHub.

See Also

Continuous Query
Read/Write Streams
Add and Update Data with Streams
Retrieving Data from Streams
Security and Encryption

Back to top Copyright © 2017 Alachisoft