JCS Caching | Community
Skip to main content
October 16, 2015
Solved

JCS Caching

  • October 16, 2015
  • 1 reply
  • 657 views

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

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 BrianKasingli

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; @9944223 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 } }

1 reply

BrianKasingli
BrianKasingliAccepted solution
New Participant
July 17, 2023

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; @9944223 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 } }