This document discusses configuring and testing caching for a web service using Mule and a NonPersistentManagedObjectStore. The web service retrieves data from a database and caches the response. Deleting data from the database has no effect, as subsequent requests use the cached response. A separate flow is created to invalidate the cache by calling ee:invalidate-cache. Testing this by hitting the invalidation endpoint clears the cache, forcing the web service to retrieve fresh data from the database on the next request.
6. Now we will configure our cachingStrategy with
NonPersistentManagedObjectStore as follows:-
<ee:object-store-caching-strategy name="cachingStrategy"
doc:name="cachingStrategy">
<managed-store storeName="myNonPersistentManagedObjectStore"
maxEntries="-1" entryTTL="20000" expirationInterval="5000"/>
</ee:object-store-caching-strategy>
So it is very simple right ?
7. In next step we will be running and testing the
application. This web servicewill intereact with database
retrieve a row from the database and show the row value
in the SOAP response.
8. Here how we will test the web service :-
You can see that we have used SoapUI to test the web service. Now
when we hit the service, you can see in the SOAP request, it takes id
as input and then fetches all the data from the database for that id.
9. Now, what we will do is manually deleting the entire row
from the database. In my case, I have used sql server
and deleted the row from the table using a SQL query:-
10. With this in place, if we again hit the service, we will get
the same response as we got earlier:-
11. You can see that, though the entire row is deleted from
the database table, it’s still showing the response, which
means it is fetching the response from cache and not
hitting the actual database
12. Now, let’s create a Mule flow that will invalidate and
clear all the Cache
So, with following flow we can clear all the Cache that
has been used:-
13. Corresponding Mule config will be :-
<flow name="cacheinvalidate" doc:name="cacheinvalidate">
<http:inbound-endpoint exchange-pattern="request-response"
address="http://localhost:8083/invalidate" doc:name="HTTP"/>
<object-to-string-transformer doc:name="Object to String"/>
<ee:invalidate-cache cachingStrategy-ref="cachingStrategy"/>
<set-payload value="All cache invalidated" doc:name="Set
Payload"/>
</flow>
As we can see the above flow will use ee:invalidate-cache to
clear all the cache.
14. Now if we hit the url:- http://localhost:8083/invalidate
following way :-
15. So, we can hit the web service again to test the output.
So, now if we hit service again after the cache has been
invalidated, we will find the following result
As you can see, it clearly shows no records are there in the
database, which means the cache has invalidated successfully and
the service is actually hitting the database.
16. It’s simple example.. isn’t it … we don’t need to
configure any complex configuration for the cache as
compared to the configuration we need to do in case of
using cache with EHCache.
17. Hope you enjoyed the simple usage of Cache with
NonPersistentManagedObjectStore and the way of
invalidating it.