Using Streams in NCache
Note
This feature is only available in NCache Enterprise edition.
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. You need 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.
Warning
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 that high loading time can annoy your viewer. For resolving this issue, streams are used to write and read high object files in chunks like format. You can add and retrieve them in stream chunks because those small chunks will take less time as compared to whole object.
Warning
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 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.
Important
You should properly close an opened stream as other threads may be waiting for that stream's availability and until you close it properly, the lock is not released. Other threads that want to acquire locks will receive an exception.