Urbanleg
Urbanleg

Reputation: 6532

How exactly does one integrate a cache in his project (spring + hibernate)

I have a project that supports 10's of concurrent users.

My project is a spring + hibernate project with a MYSQL DB.

I would like to keep a cache for some of my entities (i.e. Player)

I have a couple of questions:

1) how do I exactly work with a cache (when i have one) ? if i have to persist a Player do i change the cache and immediately persist with hibernate?

2) Does spring support a cache mechanism ? if so how does one work with it?

Upvotes: 1

Views: 64

Answers (1)

Maksym Demidas
Maksym Demidas

Reputation: 7817

Hibernate

Hibernate has Second level cache. To start work with you need to:

  • choose some cache provider (EHCache, Infinispan, ...).
  • configure cache region (and choose corresponding strategy depending on your situation)
  • enable cache for some entity

It is declarative, most of the time you do not need to change any application code.

Spring

Spring has cache abstraction. There are some common steps (choose chache provider, configure cache regions). But it is more general pourpose cache, not related to Hibernate entities and transactions. So you must do more work (annotate all necessary methods with annotations).

In general if your Player class is a Hibernate entity then it will be better to go with Hibernate cache. It may be not true if you have some special demands.

Hope it helps.

Upvotes: 1

Related Questions