https://github.com/allegro/json-cache
https://github.com/allegro/json-cache
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/allegro/json-cache
- Owner: allegro
- License: apache-2.0
- Created: 2023-10-04T12:16:56.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-28T13:47:18.000Z (9 months ago)
- Last Synced: 2025-04-11T23:49:24.391Z (8 months ago)
- Language: Java
- Size: 97.7 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# json-cache
A set of libraries focused on extracting abstraction layer for caching.
## json-cache-jackson
A library that allows for customization of POJO deserializers in `ObjectMapper` by enhancing them
with caching capabilities.
Consider the following example:
```java
public class CategoryParameter {
private final int id;
private final String value;
public CategoryParameter(
@JsonProperty("id") int id,
@JsonProperty("value") String value
) {
this.id = id;
this.value = value;
}
// getters and setters
}
```
and the following JSON payload:
```json5
{
categories: [
{
name: "cat 1",
parameters: [
{
id: 10,
value: "sanki"
},
{
id: 111,
value: "Nowy"
},
// ...
{
id: 123,
value: "wartość"
}
]
},
{
name: "cat 2",
parameters: [
{
id: 111,
value: "Nowy"
},
{
id: 555,
value: "tak"
},
{
id: 123,
value: "wartość"
}
]
}
// ...
]
}
```
Normally Jackson will spawn multiple instances of `CategoryParameter` even if such objects contain the same properties.
This library aims to reduce amount of consumed memory and computing time by caching and de-duplicating object instances
(object pooling).
### How to use
Follow those simple steps:
- add dependency for the module:
```groovy
dependencies {
implementation group: 'pl.allegro.tech', name: 'json-cache-jackson', version: '...'
// above module doesn't supply dependent libraries to avoid dependency pollution, so make sure you include them yourself
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '...'
}
```
- supply a proper _annotation_:
```java
@CacheableEntity(keyComponent = "id", cacheName = "categoryParametersCache")
public class CategoryParameter {
//...
}
```
- implement cache resolver
```java
public class MyCacheResolver implements CacheResolver {
private final Map> cacheMap = new HashMap<>();
@Override
@SuppressWarnings("unchecked")
public EntityCache resolveCache(String cacheName) {
return (EntityCache) cacheMap.computeIfAbsent(cacheName, key -> new MapBasedEntityCache<>());
}
}
```
- configure Jackson's `ObjectMapper`
```java
CacheResolver cacheResolver = new MyCacheResolver();
CacheKeyBuilderFactory keyBuilderFactory = new CacheKeyBuilderFactory(
List.of(new JsonComponentExtractingStrategy())
);
Module cachedDeserializationModule = new CachedDeserializationModule(
new CacheApplyingDeserializerModifier(keyBuilderFactory, cacheResolver)
);
ObjectMapper mapper = new ObjectMapper(); // or use existing
mapper.registerModule(cachedDeserializationModule);
```
and that's it - you're good to go :)