Contact Free trial Login

Anypoint Exchange

Home to the complete listing of connectors, templates, examples and APIs.
Discover and use proven assets built by the MuleSoft ecosystem, or add assets to a private
tenant of Anypoint Exchange for collaboration and sharing of internal best practices.

Cache Scope with Fibonacci

Cache Scope with Fibonacci

(0 reviews)

Understand benefits of cache scope using a Fibonacci function in Mule applications.

Screenshot of the main flow in Anypoint Studio, view full image here


The example that follows demonstrates the power of the cache scope with a Fibonacci function. When implementing this example you will understand how to deploy the cache scope within a Mule application. This will also give you some insight on caching strategies and how sub flows work within Mule.

Example Use Case

This example solves the Fibonacci function F(n) for the n which is given as input by the user. Intermediate results are cached and later can be retrieved by the subsequent requests. The Finobacci sequence is a series of numbers in which the next number in the series is always the sum of the two numbers preceding it.

In this example, the Mule flow receives and performs two tasks for each request:

executes, and returns the answer to, the Fibonacci equation (see below) using a number (n) provided by the caller
F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1
records and returns the cost of the calculation, wherein each individual invocation of a calculation task (i.e. add two numbers in the sequence) adds 1 to the cost

Set Up and Run this Example

  1. Open this Mule Example in Mule Studio

  2. Once the application is running, go to your localhost server and type in the following request:


    You should get a response similar to what is shown below:

     Fibonacci(10) = 55
     COST: 11
  3. Now, change the value of n to 5


    You should get a response similar to what is shown below:

     Fibonacci(5) = 5
     COST: 0

Note that the cost for calculating f(n) is 0. This is because f(5) has already been calculated and cached in the cache scope block while calculating f(10). If you try calculating f(10) again, the cost would be 0 as f(10) has already been calculated and cached.


Read full documentation in GitHub


Log into Anypoint Platform to download this asset


Created by
MuleSoft Organization
Published onJun 22, 2017


VersionRuntime version