0

I am using hibernate 5.3.7 with ehcache 3.7 and I would like to cache entities which do not exist.

Example:

I try to find an entity by id 10, if it does not exist it should return null/Optional.empty() until the cache is evicted.

The reason for this is that we store user messages in the database with region specific languages falling back to the parent language.

Example:

If I request the message button.accept for the language es_PA and it does not exist I should return the message for the language es.

So there is a high probability to get requests for entities which don't exist but should be cached.

My current configuration is this:

spring:
  jpa:
    properties:
      hibernate:
        cache:
          provider: org.ehcache.jsr107.EhcacheCachingProvider
          region:
            factory_class: jcache

And the entity class is annotated with:

@Cacheable @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

PS: I am also using Spring boot 2.1.3

Fabian Mendez
  • 512
  • 5
  • 15

1 Answers1

0

In that case you maybe shouldn't cache the entities but the result of the repository method.

That way you can also cache Optional

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82