In my previous post, I briefly introduced what is cache and how it works.
In this post, we will understand the importance of the best strategy to implement it in a Mule project and the validity of the data being cached. Bear in mind that a successful cache results in a high hit rate which means the data was present when fetched. A cache miss occurs when the data fetched was not present in the cache.
Applying Caching strategy in a Mule Project
This section of this blog is a sequel to my previous blog post about the advantages and disadvantages of applying caching in Mule4. If you have decided to deep dive here, I will demonstrate how to accomplish it inside a Mule4 project.
In this post, I will demonstrate how to accomplish caching inside the Mule application.
In my next post, I will demonstrate how to apply a HTTP Caching Policy from CloudHub.
Once you identify the flow or components where caching will be applied, follow the steps mentioned below.
- Wrap in all Message Processors. “Cache” component.
- Apply the configuration as expected for your use case. (Default Caching Strategy, Reference to a strategy)
- Reference to a strategy according to the requirements
Verifying the Caching configuration
Testing endpoint locally
Deploy application
Access to console
Assessing cache key scope
INFO 2022-03-02 15:54:20,831 [[MuleRuntime].uber.03: [emergency_api].get:\emergency_number\(code):api-config.CPU_LITE @76b41172] [processor: get:\emergency_number\(code):api-config/processors/0; event: 05f2c230-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config:MAIN-ENTRY
INFO 2022-03-02 15:54:21,698 [[MuleRuntime].uber.03: [emergency_api].get:\emergency_number\(code):api-config.CPU_LITE @76b41172] [processor: get:\emergency_number\(code):api-config/processors/1/processors/2; event: 05f2c230-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config: FR TIME-REQUEST 21
INFO 2022-03-02 15:54:21,737 [[MuleRuntime].uber.01: cachingStrategy_null @197f28ca] [processor: get:\emergency_number\(code):api-config/processors/2; event: 05f2c230-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config:MAIN-EXIT
INFO 2022-03-02 15:56:40,652 [[MuleRuntime].uber.03: [emergency_api].get:\emergency_number\(code):api-config.CPU_LITE @76b41172] [processor: get:\emergency_number\(code):api-config/processors/0; event: 59784a10-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config:MAIN-ENTRY
INFO 2022-03-02 15:56:40,929 [[MuleRuntime].uber.03: [emergency_api].get:\emergency_number\(code):api-config.CPU_LITE @76b41172] [processor: get:\emergency_number\(code):api-config/processors/1/processors/2; event: 59784a10-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config: ES TIME-REQUEST 40INFO 2022-03-02 15:56:40,944 [[MuleRuntime].uber.01: cachingStrategy_null @197f28ca] [processor: get:\emergency_number\(code):api-config/processors/2; event: 59784a10-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config:MAIN-EXIT
Second attempt to retrieve
INFO 2022-03-02 15:54:20,831 [[MuleRuntime].uber.03: [emergency_api].get:\emergency_number\(code):api-config.CPU_LITE @76b41172] [processor: get:\emergency_number\(code):api-config/processors/0; event: 05f2c230-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config:MAIN-ENTRY
INFO 2022-03-02 15:54:21,698 [[MuleRuntime].uber.03: [emergency_api].get:\emergency_number\(code):api-config.CPU_LITE @76b41172] [processor: get:\emergency_number\(code):api-config/processors/1/processors/2; event: 05f2c230-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config: FR TIME-REQUEST 21
INFO 2022-03-02 15:54:21,737 [[MuleRuntime].uber.01: cachingStrategy_null @197f28ca] [processor: get:\emergency_number\(code):api-config/processors/2; event: 05f2c230-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config:MAIN-EXIT
INFO 2022-03-02 15:56:40,652 [[MuleRuntime].uber.03: [emergency_api].get:\emergency_number\(code):api-config.CPU_LITE @76b41172] [processor: get:\emergency_number\(code):api-config/processors/0; event: 59784a10-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config:MAIN-ENTRYINFO 2022-03-02 15:56:40,929 [[MuleRuntime].uber.03: [emergency_api].get:\emergency_number\(code):api-config.CPU_LITE @76b41172] [processor: get:\emergency_number\(code):api-config/processors/1/processors/2; event: 59784a10-9a41-11ec-9801-f84d8965f96a]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config: ES TIME-REQUEST 40
INFO 2022-03-02 15:56:40,944 [[MuleRuntime].uber.01: cachingStrategy_null @197f28ca] [processor: get:\emergency_number\(code):api-config/processors/2; event: 59784a10-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config:MAIN-EXIT
INFO 2022-03-02 15:58:15,451 [[MuleRuntime].uber.01: [emergency_api].get:\emergency_number\(code):api-config.CPU_LITE @76b41172] [processor: get:\emergency_number\(code):api-config/processors/0; event: 91fb25b0-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config:MAIN-ENTRY
INFO 2022-03-02 15:58:15,504 [[MuleRuntime].uber.01: [emergency_api].get:\emergency_number\(code):api-config.CPU_LITE @76b41172] [processor: get:\emergency_number\(code):api-config/processors/2; event: 91fb25b0-9a41-11ec-9801-f84d8965f96a] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: get:\emergency_number\(code):api-config:MAIN-EXIT
Testing endpoint in CloudHub
Access to console
Try it
Second attempt to retrieve cache response
If you are interested in working with Devoteam S Platform, click here!