/// <summary> /// This methods inserts data in the cache using cache stream. /// </summary> /// <param name="key"> The key against which stream will be written. </param> /// <param name="writeBuffer"> data that will be written in the stream. </param> private static void WriteUsingStream(string key, byte[] writeBuffer) { // Declaring NCacheStream CacheStream stream = _cache.GetCacheStream(key, StreamMode.Write); stream.Write(writeBuffer, 0, writeBuffer.Length); stream.Close(); Console.WriteLine("Stream written to cache."); }
/// <summary> /// This method fetches data from the cache using streams. /// </summary> /// <param name="key"> The key of the stream that needs to be fetched from the cache. </param> private static void ReadUsingStream(string key) { byte[] readBuffer = new byte[1024]; CacheStreamAttributes cacheStreamAttributes = new CacheStreamAttributes(StreamMode.Read); // StramMode.Read allows only simultaneous reads but no writes! CacheStream stream = _cache.GetCacheStream(key, cacheStreamAttributes); // Now you have stream perform operations on it just like any regular stream. var readCount = stream.Read(readBuffer, 0, readBuffer.Length); stream.Close(); Console.WriteLine("Bytes read = " + readCount); }