Improved HtmlCache class.

This commit is contained in:
Christian P. MOMON 2024-08-18 22:37:29 +02:00
parent 5e807b5200
commit 08e6071876

View file

@ -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<String, String>
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<String, String>
//
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;
}
}