Storing large binary data for, e.g., media files, large PDF documents, into the cache using cache API is inefficient. This will require loading the entire file into the application memory and then putting it into the cache as a single object. To overcome this, NCache provides Streaming API for reading/writing large binary data into the cache. Multiple parallel streams can be opened simultaneously into the cache.
When is it Useful
Suppose there is a high traffic application which has large media files that can be viewed by the user. For fast access, highly viewed media files are kept in cache. Adding and fetching large media files even in cache can take high processing and time if they are added and fetched at once, even if it is done asynchronously. Applications like these require high performance and the slow loading time can annoy the user. For resolving this issue, streams are used to write and read high object files in chunks like format. The files can be added and retrieved in stream chunks because those small chunks will take less time as compare to the whole large object.
|
In NCache serialization/deserialization, compression/decompression or encryption/decryption is not applied on data that is added in stream to avoid the chances of data corruption.
|
Stream Buffers
Streams are directly fetched/inserted from specified storage (here in cache) in general. However, a buffer can be added in between storage and stream. The stream will be added to that buffer and when that buffer is full, it writes the stream in cache or load it where needed. For using buffer in streaming, just the size of buffer needs to be specified. This is basically used to prevent delays in loading the data, e.g., to load a media file quickly.
|
NCache Stream does not support Seek operation.
|
Stream Modes
A cache stream can be opened in any of these modes:
-
Read
-
Read without Lock
-
Write
Stream Read
In read mode, multiple read operations can be performed simultaneously on a stream but no write operation is allowed.
Stream Read without Lock
In read without lock mode, write operations can be done in parallel to read operations because read operation does 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 thread having write lock cannot read that stream
|
Properly closing a stream after reading and writing is very important. The reason being that more threads might be waiting for that stream’s availability and until it is closed properly, the lock is not released which will give exception to other threads wanting to acquire the stream.
|
See Also