<%@ page import="javax.cache.CacheManager" %>
<%@ page import="javax.cache.Caching" %>
<%@ page import="javax.cache.Cache" %>

<h2>WSO2 Carbon Caching Demo</h2>

<hr/>
<p>

<h3>Add to Cache</h3>

<form action="index.jsp" method="POST">
    <table border="0">
        <tr>
            <td>Key</td>
            <td><input type="text" name="key"/></td>
        </tr>
        <tr>
            <td>Value</td>
            <td><input type="text" name="value"/></td>
        </tr>
        <tr>
            <td> </td>
            <td><input type="submit" value="Add" name="add"></td>
        </tr>
    </table>
</form>
</p>
<hr/>
<p>

<h3>Read from Cache</h3>

<form action="index.jsp" method="POST">
    <table border="0">
        <tr>
            <td>Key</td>
            <td><input type="text" name="key"/></td>
        </tr>
        <tr>
            <td> </td>
            <td><input type="submit" value="View" name="view"></td>
        </tr>
    </table>
</form>
</p>
<hr/>

<%
    // The javax.cache.CacheManager instance used to obtain the cache
    CacheManager cacheManager =   Caching.getCacheManagerFactory().getCacheManager("tsampleCacheManager");
    Cache cache = cacheManager.getCache("sampleCache");

    if (request.getParameter("add") != null) {
        String key = request.getParameter("key");
        String value = request.getParameter("value");
        cache.put(key, value);
%>
<p>
    Added entry: <%= key %>
</p>
<%
    } else if (request.getParameter("view") != null) {
        String key = request.getParameter("key");
        if (cache.get(key) != null) {
            String content = (String) cache.get(key);
%>
            <p>
                Value of entry <%= key%> : <%= content %>
            </p>
<%
        } else {
%>
            <p>
                Unable to find an entry by the given key <%= key%>!
            </p>
<%
        }
    }
%>