I have implemented a Spring-boot Camel batch application running every 5 mins which is using camel-sql component to query some data from table. I am using java DSL implementation and configuring my routing endpoints inside RouteBuilder.configure. Everything is working fine as expected. But now as part of optimization I am planning to cache some of the query results which are not changing frequently like some global configuration tables, location timezone table etc.
I have gone through Camel EHcache documentation, but not getting the right understanding of the standard way of implementation.
My expectation is like the following
Suppose my routing is,
from(timercomponent)
.to(SqlComponent1)
.process(ResultProcessor::processResult1)
.to(SqlComponent2) // needs to be cached
.process(ResultProcessor::processResult1)
....
...
I don't want SqlComponent2 to hit the DB always and it should use a cached value from the first execution with an expiry time of 1 week.
Also I just wanted to know which is the best cache implementation to be used in this scenario.
What do you mean? Ehcache or something else? Or how to configure the Ehcache cache?
From the Ehcache point of view, you will need a Cache using a 1 week TTL expiry.
Then, it's been a while I've implemented caching with Camel but the doc seems to say that an Ehcache idempotent repository should do the trick.
Related
Can EhCache 3.x caches be configured to implement a refresh operation (see https://github.com/google/guava/wiki/CachesExplained#refresh for an example as implemented using Guava) - i.e. if the entry is expired, return the expired entry until the refreshed entry has been retrieved? This looks like it might have been possible under the 2.x versions using a decorator like http://www.ehcache.org/apidocs/2.10.3/net/sf/ehcache/constructs/refreshahead/RefreshAheadCache.html (although even this doesn't really match my use case, it looks like it always performs the refresh rather than only on expired entries) but I can't find any equivalent for the more recent releases.
Is this something that should be handled at a higher level? In the context of a Spring Boot application, is there another mechanism by which this could be done while using EhCache as the backing implementation?
I have a scenario where I want to query database once and after that want to cache the whole data in memory.
I got the suggestion for in-memory elastic search, I have googled it understand what it is and how can I implement it in my spring boot application, but I didn't find any appropriate solution.
Any suggestion on this like how can I implement this in my spring boot app and what would be the approach.
There used to be an in-memory storage type in Elasticsearch in 1.x, but it has been removed in 2.x and later versions. If your working set is small enough it might be mapped to memory in full, but you cannot really control that other than having enough memory.
If you want to run an embedded / in-process Elasticsearch with your Spring Boot application that feature was removed in 5.x and this blog post explains why.
roo-1.3.1.RC1.
I created simple 3-4 tables with proper PK and FK relations.
I loaded data into it.
I use spring-roo - reverse engg techniques to generate the JSF based UI.
I have a List of Employees which are showing in the default JSF generated page with proper pagination.
if the no of employees is very hing in DB, and If I want to load in to ehCache / hibernate 2nd level cache during application start up - then how Can I able to do that ?
ehCache provides cache warm up interface to load the data into cache before cache is available to the Application?
How Can i achieve this ?
Spring Roo generates (almost) simple Spring + Hibernate application. So, you just must follow the hibernate documentation to configure 2nd level cache. Roo will not disturb on any Hibernate annotation in your classes or changes in the persistence.xml file.
Here you will find a tutorial about it. Sure there are tons of information about it.
Good luck!
Does ehCache have any feature of auto refreshing? If yes, please let me know how to do that. If not, please let me know few possible and efficient ways in which ehCache refresh can be done.
There is no way for EhCache to know how the data that was inserted into to it, was produced. That part is totally application specific. There fore EhCache cannot refresh itself automatically- it can't pull the data, the data has to be pushed into it.
In order to do that you need to add another component to your application that will run at a scheduled time and while insert the appropriate data into EhCache.
Spring has awesome support for scheduled tasks. Check out this part of the documentation
ehCache has builtin eviction algorithms that you can use by setting timeToIdleSeconds and timetoLiveSeconds this will tell ehcache when to eject that data from the cache and force a reload of the data from your persistence data source. Read the following ehCache document on more detailed instruction on configuring TTL http://ehcache.org/documentation/configuration/data-life
I am using mybatis within a Jax-RS (Jersey) restful web app. So automatically, I dont have a session or state management.
Question is how can I use the caching features of mybatis ?
Caching in MyBatis is very straight forward. Per the documentation (pg 42 of the user manual http://mybatis.googlecode.com/svn/trunk/doc/en/MyBatis-3-User-Guide.pdf)
By default, there is no caching enabled, except for local session caching, which improves performance and is required to resolve circular dependencies. To enable a second level of caching, you simply need to add one line to your SQL Mapping file:
MyBatis 3 - User Guide
6 June 2011 43
<cache/>
Literally that’s it.
The common pitfalls I had while doing this:
On the mapper you add the cache element to; if you have dependent entities, make sure to explicitly flush the cache when required. Even though flushing is already done for you on insert, update, delete for the elements in the mappings you have set the cache element, sometimes you have to flush a cache due to updates/deletes/etc defined in different xml mappings.
Basically, when you're thinking about your caching, you should ask yourself, "When this entity is changed, do I want it to flush a cache for an entity in a different mapping?" If the answer is yes, use cache-ref element as opposed to just cache.
Ex from page 45 of the doc:
<cache-ref namespace=”com.someone.application.data.SomeMapper”/>