Caching

This tutorial provides an example of using the Composite block cache.

Note

This tutorial requires the workflow from Workflow as a Block.

Before You Begin

This tutorial requires an existing prjTutorials project. If you have not created this project yet, see Tutorial Project first.

  • Open the prjTutorials project.
  • Open wfToySolverBlock (ToySolverBlock.p7wf).
  • Select File ‣ Save Workflow As... to create a copy of the workflow. Save the workflow as wfTestCache.
  • Verify that you are editing wfTestCache and continue with the tutorial.

Task

The task in this tutorial is to avoid redundant calculations in Model when it receives an input that has already been processed before. To test the solution, inputs are specified manually in Run. In general case, such inputs can be received by Model when it works in a cycle. For example, consider the workflow from the Sampling Loop tutorial: if the input sample contains duplicate rows, each duplicate is processed despite there is no need to calculate outputs more than once. This is due to the fact that Model does not automatically remember its input and output data; however, it can be configured to cache input and output values and use the cached output value when the input value is found in cache.

Solution

The solution in this tutorial is based on the fact that Model is a Composite block; this is the only block that supports caching. Caching allows to save input and output values either to the block memory (which is cleared when the workflow finishes) or to a persistent file on disk. Current solution uses the file cache because there will be several test runs explaining how the cache works.

Cache Configuration

To set up caching, open the Model configuration.

../_images/page_tutorials_cache_01_set.png
  • On the Configuration tab, select Enable Cache.
  • Input a name for the cache file — for example, testcache. Note that if you enable caching but leave the name empty, the block will automatically use a memory cache.
  • Click b_ok to save settings and close the configuration dialog.

For testing, uplink Model.x and Model.f ports to add workflow input and output in Run. The test is to see how the workflow behaves when you run it with the same input or with different inputs, as described further.

Note

Since the workflow root is also a Composite block, in fact you can use the original Toy Solver integration workflow (created in the Conditions and Error Handling tutorial) and set up caching in the root block configuration. Current tutorial uses a workflow imported to a block for illustrative purposes only.

Workflow

The workflow should now contain only the Model block and two uplinks.

../_images/page_tutorials_cache_02_wf.png

Model is configured to use a file cache; the testcache file will be found in the project directory after the first run.

Results

To test caching, first run the workflow with some input values — for example, (0.4, 0.2). When it finishes, you can see the testcache file appeared in the project directory.

Try running the workflow again without changing the input and note the block run statistics in the finish message.

../_images/page_tutorials_cache_03_fin1.png

Note that the blocks inside Model did not start. When Model received the input values, it did a cache lookup and found the input-output pair saved on the first run. After this the result was read from the cache and output to Model.f without recalculation.

Now try changing inputs and run the workflow once again, also taking a look at the block run statistics.

../_images/page_tutorials_cache_04_fin2.png

This time the input was not found in cache and was processed as usual — you can see that blocks inside Model started to calculate output.

Conclusion

Caching allows to avoid unnecessary recalculation and to re-use the data from previous workflow runs. Note also that caching allows to share data between workflows using the same block — for example, the cache file created in this tutorial can be used in any other tutorial workflow including the Model block.