Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 12 Next »

Function: cache()

Build and use a set of values across the records in a recordset.

The first parameter is the cache key expression. This is evaluated and the result used as the key value for the cache. Before the subsequent expressions are evaluated the key value is used to retrieve the current value from the cache associated with that key. This retrieved value is then assigned to the internal variable _cacheValue. If there was no value in the cache for this key then _cacheValue is set to _NULL.

This cache key expression is followed by a number of additional expressions. These are evaluated in turn, and the value returned by the final expression is placed into the cache against the original key value.

The final cache value is returned by the expression.

Please note that the internal variable _cacheValue should not be used outside of the cache function.

When you use this function, PhixFlow log messages include the cache size.


Synchronisation

The cache function "synchronises" itself across each call. In PhixFlow, to make best use of modern computer architectures with parallel processing capabilities, when you run analysis the work to be done is divided up into parcels. These may be processed in any order. However, to make sure that the results from the cache function remain consistent, PhixFlow ensures that calls to the cache function are "synchronised" across all these parcels of work.

It is only necessary to be concerned about synchronisation if the same key will be used across multiple parcels of work. However, this consideration can become complex, so as a general rule, try and use a single call to the cache function in the table configuration.

Example

You have a calculate table processing, say, 10,000 records - and you want to accumulate totals based on a key value, of which there are 100 across the data set. For each key value, many records will be added to the total. To do this, you should use a call to the cache function such as:

...
cache ( _out.KeyVal,
    if (_cacheValue == _NULL,
		$currentValue = 0
	, // else
		$currentValue = _cacheValue
	),
	$currentValue = $currentValue + _out.CashValue,
	$currentValue
)
...

and not like:

...
$currentValue = cache(_out.KeyVal),
if ($currentValue == _NULL,
	cache(_out.KeyVal, _out.CashValue)
, // else
	cache(_out.KeyVal, $currentValue + _out.CashValue)
)
...

Syntax

cache(cache key, expression1, expression2, [,..., expressionN])

ArgumentTypeDescription
cache keyExpressionAny valid Expression
expressionNExpressionAny valid Expression

Examples

$result = cache("accountRef", _cacheValue + 1)

Get the cache value for key value "accountRef", add one to this and write this back to the cache. Finally return the new cache value associated with this key and assign it to the variable $result.

cache("accoutRef")

Return the cache value for key value "accountRef".

$result = cache("accountRef", _cacheValue + 1)

Example- generating UIDs

This example is for the case where a user wants to generate UIDs in a specific way.

In this example the stream receives updates for records in the system. So it can receive both records that exist in the system already, and those that are new. This stream is used to correctly set/lookup the various ID fields.

Now if 3 records came into this stream that had the same value for a specific attribute (e.g. another ID field such as "BarcodeNumber"), then we do not want to generate a new ID for each record... we want to generate it once and then use that value for the other records.

do(
	if($newRecord,
	//then
		do(
            $recordID =
			cache(_out.BarcodeNumber,
				if( _cacheValue == _NULL,
					$recordID = nextValue('RecordID'),
					_cacheValue
                )
            )
                
		),
	//else
		$recordID = in.RecordID
    ),
    $recordID
)


This expression above uses the cache function to hold a newly generated ID (nextValue('RecordID') in memory for each unique _out.BarcodeNumber field. So the 1st record that is processed gets the new ID generated by the sequence, then any subsequent records that are processed will find a match for their BarcodeNumber in the cache, and take that ID that was generated for the 1st BarcodeNumber.


See Also

  • No labels