Caching

Flow ships out-of-the box with a full production-grade Hazelcast cache.

Cache requests

By default, Flow will cache requests throughout a query only. In other words, Flow will only call a service with the same set of parameters once within a query. However, caches are destroyed at the end of each query.

To enable caching, simply add @Cache to the top of your query:

@Cache
find { Film[] } as {
  id: FilmId
  // Fetched from a remote service.
  // Calls will be cached.
  currentReviewScore: ReviewScore
}

Scope caches

By default, all queries share the same cache. If you want cache isolation so that population and invalidation are isolated from each other, you can apply a name to the cache:

@Cache("myCache")
find { Film[] } as {
  id: FilmId
  // Fetched from a remote service.
  // Calls will be cached.
  currentReviewScore: ReviewScore
}

Use Flow’s local cache

Flow’s local cache runs in-process within Flow.

It’s the default cache that’s enabled if no connection is provided.

// This query uses the default, local cache
@Cache
find { Film[] } as {
  id: FilmId
  // Fetched from a remote service
  currentReviewScore: ReviewScore
}

Cache TTL

Flow’s internal cache contains a TTL of 5 minutes.

Use an external cache

Flow supports external caches, and will run a near-cache in-process, offloading to the remote cache as the library supports.

To enable a remote cache, first define a connection to the cache in your connections.conf file, then specify the name of the connection in your query.

hazelcast {
   myHazelcast {
      connectionName = myHazelcast
      addresses = ["localhost:5701"]
   }
}
@Cache(connection = "myHazelcast")
find { Film[] } as {
  id: FilmId
  // Fetched from a remote service
  currentReviewScore: ReviewScore
}

Cache TTL

The default Cache TTL is defined in the connections file:

hazelcast {
  myHazelcast {
    connectionName = myHazelcast
    addresses = ["localhost:5701"]
    ttl = 20 minutes
  }
}

Max-age headers

Flow will honour max-age headers when caching responses from HTTP services:

http response cache control

Setting the cache-control: max-age= header will cause the entry to be automatically evicted after the max-age expires.