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.
Pre-Requisites
- 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.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
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
}
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 at:
Shipped with NCache: %NCHOME%\samples\dotnet\Streaming
See Also
Continuous Query
Read/Write Streams
Add and Update Data with Streams
Retrieving Data from Streams
Security and Encryption