@POST @Path("action") public String action( @FormParam("element_1") double n1, @FormParam("element_2") double n2, @FormParam("element_3") String s) { try { MemcachedClient client = MccFactory.getConst("localhost").getMemcachedClient(); JsonObject innerObject = new JsonObject(); innerObject.addProperty("key", s); innerObject.addProperty("firstNum", n1); innerObject.addProperty("secondNum", n2); client.add(s, 30000, innerObject.toString()); String keys = (String) client.get("mykeys"); keys = (keys == null) ? "" : keys; keys += s + "/"; client.replace("mykeys", 30000, keys); } catch (Exception e) { e.printStackTrace(); return getStringStackTrace(e); } return "String: " + s + ". First number = " + n1 + ", Second number = " + n2; }
@GET @Path("get") public String get(@QueryParam("key") String key) { try { MemcachedClient client = MccFactory.getConst("localhost").getMemcachedClient(); String res = (String) client.get(key); return (res != null) ? res : "No data found for " + key; } catch (Exception e) { e.printStackTrace(); return getStringStackTrace(e); } }
@DELETE @Path("delete") public String delete(@QueryParam("key") String key) { try { MemcachedClient client = MccFactory.getConst("localhost").getMemcachedClient(); String keys = (String) client.get("mykeys"); keys = (keys == null) ? "" : keys; keys = keys.replace(key + "/", ""); client.replace("mykeys", 30000, keys); client.delete(key); } catch (Exception e) { e.printStackTrace(); return getStringStackTrace(e); } return "Removed value at " + key; }
@GET @Path("list") public String list() { try { String result = ""; MemcachedClient client = MccFactory.getConst("localhost").getMemcachedClient(); String s = (String) client.get("mykeys"); String[] keys = s.split("/"); for (String key : keys) { if (key == null || key.length() < 1) continue; String ss = (String) client.get(key); if (ss != null) result += key + " " + ss + "<br/>"; } return (result.length() > 0) ? result : "No data found"; } catch (Exception e) { e.printStackTrace(); return getStringStackTrace(e); } }