Try Playground
Show / Hide Table of Contents

Streams Processing: Retrieve Data [Deprecated]

In Streams Processing, the GetCacheStream API with overload is used to open a stream. It returns the handle of the opened stream as an instance of CacheStream class. To read data with streams, the streaming mode should be set to read mode.

In Streams Processing, the Read mode can be acquired either with a lock or without a lock. In read with lock mode, multiple read operations can be performed simultaneously on a stream, but no write operation is allowed in this mode. While in read without lock, write operations can be done parallel to read operations.

Prerequisites

  • .NET
  • Java
  • Legacy API
  • To learn about the standard prerequisites required to work with all NCache client-side features including Streams Processing, please refer to the given page on Client-Side API Prerequisites.
  • For API details, refer to: ICache, CacheStream, StreamMode, CacheStreamAttributes, GetCacheStream, Read.
  • To learn about the standard prerequisites required to work with all NCache client-side features including Streams Processing, please refer to the given page on Client-Side API Prerequisites.
  • For API details, refer to: Cache, CacheStream, StreamMode, CacheStreamAttributes, getCacheStream, canRead, read, closed.
  • Create a new Console Application.
  • Make sure that the data being added is serializable.
  • Add NCache References by locating %NCHOME%\NCache\bin\assembly\4.0 and adding Alachisoft.NCache.Web and Alachisoft.NCache.Runtime as appropriate.
  • Include the Alachisoft.NCache.Web.Caching namespace in your application.

Read from Stream

This mode is used for acquiring read-only access to the stream. With the help of the example given below, a stream can be opened with read-only mode for the provided key.

Warning

The GetCacheStream with Read mode throws StreamNotFoundException if the specified key does not exist in the cache.

  • .NET
  • Java
  • Legacy API
// Precondition: Cache is already connected
// Generate a unique cache key

// Generate a unique cache key
string key = "StreamKey";

// Set StreamMode object to Read mode to read and write data with lock
// StreamMode.Read allows only simultaneous reads
// StreamMode.ReadWithoutLock allows both read and write
StreamMode streamMode = StreamMode.ReadWithoutLock;

// Provide the streamMode object to CacheStreamAttributes object
var streamAttributes = new CacheStreamAttributes(streamMode);

// Use GetCacheStream to initialize CacheStream object
using (var cacheStream = cache.GetCacheStream(key, streamAttributes))
{
    // Specify buffer to store data read from stream
    byte[] buffer = new byte[4096];

    while (true)
    {
        // Read data from stream
        int bytesRead = cacheStream.Read(buffer, 0, buffer.Length);

        // You can also write the incomming video data from the ongoing live event
        // Use the buffer data to stream the video live

        // Stop if all data is read from stream
        if (bytesRead == 0)
            break;
    }
}
// Precondition: Cache is already connected
// Generate a unique cache key
int readByteCount = 0;
byte[] readBuffer = new byte[5000];

// Initialize CacheStream object
CacheStream cacheStream = null;

// StreamMode.Read allows only simultaneous reads but no writes!
// StreamMode.ReadWithoutLock allows simultaneous reads and also let the stream be writeable!
CacheStreamAttributes cacheStreamAttributes = new CacheStreamAttributes(StreamMode.Read);

using (CacheStream cacheStream = cache.getCacheStream(key, cacheStreamAttributes))
{
    //int bytesRead = -1;
    while (cacheStream.CanRead)
    {
        var bytesRead = cacheStream.read(readBuffer, 0, readBuffer.length);
        readByteCount += bytesRead;

        // ReadBuffer is available for further processing/manipulation
        // Valid readBuffer contents (readBuffer[0] to readBuffer[bytesRead])
    }
}

Console.Writeline(key + " file content read as CacheStream. Total bytes: " + readByteCount);

// Close stream
cacheStream.Close();
string key = "key:ncache-guide";
CacheStream cacheStream = null;

cacheStream = cache.GetCacheStream(key, StreamMode.ReadWithoutLock);

Additional Resources

NCache provides sample application for Streaming on GitHub.

See Also

.NET: Alachisoft.NCache.Client namespace.
Java: com.alachisoft.ncache.client namespace.

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top