Does ehCache have any option of auto refreshing? - spring

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

Related

ResultSet caching in Spring-boot Camel SQL

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.

Advantage of using ehcahce over a static HashMap

I have always used the java singleton class for my basic caching needs.
Now the project is using ehcache and without looking deeply into source code, I am not able to figure out what was wrong with the singleton pattern.
i.e What are the benefits of using the ehcahce framework except that the caching can be done by using xml configuration and annotation without writing the boilerplate code (i.e a static HashMap)
It depends on what you need from your caching mechanism. Ehcache provides a lot of cool features, which require a lot of well designed code to do it manually:
LRU, LFU and FIFO cache eviction policies
Flexible configuration
Persistence
Replication
many more ...
I would recommend you go through them at http://ehcache.org/about/features and decide do you really need something in your project.
The most important one:
The ability to overflow to disk - this is something you don't have in normal HashMap and writing something like that is far from trivial. EhCache can function as simple to configure key-value database.
Even if you don't use overflow to disk, there's a large boilerplate to write with your own implementation. If loading the whole database would be possible, that using memory database with persistence on write and restoring on startup would be the solution. But memory is limited and you have to remove the elements from memory. But which one, based on what? Also, you must assert cache elements are not too old. Older elements should be replaced. If you need to remove elements from cache, you should start from the outdated ones. But should you do it when user requests something? It will slow down the request. Or start your own thread?
With EhCache you have the library in which all those issues are addressed and tested.
Also there is a clustered closed source version of ehcache, which allows you to have a distributed cache. That might be one reason you might want to consider using ehcache.

How do I invalidate a single entry from springmodules method cache?

We are using springmodules method caching (Spring 2.0.7) in combination with ehcache to cache data which is requested often, in order to take load from the database.
E.g. we are caching the method result of public Profile getUserProfile(User u) {...}: when the user updates data in his own profile, it would be nice to invalidate the cached Profile only for this single User directly after the update, so that the changes are reflected in the user interface right away.
Is there a way to achieve this without invalidating the complete cache for this method?
Or is there a better option than springmodules-caching for this use case?
Thanks a lot for any hints.
Or is there a better option than springmodules-caching for this use case?
Use Spring 3.1 has this new cache feature.
How do I invalidate a single entry from springmodules method cache?
By #CacheEvict from Spring 3.1
The Spring Reference for 3.1 has a nice chapter for this: 28.3 Declarative annotation-based caching

ibatis / mybatis caching within a restful webservice

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”/>

Using Ehcache API in Hibernate

Using Ehcache API How can we write our own data cache in Hibernate?
This is really two questions:
Can I write my own data cache? Of course, but it's hard to see why. How will yours be different or better than EhCache?
How can I wire my own data cache into Hibernate? Sorry, I don't know. I'd use EhCache or Terracotta or something else that exists.

Resources