Alachisoft NCache 4.1 - Online Documentation

Streaming API

 
NCache API now allows you to read and write binary data stream in the cache. NCache has implemented a CacheStream which is derived from the standard Stream and provides major functionality of streaming in NCache. However, it does not support 'seeking' which is the querying and modifying of the current position within a stream.
 
Streaming can be used with the following namespace:
 
using Alachisoft.NCache.Web.Caching;
using System.IO;
 
Stream Modes:
Stream works in two modes:
 
CacheStream Reader:
 
Read mode can either be acquired 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.
 
  • Stream Read: (With Lock): Multiple read operations can be performed on a stream, no write operation is allowed in this mode. CacheStream Reader class will throw an exception if stream is already in use by other application.
     
    CacheStream stream = _cache.GetCacheStream(key, StreamMode.Read);
    stream.Read(readBuffer, 0, readBuffer.Length);
    stream.Close();
     
  • Stream Read: (Without Lock): Multiple read and write operations can be performed on a stream.
     
    stream = _cache.GetCacheStream(key, StreamMode.ReadWithoutLock);
    stream.Read(readBuffer, 0, readBuffer.Length);
    stream.Close();
 
CacheStream Writer:
 
In write mode only single write operation can be performed on a stream. No read operation is allowed in this mode. CacheStream Writer class will throw an exception if stream is already in use by other application.
 
stream = _cache.GetCacheStream(key, StreamMode.Write);
stream.Write(writeBuffer, 0, writeBuffer.Length);
stream.Close();
 
Stream Close:
 
After using the stream in any mode, users are supposed to close the stream in order to release the lock.
 
Buffered Stream:
 
Cache stream directly reads and writes data from the cache. It directly calls cache for both operations which thereby increases the calls to the cache and reduces the performance of an application. To overcome the above issue NCache introduces the Buffer stream. Buffer stream is the set of bytes used for storing the data up to the certain limit before reading or writing it to the cache. Until the buffer reaches its maximum limit the data will not be read or write from/to the cache. Buffer stream reads or writes a chunk of data to the cache in a single call which reduces the number of calls to the cache. This chunk will be equal to the buffer size. Buffer size varies according to the requirement of application. Following code shows how to GetBufferedStream:
 
 
Stream wstream = _cache.GetCacheStream(key, StreamMode.Write).GetBufferedStream(1000);
wstream.Write(writeBuffer, 0, 200);
wstream.Write(writeBuffer, 200, 600);
wstream.Write(writeBuffer, 600, 1000);
wstream.Close();
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.