Caffeine Cache
Since Camel 2.20
Only producer is supported
The Caffeine Cache component enables you to perform caching operations using the simple cache from Caffeine.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-caffeine</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> caffeine-cache://cacheName[?options]
You can append query options to the URI in the following format: ?option=value&option=#beanRef&….
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Caffeine Cache component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
The action to perform when the message has no CamelCaffeineAction header. When neither sets an action, the exchange fails. Enum values:
| String | ||
Whether the component creates the cache when the registry has no cache bean with the cache name. The component keeps the caches it creates by name, so the endpoints with the same cache name share one cache, which the options of the first of these endpoints to start configure. The cache options (evictionType, maximumSize and the others) only apply to a cache that the component creates. When false and the registry has no such bean, the endpoint fails to start. | true | boolean | |
Which options configure the eviction of the cache: SIZE_BASED uses initialCapacity and maximumSize, and TIME_BASED uses expireAfterAccessTime and expireAfterWriteTime. The options of the other type are ignored. Enum values:
| SIZE_BASED | EvictionType | |
The time in seconds after the last read or write of an entry, or its creation, after which Caffeine removes the entry from the cache (Caffeine expireAfterAccess). With 0, entries expire right away. Only used when evictionType is TIME_BASED, together with expireAfterWriteTime: an entry expires at the first of the two times. | 300 | int | |
The time in seconds after the creation of an entry, or the last replacement of its value, after which Caffeine removes the entry from the cache (Caffeine expireAfterWrite). With 0, entries expire right away. Only used when evictionType is TIME_BASED, together with expireAfterAccessTime: an entry expires at the first of the two times. | 300 | int | |
The minimum total size of the internal data structures of the cache (Caffeine initialCapacity). A large enough estimate avoids resizing later, but a too large value wastes memory. Only used when evictionType is SIZE_BASED; when not set, Caffeine uses its default. | Integer | ||
The key of the entry for the GET, PUT and INVALIDATE actions when the message has no CamelCaffeineKey header. When neither sets a key, these actions fail. | String | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The maximum number of entries the cache may contain (Caffeine maximumSize). As the size grows close to the maximum, Caffeine evicts the entries that are less likely to be used again, and it may evict an entry before the limit is exceeded or exceed it for a short time while evicting. With 0, Caffeine evicts each entry right after it is stored. Only used when evictionType is SIZE_BASED; when not set, the size of the cache has no limit. | Integer | ||
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
The Caffeine CacheLoader that loads the value of a key that is not in the cache, for the GET and GET_ALL actions. Only used by caffeine-loadcache, which needs it to create the cache. | CacheLoader | ||
The shared configuration that each endpoint of this component starts from; the options of the endpoint override it. | CaffeineConfiguration | ||
A Caffeine RemovalListener that the cache notifies each time an entry is removed, whether it was invalidated, replaced, evicted or expired. Caffeine calls it asynchronously after the removal, and only logs the exceptions it throws. | RemovalListener | ||
A Caffeine StatsCounter that records the statistics of the cache instead of the default counter of Caffeine. Only used when statsEnabled is true. | StatsCounter | ||
Whether the cache records statistics (Caffeine recordStats), such as the hit and miss counts that Cache.stats() returns. Recording them costs some performance on each cache operation. | false | boolean | |
The fully qualified class name of the type that the PUT action converts the message body to before it stores it as the value. When not set, the body is stored as it is. Not used when the CamelCaffeineValue header gives the value. | String |
Endpoint Options
The Caffeine Cache endpoint is configured using URI syntax:
caffeine-cache:cacheName
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The action to perform when the message has no CamelCaffeineAction header. When neither sets an action, the exchange fails. Enum values:
| String | ||
Whether the component creates the cache when the registry has no cache bean with the cache name. The component keeps the caches it creates by name, so the endpoints with the same cache name share one cache, which the options of the first of these endpoints to start configure. The cache options (evictionType, maximumSize and the others) only apply to a cache that the component creates. When false and the registry has no such bean, the endpoint fails to start. | true | boolean | |
Which options configure the eviction of the cache: SIZE_BASED uses initialCapacity and maximumSize, and TIME_BASED uses expireAfterAccessTime and expireAfterWriteTime. The options of the other type are ignored. Enum values:
| SIZE_BASED | EvictionType | |
The time in seconds after the last read or write of an entry, or its creation, after which Caffeine removes the entry from the cache (Caffeine expireAfterAccess). With 0, entries expire right away. Only used when evictionType is TIME_BASED, together with expireAfterWriteTime: an entry expires at the first of the two times. | 300 | int | |
The time in seconds after the creation of an entry, or the last replacement of its value, after which Caffeine removes the entry from the cache (Caffeine expireAfterWrite). With 0, entries expire right away. Only used when evictionType is TIME_BASED, together with expireAfterAccessTime: an entry expires at the first of the two times. | 300 | int | |
The minimum total size of the internal data structures of the cache (Caffeine initialCapacity). A large enough estimate avoids resizing later, but a too large value wastes memory. Only used when evictionType is SIZE_BASED; when not set, Caffeine uses its default. | Integer | ||
The key of the entry for the GET, PUT and INVALIDATE actions when the message has no CamelCaffeineKey header. When neither sets a key, these actions fail. | String | ||
The maximum number of entries the cache may contain (Caffeine maximumSize). As the size grows close to the maximum, Caffeine evicts the entries that are less likely to be used again, and it may evict an entry before the limit is exceeded or exceed it for a short time while evicting. With 0, Caffeine evicts each entry right after it is stored. Only used when evictionType is SIZE_BASED; when not set, the size of the cache has no limit. | Integer | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The Caffeine CacheLoader that loads the value of a key that is not in the cache, for the GET and GET_ALL actions. Only used by caffeine-loadcache, which needs it to create the cache. | CacheLoader | ||
A Caffeine RemovalListener that the cache notifies each time an entry is removed, whether it was invalidated, replaced, evicted or expired. Caffeine calls it asynchronously after the removal, and only logs the exceptions it throws. | RemovalListener | ||
A Caffeine StatsCounter that records the statistics of the cache instead of the default counter of Caffeine. Only used when statsEnabled is true. | StatsCounter | ||
Whether the cache records statistics (Caffeine recordStats), such as the hit and miss counts that Cache.stats() returns. Recording them costs some performance on each cache operation. | false | boolean | |
The fully qualified class name of the type that the PUT action converts the message body to before it stores it as the value. When not set, the body is stored as it is. Not used when the CamelCaffeineValue header gives the value. | String |
Message Headers
The Caffeine Cache component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelCaffeineAction (producer) Constant: | The action to perform, which takes precedence over the action option. Possible values: CLEANUP PUT PUT_ALL GET GET_ALL INVALIDATE INVALIDATE_ALL AS_MAP. | String | |
CamelCaffeineActionHasResult (producer) Constant: | Whether the action has put a result in the message body: true for GET when the key has a value, and for GET_ALL and AS_MAP; false for the other actions. | Boolean | |
CamelCaffeineActionSucceeded (producer) Constant: | Set to true when the action has completed. A failing action fails the exchange instead. | Boolean | |
| Constant: | The key of the entry for GET, PUT and INVALIDATE, which takes precedence over the key option. The key is converted to a String. | ||
| Constant: | The Set of the keys to get for GET_ALL, which gets no entries without it, and of the keys to invalidate for INVALIDATE_ALL, which invalidates all the entries without it. AS_MAP sets it to the keys of the cache. | Set | |
| Constant: | The value to store for PUT, or a Map of the entries to store for PUT_ALL. When not set, the message body is used. | ||
CamelCaffeineOldValue (producer) Constant: | Not set by any action of this component. |
Message Body
The message body at each side of the Caffeine Cache component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The value to store for PUT, converted to the valueType when set, and a Map of the entries for PUT_ALL, unless the CamelCaffeineValue header gives it; without either they fail. The other actions do not use the body. |
Producer output |
| By the action, which the CamelCaffeineAction header can set: the value of the key for GET, or the body as it was when the key is not in the cache, a Map of the entries found for GET_ALL, and the cache as a Map for AS_MAP. The other actions keep the body.
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Examples
You can use your cache with the following code:
-
Java
-
XML
-
YAML
from("direct://start")
.to("caffeine-cache://cache?action=PUT&key=1")
.to("caffeine-cache://cache?key=1&action=GET")
.log("Test! ${body}")
.to("mock:result"); <route>
<from uri="direct://start"/>
<to uri="caffeine-cache://cache?action=PUT&key=1"/>
<to uri="caffeine-cache://cache?key=1&action=GET"/>
<log message="Test! ${body}"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct://start
steps:
- to:
uri: caffeine-cache://cache
parameters:
action: PUT
key: 1
- to:
uri: caffeine-cache://cache
parameters:
key: 1
action: GET
- log:
message: "Test! ${body}"
- to:
uri: mock:result In this way, you’ll work always on the same cache in the registry.