18
Caching and invalidating with managed-store

Caching invalidating with managed store

Embed Size (px)

Citation preview

Page 1: Caching invalidating with managed store

Caching and invalidating with managed-store

Page 2: Caching invalidating with managed store

Today’s topic we will be discussing the NonPersistentManagedObjectStore and we will not be doing any complex Caching configuration with spring beans in our Mule config.

.

Page 3: Caching invalidating with managed store

How ??? I will show you how

.

Page 4: Caching invalidating with managed store

So, let us consider we have configured a web service with the Mule cache scope in following way:-

Page 5: Caching invalidating with managed store

<flow name="ServiceFlow" doc:name="ServiceFlow"><http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" path="mainData" doc:name="HTTP" /><object-to-string-transformer doc:name="Object to String"/> <ee:cache doc:name="Cache" cachingStrategy-ref="cachingStrategy" > <vm:outbound-endpoint exchange-pattern="request-response" path="Flow1-WT-Main" doc:name="VM"/> <object-to-string-transformer doc:name="Object to String"/> </ee:cache></flow> <flow name="ServiceFlow2" doc:name="ServiceFlow2"><vm:inbound-endpoint exchange-pattern="request-response" path="Flow1-WT-Main" doc:name="VM"/> <cxf:jaxws-service serviceClass="com.test.services.schema.maindata.v1.MainData" doc:name="SOAP"/><component class="com.test.services.schema.maindata.v1.Impl.MainDataImpl" doc:name="JavaMain_ServiceImpl"/></flow>

Corresponding Mule flow will be :-

Page 6: Caching invalidating with managed store

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 ?

Page 7: Caching invalidating with managed store

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.

Page 8: Caching invalidating with managed store

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.

Page 9: Caching invalidating with managed store

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:-

Page 10: Caching invalidating with managed store

With this in place, if we again hit the service, we will get the same response as we got earlier:-

Page 11: Caching invalidating with managed store

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

Page 12: Caching invalidating with managed store

Now, let’s create a Mule flow that will invalidate and clear all the CacheSo, with following flow we can clear all the Cache that has been used:-

Page 13: Caching invalidating with managed store

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.

Page 14: Caching invalidating with managed store

Now if we hit the url:- http://localhost:8083/invalidate following way :-

Page 15: Caching invalidating with managed store

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.

Page 16: Caching invalidating with managed store

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.

Page 17: Caching invalidating with managed store

Hope you enjoyed the simple usage of Cache with NonPersistentManagedObjectStore and the way of invalidating it.

Page 18: Caching invalidating with managed store

Thank You