Skip to content

Kims-DeveloperGroup/hibernate-arcus

Repository files navigation

hibernate-arcus

Hibernate 2nd Cache implemetation using Arcus cache cloud

Arcus is memcached based cache cloud
Hibernate second cache interface puts the result entities into a provided cache storage and reuse them without accessing a database.
hibernate-arcus offers an implementation, and employs arcus cache cloud as a cache storage



Quick Tutorial

1. Add the hibernate-arcus

Available in MavenCentral

<dependency>
  <groupId>com.github.kims-developergroup</groupId>
  <artifactId>hibernate-arcus</artifactId>
  <version>1.2.0-RELEASE</version>
</dependency>

Snapshot maven repository: https://oss.sonatype.org/content/repositories/snapshots/

2. Set the properties below

if you use spring jpa

//application.properties

spring.jpa.properties.hibernate.cache.region.factory_class=com.devookim.hibernatearcus.factory.HibernateArcusRegionFactory
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true

spring.jpa.properties.hibernate.cache.arcus.serviceCode=${arcus_service_code}  # required
spring.jpa.properties.hibernate.cache.arcus.poolSize=${arcus_client_pool_size} # default poolSize=1
spring.jpa.properties.hibernate.cache.arcus.host=${hostName}:{$port}           # required

or if you use only hibernate

//hibernate.properties

hibernate.cache.region.factory_class=com.devookim.hibernatearcus.factory.HibernateArcusRegionFactory
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true

hibernate.cache.arcus.serviceCode=${arcus_service_code}  # required
hibernate.cache.arcus.poolSize=${arcus_client_pool_size} # default poolSize=1
hibernate.cache.arcus.host=${hostName}:{$port}           # required

In case that query cache is not required, and set hibernate.cache.use_query_cache=false
However, hibernate.cache.use_query_cache=true is necessary set with hibernate.cache.use_second_level_cache=true

3. How to use cache

  1. Attach @Cache annotation to an entity class
    Entities are cached with an id.
@Cache(usage = ${CacheConcurrencyStrategy}, region = "${regionName}")
public static class ParentDomainData implements Serializable {
  @Id
  long id;
...
}

Note: when changing CacheConcurrencyStrategy, regionName also should be modified not to reuse exsting cache items.
For more detail: When @Cache.usage changes, casting exception is thrown


  1. @NaturalId
    You may want to cache entities with a natural id, then attach @NaturalId annotation
@Cache(usage = ${CacheConcurrencyStrategy}, region = "${regionName}")
public static class ParentDomainData implements Serializable {
...
@NaturalId
long someNaturalId;
...
}
  1. @QueryHint
    You may want to cache queries, then attach @QueryHint
@QueryHints(value = {@QueryHint(value="true", name = HINT_CACHEABLE)})
    Optional<Entity> findByName(String name);



How to run tests

  1. Run arcus cache locally (DockerImage is ready here)

Just run the docker-compose in the project root

docker-compose up
gradlew clean test --info

Additional Configuration Properties

hibernate.cache.arcus.fallbackEnabled  (default: true) // whether to use fallback mode or not.
hibernate.cache.arcus.initFallbackMode (default: false) // Initial value of fallback mode
hibernate.cache.arcus.reconnectIntervalInSec (default: 10000) unit mills // ArcusClient retry connection interval in seconds
hibernate.cache.arcus.opTimeout (default 10000) unit mills // arcus client cache operation timeout.
hibernate.cache.arcus.healthCheckIntervalInSec(default 10) unit sec // interval time of health check
hibernate.cache.arcus.domainData.evictionRegionGroupOnCacheUpdate(default "") // domain data region group to be evicted on cache update

Contribution

Always welcome all shapes of your contribution and use-cases