Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

JCS Caching

Avatar

Former Community Member

Has any one used JCS Caching with AEM?

We are planning to use JCS Caching! as a POC

If any one can share som exp wrt the same.

Thanks,

Prem pratick kumar

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

In this example, the servlet extends SlingSafeMethodsServlet to handle GET requests. It includes methods to generate a cache key, retrieve cached response, cache a response, and process the request to generate the response.

The JCS cache instance is created using the cache region name (myCacheRegion). The getCachedResponse method retrieves the cached response using the cache key, while cacheResponse caches the response for future use. The processRequest method is responsible for processing the request and generating the response.

import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

public class MyCachingServlet extends SlingSafeMethodsServlet {

    private static final String CACHE_REGION = "myCacheRegion";
    private JCS cache;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String cacheKey = generateCacheKey(request);

        // Check if the response is already cached
        String cachedResponse = getCachedResponse(cacheKey);
        if (cachedResponse != null) {
            response.getWriter().print(cachedResponse);
            return;
        }

        // Process the request and generate the response
        String responseData = processRequest(request);

        // Cache the response for future use
        cacheResponse(cacheKey, responseData);

        response.getWriter().print(responseData);
    }

    private String generateCacheKey(SlingHttpServletRequest request) {
        // Generate a unique cache key based on request parameters, path, etc.
        // Example: String cacheKey = request.getPathInfo() + "_" + request.getParameter("param");
        // Return the generated cache key
    }

    private String getCachedResponse(String cacheKey) {
        try {
            if (cache == null) {
                cache = JCS.getInstance(CACHE_REGION);
            }
            return (String) cache.get(cacheKey);
        } catch (CacheException e) {
            // Handle cache exception
        }
        return null;
    }

    private void cacheResponse(String cacheKey, String responseData) {
        try {
            if (cache == null) {
                cache = JCS.getInstance(CACHE_REGION);
            }
            cache.put(cacheKey, responseData);
        } catch (CacheException e) {
            // Handle cache exception
        }
    }

    private String processRequest(SlingHttpServletRequest request) {
        // Process the request and generate the response data
        // Return the response data
    }
}

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

In this example, the servlet extends SlingSafeMethodsServlet to handle GET requests. It includes methods to generate a cache key, retrieve cached response, cache a response, and process the request to generate the response.

The JCS cache instance is created using the cache region name (myCacheRegion). The getCachedResponse method retrieves the cached response using the cache key, while cacheResponse caches the response for future use. The processRequest method is responsible for processing the request and generating the response.

import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

public class MyCachingServlet extends SlingSafeMethodsServlet {

    private static final String CACHE_REGION = "myCacheRegion";
    private JCS cache;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String cacheKey = generateCacheKey(request);

        // Check if the response is already cached
        String cachedResponse = getCachedResponse(cacheKey);
        if (cachedResponse != null) {
            response.getWriter().print(cachedResponse);
            return;
        }

        // Process the request and generate the response
        String responseData = processRequest(request);

        // Cache the response for future use
        cacheResponse(cacheKey, responseData);

        response.getWriter().print(responseData);
    }

    private String generateCacheKey(SlingHttpServletRequest request) {
        // Generate a unique cache key based on request parameters, path, etc.
        // Example: String cacheKey = request.getPathInfo() + "_" + request.getParameter("param");
        // Return the generated cache key
    }

    private String getCachedResponse(String cacheKey) {
        try {
            if (cache == null) {
                cache = JCS.getInstance(CACHE_REGION);
            }
            return (String) cache.get(cacheKey);
        } catch (CacheException e) {
            // Handle cache exception
        }
        return null;
    }

    private void cacheResponse(String cacheKey, String responseData) {
        try {
            if (cache == null) {
                cache = JCS.getInstance(CACHE_REGION);
            }
            cache.put(cacheKey, responseData);
        } catch (CacheException e) {
            // Handle cache exception
        }
    }

    private String processRequest(SlingHttpServletRequest request) {
        // Process the request and generate the response data
        // Return the response data
    }
}