tree: ca734399a468ae78a58019a15e9bc3784c7fb813 [search]
  1. customers/
  2. incoming/
  3. test/
  4. application.properties
  5. cache.camel.yaml
  6. metadata.json
  7. README.md
route/cache/README.md

Cache

Each order needs its customer, and the customer service is slow. The Cache EIP keeps the answer per customer for a while, so the shop calls the service once for many orders of the same customer. A customer that changes is removed from the cache, and a failed lookup is never cached.

What you will see

INFO ... cache.camel.yaml:77 : Calling the customer service for C-482
INFO ... cache.camel.yaml:87 : Order ORD-1001 for Anna Jensen (gold), customer from the service
INFO ... cache.camel.yaml:77 : Calling the customer service for C-207
INFO ... cache.camel.yaml:87 : Order ORD-1002 for Jonas Weber (silver), customer from the service
INFO ... cache.camel.yaml:87 : Order ORD-1004 for Anna Jensen (gold), customer from the cache
INFO ... cache.camel.yaml:87 : Order ORD-1005 for Jonas Weber (silver), customer from the cache
INFO ... cache.camel.yaml:112 : Customer C-482 changed, removed from the cache
INFO ... cache.camel.yaml:77 : Calling the customer service for C-482
INFO ... cache.camel.yaml:87 : Order ORD-1006 for Anna Jensen (gold), customer from the service
INFO ... cache.camel.yaml:77 : Calling the customer service for C-999
INFO ... cache.camel.yaml:93 : Order ORD-1007: customer C-999 not found (HTTP 404)

Install Camel CLI

Install JBang and the Camel CLI as described in the root README; camel --version confirms the install. The Cache EIP is new in Camel 4.23.

Run it

camel run *

This runs the shop's routes and the customer service in one app, on port 8080. Stop the example with ctrl + c.

How it works

  • incoming/ holds six orders and, after the fourth, a notice from the back office that customer C-482 changed. The files are read one a second in name order. customers/ holds the data of the customer service. C-999 has no file.
  • The customer-service route plays the slow customer service: GET /customers/{id} takes a second and answers with the customer's file, or 404.
  • The order route wraps the call in a cache block, with the customer id as the key and a time to live of ten seconds (customer.cache.ttl). On a miss, the steps inside run and their result is stored. On a hit, the cached customer becomes the body, and the steps inside are skipped. The variable source shows which one it was.
  • convertBodyTo: String at the end of the block stores the customer as text, not as the stream that the HTTP call returns.
  • The cache's store is a named bean, customerCache, a MemoryKeyValueRepository. The customer-changed route calls its delete method with the customer id, so the next order of C-482 gets fresh data.
  • The call for C-999 fails with 404 inside the cache block, so nothing is stored for it. The doTry around the block catches the failure and logs it, and the shop goes on with the next order.

Build it step by step

Ask your assistant, or type it yourself, one step at a time, and run after each:

  1. A platform-http route for GET /customers/{id} that answers with customers/<id>.json after a second, and a file route on incoming that calls it for each order and logs the order with the customer's name and tier.
  2. The customer service is slow. Keep its answer per customer id for ten seconds, so it is called only once for many orders of the same customer. Log whether the customer came from the service or from the cache.
  3. A file with customerChanged instead of an order says that a customer changed. Remove that customer from the cache, so the next order gets fresh data.
  4. The service answers 404 for a customer it does not know. Log “customer ... not found” and go on with the next order.

Try changing

  • Set customer.cache.ttl=1s: the cached customers expire before the next order, and every order calls the service.
  • Remove keyValueRepository: customerCache: the cache still uses the bean, because it is the only KeyValueRepository in the registry. With two such beans, name the one each cache block uses.
  • Move the doTry inside the cache block: the failure for C-999 is then caught inside, the block succeeds, and its result is cached.

Integration testing

The example comes with a test in the Citrus YAML DSL, test/cache.citrus.it.yaml, which the Camel CLI runs:

camel test run test/cache.citrus.it.yaml

The test runs the routes and verifies the hits, the removed customer and the unknown customer.

Help and contributions

If you hit any problem using Camel or have some feedback, then please let us know.

We also love contributors, so get involved :-)

The Camel riders!