Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.此内容没有您所选择的语言版本。
Chapter 17. Cache
17.1. Cache Component 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Available as of Camel 2.1 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The cache component enables you to perform caching operations using EHCache as the Cache Implementation. The cache itself is created on demand or if a cache of that name already exists then it is simply utilized with its original settings.
This component supports producer and event based consumer endpoints.
The Cache consumer is an event based consumer and can be used to listen and respond to specific cache activities. If you need to perform selections from a pre-existing cache, use the processors defined for the cache component.
Maven users will need to add the following dependency to their
pom.xml
for this component:
URI format 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
cache://cacheName[?options]
cache://cacheName[?options]
You can append query options to the URI in the following format,
?option=value&option=#beanRef&...
Options 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The Cache component supports the following options:
Name | Default Value | Description |
---|---|---|
maxElementsInMemory
|
1000
|
The number of elements that may be stored in the defined cache |
memoryStoreEvictionPolicy
|
MemoryStoreEvictionPolicy.LFU
|
The number of elements that may be stored in the defined cache. Options include
|
overflowToDisk
|
true
|
Specifies whether cache may overflow to disk |
eternal
|
false
|
Sets whether elements are eternal. If eternal, timeouts are ignored and the element never expires.
|
timeToLiveSeconds
|
300
|
The maximum time between creation time and when an element expires. Is used only if the element is not eternal
|
timeToIdleSeconds
|
300
|
The maximum amount of time between accesses before an element expires |
diskPersistent
|
false
|
Whether the disk store persists between restarts of the Virtual Machine. |
diskExpiryThreadIntervalSeconds
|
120
|
The number of seconds between runs of the disk expiry thread. |
cacheManagerFactory
|
null
|
Camel 2.8: If you want to use a custom factory which instantiates and creates the EHCache net.sf.ehcache.CacheManager . Type: abstract org.apache.camel.component.cache.CacheManagerFactory
|
eventListenerRegistry
|
null
|
Camel 2.8: Sets a list of EHCache net.sf.ehcache.event.CacheEventListener for all new caches\- no need to define it per cache in EHCache xml config anymore. Type: org.apache.camel.component.cache.CacheEventListenerRegistry
|
cacheLoaderRegistry
|
null
|
Camel 2.8: Sets a list of org.apache.camel.component.cache.CacheLoaderWrapper that extends EHCache net.sf.ehcache.loader.CacheLoader for all new caches\- no need to define it per cache in EHCache xml config anymore. Type: org.apache.camel.component.cache.CacheLoaderRegistry
|
key
|
null
|
Camel 2.10: To configure using a cache key by default. If a key is provided in the message header, then the key from the header takes precedence. |
operation
|
null
|
Camel 2.10: To configure using an cache operation by default. If an operation in the message header, then the operation from the header takes precedence. |
objectCache
|
false
|
Camel 2.10: Whether to turn on allowing to store non serializable objects in the cache. If this option is enabled then overflow to disk cannot be enabled as well. |
configurationFile
|
|
Camel 2.13/2.12.3: To configure the location of the
ehcache.xml file to use, such as classpath:com/foo/mycache.xml to load from classpath. If no configuration is given, then the default settings from EHCache is used.
|
configuration
|
|
To use a custom
org.apache.camel.component.cache.CacheConfiguration configuration.
|
Cache component options 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Name
|
Default Value
|
Description
|
---|---|---|
configuration
|
|
To use a custom
org.apache.camel.component.cache.CacheConfiguration configuration.
|
cacheManagerFactory
|
|
To use a custom
org.apache.camel.component.cache.CacheManagerFactory .
|
configurationFile
|
|
Camel 2.13/2.12.3: To configure the location of the
ehcache.xml file to use, such as classpath:com/foo/mycache.xml to load from classpath. If no configuration is given, then the default settings from EHCache is used.
|
Message Headers Camel 2.8+ 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Header | Description |
---|---|
CamelCacheOperation
|
The operation to be performed on the cache. The valid options are
|
CamelCacheKey
|
The cache key used to store the Message in the cache. The cache key is optional if the CamelCacheOperation is CamelCacheDeleteAll
|
Header changes in Camel 2.8
The header names and supported values have changed to be prefixed with
CamelCache
and use mixed case. This makes them easier to identify and keep separate from other headers. The CacheConstants
variable names remain unchanged, just their values have been changed. Also, these headers are now removed from the exchange after the cache operation is performed.
The
CamelCacheAdd
and CamelCacheUpdate
operations support additional headers:
Header | Type | Description |
---|---|---|
CamelCacheTimeToLive
|
Integer
|
Camel 2.11: Time to live in seconds. |
CamelCacheTimeToIdle
|
Integer
|
Camel 2.11: Time to idle in seconds. |
CamelCacheEternal
|
Boolean
|
Camel 2.11: Whether the content is eternal. |
Cache Producer 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Sending data to the cache involves the ability to direct payloads in exchanges to be stored in a pre-existing or created-on-demand cache. The mechanics of doing this involve
- setting the Message Exchange Headers shown above.
- ensuring that the Message Exchange Body contains the message directed to the cache
Cache Consumer 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Receiving data from the cache involves the ability of the CacheConsumer to listen on a pre-existing or created-on-demand Cache using an event Listener and receive automatic notifications when any cache activity take place (i.e CamelCacheGet/CamelCacheUpdate/CamelCacheDelete/CamelCacheDeleteAll). Upon such an activity taking place
- an exchange containing Message Exchange Headers and a Message Exchange Body containing the just added/updated payload is placed and sent.
- in case of a CamelCacheDeleteAll operation, the Message Exchange Header CamelCacheKey and the Message Exchange Body are not populated.
Cache Processors 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
There are a set of nice processors with the ability to perform cache lookups and selectively replace payload content at the
- body
- token
- xpath level
Example 1: Configuring the cache 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Example 2: Adding keys to the cache 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Example 2: Updating existing keys in a cache 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Example 3: Deleting existing keys in a cache 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Example 4: Deleting all existing keys in a cache 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Example 7: Getting an entry from the Cache 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Example 8: Checking for an entry in the Cache 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Note: The CHECK command tests existence of an entry in the cache but doesn't place a message in the body.
Management of EHCache 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Here's a snippet on how to expose them via JMX in a Spring application context:
Of course you can do the same thing in straight Java:
ManagementService.registerMBeans(CacheManager.getInstance(), mbeanServer, true, true, true, true);
ManagementService.registerMBeans(CacheManager.getInstance(), mbeanServer, true, true, true, true);
You can get cache hits, misses, in-memory hits, disk hits, size stats this way. You can also change CacheConfiguration parameters on the fly.
Cache replication Camel 2.8+ 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
The Camel Cache component is able to distribute a cache across server nodes using several different replication mechanisms including: RMI, JGroups, JMS and Cache Server.
There are two different ways to make it work:
- You can configure
ehcache.xml
manually, or - You can configure these three options:
cacheManagerFactory
eventListenerRegistry
cacheLoaderRegistry
Configuring Camel Cache replication using the first option is a bit of hard work as you have to configure all caches separately. So in a situation when the all names of caches are not known, using
ehcache.xml
is not a good idea.
The second option is much better when you want to use many different caches as you do not need to define options per cache. This is because replication options are set per
CacheManager
and per CacheEndpoint
. Also it is the only way when cache names are not know at the development phase.
Note
It might be useful to read the EHCache manual to get a better understanding of the Camel Cache replication mechanism.
Example: JMS cache replication 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
JMS replication is the most powerful and secured replication method. Used together with Camel Cache replication makes it also rather simple. An example is available on a separate page.