Hi @vsps
ACS AEM Commons Http Cache Store to store a JSON object:
- Create a custom cache store implementation by extending the AbstractKeyValueCacheStore class. This implementation should define the getValue and putValue methods to get and store the JSON object respectively. Here is an example implementation:
@Component(service = KeyValueCacheStore.class, property = {
KeyValueCacheStore.KEY_VALUE_CACHE_TYPE_PROPERTY + "=" + "my-json-cache"
})
public class MyJsonCacheStore extends AbstractKeyValueCacheStore<JSONObject> {
@Override
protected JSONObject getValue(String key) {
// Get the JSON object from the cache
return getCache().getIfPresent(key);
}
@Override
protected void putValue(String key, JSONObject value) {
// Store the JSON object in the cache
getCache().put(key, value);
}
@Override
protected Cache<String, JSONObject> createCache() {
// Create a new cache with the desired configuration
return Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.maximumSize(1000) .build();
}
}- Configure the Http Cache Store to use the custom cache store implementation. This can be done by creating an OSGi configuration for the HttpCacheConfig service and setting the cacheStoreType property to the name of the custom cache store implementation. Here is an example configuration:
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
xmlns:cq="http://www.day.com/jcr/cq/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="sling:OsgiConfig"
httpcache.config.order="1000"
httpcache.config.requesturi.patterns="[(.*)]"
httpcache.config.request.authentication="both"
httpcache.config.expiry.on.create="{Long}50000"
cacheStoreType="my-json-cache"/>- Use the Http Cache Store to store and retrieve the JSON object. This can be done by injecting the HttpCacheStore service and calling the put and get methods. Here is an example usage:
@Reference
private HttpCacheStore httpCacheStore;
public void storeJson(String url, JSONObject json) {
// Store the JSON object in the cache
httpCacheStore.put(url, json, Collections.emptyMap());
}
public JSONObject getJson(String url) {
// Get the JSON object from the cache
return httpCacheStore.get(url, JSONObject.class);
}Regarding the Nullpointer exception in CaffeineMemHttpCacheStoreImpl, it's possible that the configuration is not set up correctly or there is an issue with the cache store implementation.