Stream Processing in Cache
Storing large amounts of data (media files or large PDF documents) directly 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. It eventually results in higher memory consumption at the client end. NCache provides Stream Processing through NCache API, which enables you to read or write large binary data into the cache in the form of smaller chunks. Moreover, it allows you to simultaneously open multiple parallel streams into the cache.
Streams Processing: When is it Useful
Suppose you have a high-traffic application that contains large media files that can be viewed by the users, while you keep frequently viewed media files in your cache for fast access. Adding and fetching a large media file in the cache can be costly if you add or fetch it at once, even asynchronously. Meanwhile, such applications require high performance and real-time response. In this regard, Stream Processing can effectively reduce time and memory costs by allowing you to write and read large objects in the form of chunks.
Warning
In NCache, serialization/de-serialization, compression/decompression, or encryption/decryption are not applied to the Stream Processing.
Stream Buffers
Streams are directly fetched from or inserted into specified storage (here in cache) in general. You can also add a buffer between storage and stream. Then the stream is added to that buffer first. Once the buffer is full, it is written to the cache or loaded where needed. For using a buffer in streaming, you only have to specify the size of the buffer. Buffering can provide a significant performance boost by reducing the response time.
Note
The default size of a stream buffer is 4K.
Stream Modes
NCache provides different streaming modes that enable read/write access while retrieving/updating data using streams in the cache. Any of these modes must be specified based on your desired operation.
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. This mode can be specified when you want to ensure data consistency in the presence of multiple applications.
Stream Read without Lock
In ReadWithoutLock
mode, write operations can be performed in parallel to read
operations because read operations do not acquire any lock to stop writing. You can read data by specifying ReadWithoutLock
mode when there is a need to process the data concurrently.
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.
Note
NCache Stream does not support the Seek
operation.
Important
You should properly close an opened stream to release the lock as other threads may be waiting for that stream to be available. If a stream is not closed, any other threads trying to acquire locks will receive an exception.
In This Section
Add/Update Data with Streams
Explains how to add data to a stream or update the added data.
Retrieve Data from Streams
Explains how you retrieve data from a stream.