Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.enryold:onion-cache:0.0.12'
}
dependencies {
implementation("com.github.enryold:onion-cache:0.0.12")
}
<dependency>
<groupId>com.github.enryold</groupId>
<artifactId>onion-cache</artifactId>
<version>0.0.12</version>
</dependency>
libraryDependencies += "com.github.enryold" % "onion-cache" % "0.0.12"
:dependencies [[com.github.enryold/onion-cache "0.0.12"]]
ICacheLayerDataModel interface comes with 2 methods:
Under /models you can find an example with dynamodb as datasource.
Look at https://jitpack.io/#enryold/onion-cache/
Imagine a system that saves data on DynamoDb, but some datas are very popular in certain period of time with an increase of throughput-rate. In this case is useful to put Redis or Memcached between your webapp and DynamoDb and an LRU cache inside your application to avoid any network call.
First of all we have to create all the layers we need in our system design, with their services, keys, and marshallers
// THIS EXAMPLE CAN BE FOUND UNDER /test FOLDER IN METHOD NAMED test_LRU_REDIS_DYNAMODB_FALLBACK()
// SERVICES FIRST
// IN MEMORY LRU SERVICE, 100 is the size of the LRU map.
InMemoryLRUService inMemoryLRUService = new InMemoryLRUService(100);
// REDIS SERVICE
RedisService redisService = new RedisService("localhost", 6379);
// DYNAMODB SERVICE
DynamoDBService onionDynamoDBService = new DynamoDBService(dynamodb);
// NOW BUILD CACHE LAYERS FOR THE MODEL Person, WITH REPRESENT A PERSON WITH NAME AND SURNAME PROPERTIES.
CacheLayer LRULayer = new CacheLayer<InMemoryLRUService, CacheLayerJsonMarshaller<Person>, Person>()
.withMainService(inMemoryLRUService)
.withMainServiceMarshaller(new CacheLayerJsonMarshaller<>(Person.class, String.class));
CacheLayer DynamoDBLayer = new CacheLayer<DynamoDBService, CacheLayerDynamoDBMarshaller<Person>, Person>()
.withMainService(onionDynamoDBService)
.withMainServiceMarshaller(new CacheLayerDynamoDBMarshaller<>(Person.class, Person.class));
// IN REDIS WE CAN SET DEFAULT EXPIRATION FOR KEYS IN SECONDS
CacheLayer redisLayer = new CacheLayer<RedisService, CacheLayerJsonMarshaller<Person>, Person>()
.withMainService(redisService)
.withMainServiceMarshaller(new CacheLayerJsonMarshaller<>(Person.class, String.class))
.withDefaultExpiration(3600);
// NOW WE ARE READY TO BUILD OUR ONIONCACHE OBJECT, THE ORDER OF addLayer IS IMPORTANT!!
OnionCache<Person> onionCache = new OnionCache<Person>()
.addLayer(LRULayer)
.addLayer(redisLayer)
.addLayer(DynamoDBLayer);
// THEN, WE CAN USER ONIONCACHE TO RETRIEVE OUR OBJECTS FROM THE LAYERS.
Optional<Person> object = onionCache.get(new Person("Jason", "Bourne"));
// HOWEVER, WE CAN ALSO SET A NEW OBJECT, WITH CUSTOM DEFAULT EXPIRATION (This works only in Redis/Memcached)
Person goJason = new Person("Jason", "Bourne");
boolean success = onionCache.set(goJason.getName(), goJason.getSurname(), goJason, 30 );