Expand my Community achievements bar.

SOLVED

best practice for caching content in services for servlets (cloud)

Avatar

Level 9

We have several servlets, e..g "register" and "login".

Some of these need to read a list of allowed countries, or swear words (so they cant create accounts with swear words in the display name etc).

 

If this was non-cloud we would make these OSGI configs, which have the beautiful OSGi editor tools.

 

However, cloud has had this removed, so we have to improvise without OSGi.  We are planning to put the list of words in a txt file in DAM in csv format.   Then our content editors can upload new versions whenever needed.  Its pretty crude and error prone, and users could get at the data.

 

in our service layer, we can presumably read the DAM files using resource resolvers, queryManagers or similar.

 

However, reading these files on every request is going to be slow or use server resources.

 

Presumably we can cache them in the service layer, by just putting them into a StringBuffer or similar, and track the time they were last updated, and synchronize the methods

 

Is there some better way to access DAM resources which will cache between requests?  The files will be rarely updated (e.g. once a month), but as we will want updates to go live quickly, we would need to poll the file every 30 mins or so.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @TB3dock 

 

Option-1: If the data to be cached in not huge, you can use Java Map/List. Example: https://www.albinsblog.com/2018/01/developing-caching-service-to-cache-static-data-adobe-experience-... 

 

Caution:

Storing data in a Java Map within the AEM application means that the data will reside in memory. Depending on the size and number of cached objects, this can increase the memory consumption of your AEM instance. It's important to ensure that your AEM environment has enough memory allocated to handle the cached data without impacting the overall performance or stability.

 

Option-2: if you have multiple requirements for In-memory caching, have a look at EhCache as well

Simple Integration: Ehcache integrates seamlessly with AEM and provides a straightforward configuration setup.
Distributed Caching: Ehcache allows you to distribute the cache across multiple instances, making it suitable for clustered or distributed AEM environments.
Comprehensive Features: Ehcache offers various features such as expiration policies, eviction strategies, in-memory data storage, and disk overflow, allowing you to fine-tune caching behavior to meet your requirements.


Aanchal Sikka

View solution in original post

4 Replies

Avatar

Employee Advisor

Hi,

 

To efficiently access DAM resources and cache them between requests in AEM as a Cloud Service:

  1. Store configuration files in AEM DAM in CSV format.
  2. Implement a service layer using AEM's ResourceResolver or Sling APIs to access DAM resources.
  3. Implement a caching mechanism (e.g., CQ Commons Cache API or Apache Sling Caching) to store and retrieve the configuration data.
  4. Set up a scheduler or timer task to periodically refresh the cache based on updates to the DAM files.

By caching the configuration data and periodically refreshing it, you can minimize the need for frequent reads from DAM while ensuring up-to-date information is available.

 
 
 

Avatar

Level 1

Thanks this is useful.  Out of interest is there ant advantage to using a page properly instead?  The users would then paste in the CSV vs uploading it

 

Avatar

Correct answer by
Community Advisor

Hello @TB3dock 

 

Option-1: If the data to be cached in not huge, you can use Java Map/List. Example: https://www.albinsblog.com/2018/01/developing-caching-service-to-cache-static-data-adobe-experience-... 

 

Caution:

Storing data in a Java Map within the AEM application means that the data will reside in memory. Depending on the size and number of cached objects, this can increase the memory consumption of your AEM instance. It's important to ensure that your AEM environment has enough memory allocated to handle the cached data without impacting the overall performance or stability.

 

Option-2: if you have multiple requirements for In-memory caching, have a look at EhCache as well

Simple Integration: Ehcache integrates seamlessly with AEM and provides a straightforward configuration setup.
Distributed Caching: Ehcache allows you to distribute the cache across multiple instances, making it suitable for clustered or distributed AEM environments.
Comprehensive Features: Ehcache offers various features such as expiration policies, eviction strategies, in-memory data storage, and disk overflow, allowing you to fine-tune caching behavior to meet your requirements.


Aanchal Sikka

Avatar

Community Advisor

@TB3dock do you want those config values to be server side loaded and rendered client side or client side loaded and client side rendered?

 

As you took reference of osgi configs.. I assume you need country list to be accessed server side i.e in our author and publish instances.. so can try storing those values in a ACS generic list, a data page kind of page.. and acess that page in your servlet and display as needed.