NCache 4.4 - Online Documentation

Using Streams

 
Opening with Stream Modes
 
GetCacheStream API with different overloads is used to open a stream. GetCacheStream API opens the stream of specified key with provided streaming mode.  It  returns the handle of the opened stream as an object of CacheStream class.
 
CacheStream is derived from System.IO.Stream. It is designed to read/write BLOB using standard Stream interface. However, it does not support 'seeking' which is the querying and modifying the current position within a stream.
 
  • Reading from Stream
 
This mode is used for acquiring read-only access to the stream. With the help of example given below, a stream can be opened with read-only property on provided key. 
 
string key = "key:ncache-manual";
CacheStream cacheStream = null;
try
{
    cacheStream = cache.GetCacheStream(key, StreamMode.Read);
}
catch (StreamException ex)
{
    // handle exception
}
 
Now data can be read from this CacheStream object. GetCacheStream with Read mode throws StreamException if no data exists in the cache with the specified key.
 
  • Reading from Stream without Lock
 
This mode is useful when write and read operations are to be performed in parallel. The following example explains how to get stream object with read-without-lock property.      
 
string key = "key:ncache-manual";
CacheStream cacheStream = null;
try
{
    cacheStream = cache.GetCacheStream(key, StreamMode.ReadWithoutLock);
}
catch (StreamException ex)
{
    // handle exception
}
 
Data can be read from this CacheStream object. GetCacheStream with ReadWithoutLock mode throws StreamException if no data exists in the cache with the specified key.
 
  • Writing to a Stream
 
In write mode only single write operation can be performed on a stream. It will throw an exception if stream is already in use by another application.
 
string key = "key:ncache-manual";
CacheStream cacheStream = null;
try
{
    cacheStream = cache.GetCacheStream(key, StreamMode.Write);
}
catch (OperationFailedException ex)
{
    // handle exception
}
 
Now data can be written to this CacheStream object.
 
Using GetBufferedStream
 
Buffered stream is the set of bytes used for storing the data up to the certain limit before reading or writing it to the cache. The following code shows how to useĀ GetBufferedStream:
 
string key = "key:ncache-manual";
byte[] writeBuffer = System.IO.File.ReadAllBytes("C:\\ncache-manual.pdf");
int length = writeBuffer.Length;
try
{
    System.IO.Stream wstream = _cache.GetCacheStream(key, StreamMode.Write).GetBufferedStream(length);
    wstream.Write(writeBuffer, 0, length/2);//...No data is written in cache as buffer is not full
    wstream.Write(writeBuffer, length/2, length/2);//...Data is written in cache because buffer is full now
    ////...Close the stream
}
catch (StreamException ex)
{
    // handle exception }
26.2.3.     Adding or Updating Data with Streams
string key="key:ncache-manual";
byte[] dataToWrite = System.IO.File.ReadAllBytes("C:\\ncache-manual.pdf");
try
{
    cacheStream = cache.GetCacheStream(key, StreamMode.Write);
    cacheStream.Write(dataToWrite, 0, dataToWrite.Length);
//...Close stream
 
}
catch (StreamException ex)
{
// handle exception
}
 
The same method is used for adding and updating the data with stream. If the key provided with GetCacheStream API does not exist in the cache, the data written on the stream is added in cache. If an item already exists in cache with the provided key, binary data written on the stream appends with the existing data against the specified key.
 
Retrieving Data from Streams
 
To read data from streams, open stream in any read mode as explained above, and simply read data from it.
 
string key = "key:ncache-manual";
try
{
CacheStream cacheStream = cache.GetCacheStream(key, StreamMode.Read);
int dataSize = Convert.ToInt32(cacheStream.Length);
byte[] dataToRead = newbyte[dataSize];
    cacheStream.Read(dataToRead, 0, dataSize);
//...Close stream
}
catch (StreamException e)
{
// handle exception
}
 
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.
 
Closing a Stream
 
The stream should be closed after being used in any mode in order to release the lock.
 
string key = "key:ncache-manual";
try
{
CacheStream cacheStream = cache.GetCacheStream(key, StreamMode.Read);
int dataSize = Convert.ToInt32(cacheStream.Length);
byte[] dataToRead = newbyte[dataSize];
cacheStream.Read(dataToRead, 0, dataSize);
// your logic here
cacheStream.Close();
}
catch (StreamException e)
{
// handle exception
}
 
Reading or writing a stream that was previously in use throws exception i.e. StreamAlreadyLockedExceptionif it was not closed properly.
 
 
Close the stream after all operations are done to the stream.
 
 
 
 
See Also