io.soabase:soabase-cache

A suite of Dropwizard bundles and utilities that aid in building Service Oriented Applications.

License

License

GroupId

GroupId

io.soabase
ArtifactId

ArtifactId

soabase-cache
Last Version

Last Version

0.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

A suite of Dropwizard bundles and utilities that aid in building Service Oriented Applications.
Project Organization

Project Organization

Soabase
Source Code Management

Source Code Management

https://github.com/soabase/soabase-cache

Download soabase-cache

How to add to project

<!-- https://jarcasting.com/artifacts/io.soabase/soabase-cache/ -->
<dependency>
    <groupId>io.soabase</groupId>
    <artifactId>soabase-cache</artifactId>
    <version>0.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/io.soabase/soabase-cache/
implementation 'io.soabase:soabase-cache:0.1.1'
// https://jarcasting.com/artifacts/io.soabase/soabase-cache/
implementation ("io.soabase:soabase-cache:0.1.1")
'io.soabase:soabase-cache:jar:0.1.1'
<dependency org="io.soabase" name="soabase-cache" rev="0.1.1">
  <artifact name="soabase-cache" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.soabase', module='soabase-cache', version='0.1.1')
)
libraryDependencies += "io.soabase" % "soabase-cache" % "0.1.1"
[io.soabase/soabase-cache "0.1.1"]

Dependencies

compile (1)

Group / Artifact Type Version
io.dropwizard : dropwizard-core jar 1.0.0

test (3)

Group / Artifact Type Version
io.dropwizard : dropwizard-testing jar 1.0.0
io.dropwizard : dropwizard-jdbi jar 1.0.0
org.hsqldb : hsqldb jar 2.3.4

Project Modules

There are no modules declared in this project.

Build Status Maven Central

soabase-cache

Simple caching proxy that integrates with Dropwizard

Features

  • Proxies any Interface to cache the method return value
  • Cache keys based on class name, method name, parameter values or any combination
  • Supports any backing store for cached objects - default uses in-memory hash map

Usage

  1. Create Interfaces that model the objects to be cached, marking cacheable methods with cache annotations
  2. Create the concrete implementation of the Interface
  3. Allocate a CacheBackingStore instance either directly or using CacheBackingStoreFactory
  4. Create the cache proxy using CachingControllerBuilder

Simple Example

...

public interface PersonContainer {
    @Cache
    Person getPerson(@CacheKey int id);
    
    @CacheClear
    void addPerson(Person p, @CacheKey int id);
}

public class PersonContainerImpl implements PersonContainer {
    ...
}

...

CacheBackingStore store = cacheBackingStoreFactory.build(environment);
PersonContainer containerImpl = new PersonContainerImpl();
PersonContainer container = CachingControllerBuilder.build(store, containerImpl, PersonContainer.class);

...

Person p = container.get(id); // next call to container.get with the same ID will be cached

container.put(newP, id);  // clears the cache entry for id

Group/Artifact

Soabase Cache is available from Maven Central

GroupId ArtifactId
io.soabase soabase-cache

Reference

Annotations

@Cache

Marks that the annotated method's return value is to be cached. See below for details on how the cache key is generated.

@CacheClear

Marks that the annotated method's value is cleared from the cache. See below for details on how the cache key is generated.

@CacheKey

Controls how cache keys are generated (see below). Can be applied to Interfaces, methods and/or parameters.

Key Generation

A list of "key parts" is generated and then combined to create the cache key.

  • Part 0 - Part 0 is based on the Interface name. If the Interface is annotated with @CacheKey and the annotation value is not an empty string, that value is used as Part 0. Otherwise, The Interface's full name is used.
  • Part 1 - Part 1 is based on the Method. If the method is annotated with @CacheKey, Part 1 is either the annotation value or the method name if the annotation value is an empty string. If the method is not annotated with @CacheKey then there is no Part 1.
  • Parts 1+ - The remaining parts consist of any parameters annotated with @CacheKey. The value is a concatenation of the annotation value and the parameter value.

The key parts are combined using the configured KeyPartCombiner. The StandardKeyPartCombiner joins all the parts separated with a - (dash).

Backing Store

The actual caching of objects is done by the configured CacheBackingStore. The library comes with MemoryCacheBackingStore which stores objects in a Guava Cache Map.

CacheKeyAccessor

To get the value of a method parameter, String.valueOf() is called. However, the object can implement CacheKeyAccessor to provide a custom key value.

Versions

Version
0.1.1
0.1.0