NCache 4.6 - Online Documentation


Storing large binary data for e.g. media files, large PDF documents, into the cache using cache API is inefficient. You will have to first load the entire file into the application memory and then put it into the cache as a single object. This requires more memory for the client application end. To overcome this, NCache provides Streaming API for reading/writing large binary data into the cache.  You can open multiple parallel streams into the cache.
NCache Stream does not support Seek operation, which is the querying and modifying of the current position within a stream.
When is it Useful
Suppose you have a high traffic application which contains large media files that can be viewed by your users and you keep highly viewed media files in your cache for fast access. Adding and fetching large media files even in cache can take high processing and time if you add or fetch it at once, even asynchronously. Applications like these require high performance and the excessive loading time can annoy your viewer. For resolving this issue, streams are used to write and read heavy object files in a chunks-like format. You can add and retrieve them in stream chunks because those small chunks will take less time as compared to the whole object.
In NCache, serialization/de-serialization, compression/decompression or encryption/decryption is not applied to the streams.
Stream Buffers
Streams are directly fetched/inserted from specified storage (here in cache) in general. You can also add a buffer in between storage and stream. Your stream will be added to that buffer and when that buffer will be full, it writes your stream in cache or loads it where needed. For using a buffer in streaming, you just have to specify size of the buffer. This is basically used when you want to load your data without delays of loading like when media files are loaded.
Stream Modes
NCache allows following streaming modes:
  • Stream Read
In Read mode, read operations can be performed simultaneously on a stream by multiple clients but no write operation is allowed in this mode. Stream is opened with ReaderLock on it.
  • Stream Read without Lock
In Read without Lock mode, write operations can be done in parallel to read operations because read operations do not acquire any lock to stop writing.
  • Stream Write
In Write mode only a single write operation can be performed on a stream. No read operation is allowed in this mode. Even threads having write locks cannot read that stream.
If you opened a stream for reading or writing then you should properly close that stream too. Because more threads may be waiting for that stream's availability and until you close it properly, lock is not released. Other threads that want to acquire locks will receive exception.
See Also