Caching in Spring Web service? - java

I need to load some data from DB to Cache on server start up.And once a request came, need to take the data from this cache.Also need to refresh cache in frequent intervals.It would be help if somebody provide way for achieving this.I am using Spring 3.1.
Thanks

There are lots of cache implementations available:
The Spring Cache abstraction
Ehcache (which is one possible provider for Spring Cache)
Guava's LoadingCache
Infinispan as user1516873 suggests
A plain ConcurrentHashMap from the JDK if you don't want more dependencies
etc.

I think this is what you are looking for:
Spring: hibernate + ehcache
As you will see my recommendation is to use hibernate second level cache and your problem will be fixed at configuration level.

Try Infinispan.
It modern, open source and easy to use cache. You can embed it and use directly, or use it as second level cache in hibernate.

Related

EhCache 3.8.1 BootstrapCacheLoader

I have a problem when using cache across different modules. At first, I was using Ehcache 2.10.6 so in my configuration ehcache.xml file I had something like this:
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
and I was using it as cache.bootstrap();
After upgrading to 3.x version (3.8.1) , this is not available nor I can see any replacements. Is there any replacement to do or something else to replicate the configuration above?
Thanks.
RMI based replication has been discontinued in Ehcache 3.x. Its implementation in Ehcache 2.x was lacking in semantics: writes could be lost, reads were not guaranteed to be consistent, etc ...
Instead you should look into cache clustering with Terracotta.

How do I use Spring's #Cacheable with Aerospike as the cache?

I just want to use Aerospike as the backing cache for Spring CacheManager.
Should I use spring data aerospike when I don't intend to use Aerospike as a data-store but only as a cache?
Is there any implementation available similar to HazelcastCacheManager or do I need to write my own?
Any help is appreciated.
Found this implementation for Aerospike Cache Manager.
https://github.com/shaileshmishra008/spring-cache-aerospike
I was able to write a version using this project as a reference.
There were some big changes in the Cache area in Spring Data Aerospike - starting from versions 2.5.0/3.0.0.
A Medium article about Caching with Spring Boot and Aerospike:
https://medium.com/aerospike-developer-blog/caching-with-spring-boot-and-aerospike-17b91267d6c

How to configure Amazon Elasticache with Hibernate second level cache (Hibernate 5.0.7 & Wildfly 10.0.0)

I want to configure Hibernate's second level cache to use AWS Elasticache (currently trying to use memcached). I've looked at things like this, but can't seem to get it to work.
In the Hibernate 5.0 docs, it says that Wildfly automatically configures Hibernate's second level cache to use Inifinispan. Is there an easy way to disable this? Does configuring the cache to use elasticache (correctly) override this?
How can I configure the cache to use memcached in Hibernate 5? Several places say to set the property "hibernate.cache.provider_class" but the Hibernate 5.0 docs say that the property "hibernate.cache.region.factory_class" needs to be set to determine which provider to use. Also are old versions of hibernate-memcached (like this) still usable?

hazelcast : changing configuration programatically doesnt work

I am unable to configure/change the Map(declared as part of hazelcast config in spring) properties after hazelcast instance start up. I am using hazelcast integrated with spring as hibernate second level cache. I am trying to configure the properties of map (like TTL) in an init method (PostConstruct annotated) which is called during spring bean initialization.
There is not enough Documentation , if there is please guide me to it.
Mean while i went through this post and found this Hazelcast MapStoreConfig ignored
But how does the management center changes the config, will it recreate a new instance again ?
Is hazelcast Instance light weight unlike session factory ? i assume not,
please share your thoughts
This is not yet supported. JCache is the only on-the-fly configuration data structure at the moment.
However you'll most probably be able to destroy a proxy (DistributedObject like IMap, IQueue, ...), reconfigure it and recreate it. Anyhow at the time of recreation you must make sure that every node sees the same configuration, for example by storing the configuration itself inside an IMap or something like that. You'll have to do some wrapping on your own.
PS: This is not officially supported and an implementation detail that might change at later versions!
PPS: This feature is on the roadmap for quite some time but didn't made it into a release version yet, it however is still expected to have full support at some time in the future.

Hibernate-Search with Lucene without database? [duplicate]

Is it possible to use hibernate-search only for it's annotations (bean => document/document => bean mapping), without using a database at all? If so, are there any online samples that show basically how to set this up?
I found the following: http://mojodna.net/2006/10/02/searchable-annotation-driven-indexing-and-searching-with-lucene.html, but I'd prefer hibernate-search if it supports my use case.
Hibernate search 3.4 has decoupled the query engine from Hibernate Core. For instance, Hibernate Search is reused to implement queries with Infinispan. I don't know if the code is packaged so that you could use HS with, let's say Spring and JDBCTemplate (something I would like to do). That's a lead I will investigate later but maybe you can check it out...
Starman is correct, Hibernate Search in version 3.4 is abstracting the search engine from Hibernate Core, and the Infinispan Query is an integration example which works fine without a database. There would be no problems with Spring either, but you'd need to make sure to send update event to the query engine so that the index doesn't get out of synch. When using Hibernate the advantage is that it transparently listens for changes to the database and applies them to the index at transaction commit, so the index is always in synch (or close, if configuring Search to use async backends).
I'd suggest to look into the code of Infinispan Query, as it's very small and just delegating calls to expose an Infinispan flavoured API. Most of the code is tests or integration to properly manage the lifecycle of the engine: start and stop it together with Infinispan.
I don't think that's possible because when you enable Hibernate search you are enabling that on a Entity and that Entity has references to the table and the search index.

Categories