From 08e607187673c602cb444a6fc0d57c705aeb3729 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 18 Aug 2024 22:37:29 +0200 Subject: [PATCH] Improved HtmlCache class. --- src/fr/devinsy/kiss4web/HtmlCache.java | 134 ++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 2 deletions(-) diff --git a/src/fr/devinsy/kiss4web/HtmlCache.java b/src/fr/devinsy/kiss4web/HtmlCache.java index 20151e2..bc14694 100644 --- a/src/fr/devinsy/kiss4web/HtmlCache.java +++ b/src/fr/devinsy/kiss4web/HtmlCache.java @@ -23,6 +23,9 @@ import java.util.HashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fr.devinsy.strings.StringList; +import jakarta.servlet.http.HttpServletRequest; + /** * The Class HtmlCache. */ @@ -39,11 +42,107 @@ public class HtmlCache extends HashMap super(); } + /** + * Builds the key. + * + * @param object + * the object + * @param strings + * the strings + * @return the string + */ + public String buildKey(final Object object, final String... strings) + { + String result; + + if (object == null) + { + result = null; + } + else + { + result = buildKey(object.getClass().getCanonicalName(), strings); + } + + // + return result; + } + + /** + * Builds the key. + * + * @param main + * the main + * @param strings + * the strings + * @return the string + */ + public String buildKey(final String main, final String... strings) + { + String result; + + if (main == null) + { + result = null; + } + else + { + int capacity = strings.length * 2 + 1; + StringList buffer = new StringList(capacity); + + buffer.add(main); + for (int index = 0; index < strings.length; index++) + { + buffer.append('#'); + buffer.append(index); + buffer.append(strings[index]); + } + result = buffer.toString(); + } + + // + return result; + } + + /** + * Gets the HTML code using a GET request. + * + * @param request + * the request + * @return the string + */ + public String get(final HttpServletRequest request) + { + String result; + + if (request == null) + { + result = null; + } + else + { + String key = request.getPathInfo(); + if (request.getQueryString() != null) + { + key = key + "?" + request.getQueryString(); + } + result = super.get(key); + + if (result != null) + { + logger.info("HTML CACHE MATCHED: [{}]", key); + } + } + + // + return result; + } + /** * Gets the. * - * @param get - * the get + * @param key + * the key * @return the string */ public String get(final String key) @@ -67,4 +166,35 @@ public class HtmlCache extends HashMap // return result; } + + /** + * Put HTML code using a GET request. + * + * @param html + * the html + * @param request + * the request + * @return the string + */ + public String put(final HttpServletRequest request, final String html) + { + String result; + + if ((request == null) || (html == null)) + { + result = null; + } + else + { + String key = request.getPathInfo(); + if (request.getQueryString() != null) + { + key = key + "?" + request.getQueryString(); + } + result = super.put(key, html); + } + + // + return result; + } }