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 a replacement of Java input/output streaming, CacheStream 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. Include the following import statements in your project for using stream:
 
import com.alachisoft.ncache.web.caching.*;
 
Open Stream:
 
To open a stream with respect to a specific Key in Cache, the GetStreamMethod is used where three different StreamModes can be defined.
 
Cache _cache = NCache.initializeCache("myCache");
CacheStream stream;
stream = _cache.GetCacheStream("Customer:David:1001", StreamMode.Read);
 
Once the stream is obtained, normal streaming operations can be applied, some of them explained below.
 
Close Stream:
 
After using the stream in any mode, users are expected to close the stream in order to release the lock.
 
Cache _cache = NCache.initializeCache("myCache");
CacheStream stream;
stream = _cache.GetCacheStream("Customer:David:1001", StreamMode.Read);
//Do some operation
stream.Close();
 
Read Stream:
 
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.
 
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.
 
Cache _cache = NCache.initializeCache("myCache");
CacheStream stream;
byte [] readBuffer = new byte [1024];
int readCount;
stream = _cache.GetCacheStream("Customer:David:1001", StreamMode.Read);
readCount = stream.Read(readBuffer, 0, readBuffer.length);
// Do operations ...
stream.Close();
 
Read Without Lock: Multiple read and write operations can be performed on the stream acquired.
 
Cache _cache = NCache.initializeCache("myCache");
CacheStream stream;
byte [] readBuffer = new byte [1024];
int readCount;
stream = _cache.GetCacheStream("Customer:David:1001", StreamMode.ReadWithoutLock);
readCount = stream.Read(readBuffer, 0, readBuffer.length);
// Do some operations ...
stream.Close();
 
Write Stream:
 
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.
 
Cache _cache = NCache.initializeCache("myCache");
CacheStream stream;
byte [] writeBuffer = new byte [1024];
stream = _cache.GetCacheStream("Customer:David:1001", StreamMode.Write);
stream.Write(writeBuffer, 0, writeBuffer.length);
stream.Close();
 
Get Stream Length
 
After Reading a stream, its current length can be obtained. Its return typed is 'long' and it simply returns how many bytes does it contain. Exception 'IllegalAccessException' is thrown if it is called after closing the stream.
 
Cache _cache = NCache.initializeCache("myCache");
CacheStream stream;
stream = _cache.GetCacheStream("Customer:David:1001", StreamMode.Read);
stream.Ready();
long length = stream.Length();
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.