NCache 4.6 - Online Documentation

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.
 
      To utilize the API, include the following namespace in your application: Alachisoft.NCache.Web.Caching. 
 
  • 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.
 
 
See Also