acs commons httpCache | Community
Skip to main content
New Participant
February 24, 2024
Solved

acs commons httpCache

  • February 24, 2024
  • 2 replies
  • 1276 views

Hi
I am trying to use https://adobe-consulting-services.github.io/acs-aem-commons/features/http-cache/subpages/base-config.html 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-cache-store.html
for custom cache store. 

Also I tried 

https://adobe-consulting-services.github.io/acs-aem-commons/features/http-cache/subpages/activating-caffeine-cache.html#activating-the-store
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]"
/>

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Raja_Reddy

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. 

2 replies

kautuk_sahni
Employee
March 7, 2024

@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
vspsAuthor
New Participant
March 7, 2024

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

Raja_Reddy
Raja_ReddyAccepted solution
New Participant
February 25, 2024

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. 

vspsAuthor
New Participant
February 26, 2024

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.