Expand my Community achievements bar.

SOLVED

acs commons httpCache

Avatar

Level 2

Hi
I am trying to use https://adobe-consulting-services.github.io/acs-aem-commons/features/http-cache/subpages/base-config... for storing JSON object. Can someone provide a working example. I tried using 
https://adobe-consulting-services.github.io/acs-aem-commons/features/http-cache/subpages/creating-ca...
for custom cache store. 

Also I tried 

https://adobe-consulting-services.github.io/acs-aem-commons/features/http-cache/subpages/activating-...
Not sure if I how to configure it correctly.

I am getting Nullpointer exception in CaffeineMemHttpCacheStoreImpl. 
I have added following configuration in code - 
com.adobe.acs.commons.httpcache.store.caffeine.impl.CaffeineMemHttpCacheStoreImpl.xml

<?xml version="1.0" encoding="UTF-8"?>
jcr:primaryType="sling:OsgiConfig"
httpcache.cachestore.caffeine.ttl="{Long}360000"
httpcache.cachestore.caffeine.maxsize="{Long}50"
httpcache.cachestore.caffeine.configName="cacheConfiguration"
/>


com.adobe.acs.commons.httpcache.config.impl.HttpCacheConfigImpl-mycode.xml -

<?xml version="1.0" encoding="UTF-8"?>
jcr:primaryType="sling:OsgiConfig"
httpcache.config.order="1000"
httpcache.config.requesturi.patterns="[(.*)]"
httpcache.config.request.authentication="both"
httpcache.config.expiry.on.create="{Long}50000"
cacheConfigExtension.target="(&amp;(service.factoryPid=com.adobe.acs.commons.httpcache.store.caffeine.impl.CaffeineMemHttpCacheStoreImpl)(config.name=cacheConfiguration))"
cacheKeyFactory.target="(&amp;(service.factoryPid=com.adobe.acs.commons.httpcache.store.caffeine.impl.CaffeineMemHttpCacheStoreImpl)(config.name=cacheConfiguration))"
httpcache.config.cache-handling-rules.pid="[pid-of-rule1, pid-of-rule2]"
/>

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @vsps 

ACS AEM Commons Http Cache Store to store a JSON object:

  1. 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();
    }
}
  1. 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"/>
  1. 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. 



View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hi @vsps 

ACS AEM Commons Http Cache Store to store a JSON object:

  1. 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();
    }
}
  1. 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"/>
  1. 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. 



Avatar

Level 2

Thanks for replay @RajaReddy_3370
Tried to use above code. But none of classes are resolved. I tried adding 

in core/pom -

<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency

in all/pom.xml - 
<embedded>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<target>/apps/app-packages/application/install</target>
</embedded>
<embedded>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<target>/apps/app-packages/application/install</target>
</embedded>

I already had acs-aem-commons-all in pom.
thanks in advance.

 

 

Avatar

Administrator

@vsps Did you find the suggestion helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni

Avatar

Level 2

Thanks  @Raja_Reddy @kautuk_sahni  it worked for me. Earlier my local instance was not building correctly. Removing .m2 folder and rebuilding helped.