getCache()

Returns the row specified by a given index from the current bucket in the event cache.

Syntax

getCache (cacheName, index )

Parameters

cacheName The name of the event cache.
index

Row index in the event cache as an integer.

Usage

Returns the row specified by a given index from the current bucket in the event cache. This index is 0 based. The function takes the name of the event cache and an integer as its arguments, and returns a row from the event cache. Specifying an invalid index parameter generates a bad record.

Example

This example obtains the top 3 distinct prices per trading symbol. In order to accomplish this task, the example makes use of the getCache(), cacheSize() and deleteCache() functions.

CREATE SCHEMA TradesSchema (
    Id integer, 
    TradeTime date,
    Venue string,  
    Symbol string, 
    Price float, 
    Shares integer
)
;

CREATE INPUT WINDOW QTrades SCHEMA 
TradesSchema PRIMARY KEY (Id)
;
CREATE FLEX flexOp
    IN QTrades
    OUT OUTPUT WINDOW QTradesStats SCHEMA TradesSchema PRIMARY KEY(Symbol,Price)
    BEGIN
       DECLARE
          typedef [integer Id;| date TradeTime; string Venue; 
             string Symbol; float Price; 
             integer Shares] QTradesRecType;
          eventCache(QTrades[Symbol], manual, Price asc) tradesCache;
          typeof(QTrades) insertIntoCache( typeof(QTrades) qTrades )
          {
             integer counter := 0;
             typeof (QTrades) rec;
             long cacheSz := cacheSize(tradesCache);
             while (counter < cacheSz) {
                rec := getCache( tradesCache, counter );
                if( round(rec.Price,2) = round(qTrades.Price,2) ) {
                   deleteCache(tradesCache, counter);
                   insertCache( tradesCache, qTrades );
                   return rec;
                   break;
                } else if( qTrades.Price < rec.Price) {
                   break;
                }
                counter++;
            }
            if(cacheSz < 3) {       
               insertCache(tradesCache, qTrades);
               return qTrades;
            } else {
               rec := getCache(tradesCache, 0);
               deleteCache(tradesCache, 0);
               insertCache(tradesCache, qTrades);
               return rec;
            } 
            return null;
         }
      END;

      ON QTrades {
         keyCache( tradesCache, [Symbol=QTrades.Symbol;|] );
         typeof(QTrades) rec := insertIntoCache( QTrades );  
         if(rec.Id) {
            if(rec.Id <> QTrades.Id) {
               output setOpcode(rec, delete);
            } 
            output setOpcode(QTrades, upsert);
         }
     };
    END;