|
@@ -1,14 +1,17 @@
|
|
package hello;
|
|
package hello;
|
|
|
|
|
|
-import java.io.*;
|
|
|
|
-import java.sql.*;
|
|
|
|
-import java.util.*;
|
|
|
|
-import java.util.concurrent.*;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.sql.SQLException;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
-import javax.annotation.*;
|
|
|
|
-import javax.servlet.*;
|
|
|
|
-import javax.servlet.http.*;
|
|
|
|
-import javax.sql.*;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import javax.servlet.ServletConfig;
|
|
|
|
+import javax.servlet.ServletException;
|
|
|
|
+import javax.servlet.http.HttpServlet;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import javax.sql.DataSource;
|
|
|
|
|
|
import org.cache2k.Cache;
|
|
import org.cache2k.Cache;
|
|
import org.cache2k.Cache2kBuilder;
|
|
import org.cache2k.Cache2kBuilder;
|
|
@@ -20,7 +23,7 @@ import org.cache2k.Cache2kBuilder;
|
|
public class Cache2kPostgresServlet extends HttpServlet {
|
|
public class Cache2kPostgresServlet extends HttpServlet {
|
|
// Database details.
|
|
// Database details.
|
|
private static final int DB_ROWS = 10000;
|
|
private static final int DB_ROWS = 10000;
|
|
-
|
|
|
|
|
|
+
|
|
// Database connection pool.
|
|
// Database connection pool.
|
|
@Resource(name = "jdbc/hello_world")
|
|
@Resource(name = "jdbc/hello_world")
|
|
private DataSource dataSource;
|
|
private DataSource dataSource;
|
|
@@ -36,13 +39,10 @@ public class Cache2kPostgresServlet extends HttpServlet {
|
|
} catch (SQLException e) {
|
|
} catch (SQLException e) {
|
|
throw new ServletException(e);
|
|
throw new ServletException(e);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
// Build the cache
|
|
// Build the cache
|
|
- cache = new Cache2kBuilder<Integer, CachedWorld>() {}
|
|
|
|
- .name("cachedWorld")
|
|
|
|
- .eternal(true)
|
|
|
|
- .entryCapacity(DB_ROWS)
|
|
|
|
- .build();
|
|
|
|
|
|
+ cache = new Cache2kBuilder<Integer, CachedWorld>() {
|
|
|
|
+ }.name("cachedWorld").eternal(true).entryCapacity(DB_ROWS).build();
|
|
cache.putAll(worlds);
|
|
cache.putAll(worlds);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -50,18 +50,16 @@ public class Cache2kPostgresServlet extends HttpServlet {
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
|
|
IOException {
|
|
IOException {
|
|
final int count = Common.normalise(req.getParameter("queries"));
|
|
final int count = Common.normalise(req.getParameter("queries"));
|
|
- final Random random = ThreadLocalRandom.current();
|
|
|
|
|
|
+ final CachedWorld[] worlds = new CachedWorld[count];
|
|
|
|
|
|
- //TODO prevent duplicate numbers to be added
|
|
|
|
- List<Integer> keys = new ArrayList<Integer>(count);
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
for (int i = 0; i < count; i++) {
|
|
- keys.add(new Integer(random.nextInt(DB_ROWS) + 1));
|
|
|
|
|
|
+ worlds[i] = cache.get(ThreadLocalRandom.current().nextInt(DB_ROWS) + 1);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
// Set content type to JSON
|
|
// Set content type to JSON
|
|
res.setHeader(Common.HEADER_CONTENT_TYPE, Common.CONTENT_TYPE_JSON);
|
|
res.setHeader(Common.HEADER_CONTENT_TYPE, Common.CONTENT_TYPE_JSON);
|
|
|
|
|
|
// Write JSON encoded message to the response.
|
|
// Write JSON encoded message to the response.
|
|
- Common.MAPPER.writeValue(res.getOutputStream(), cache.getAll(keys).values());
|
|
|
|
|
|
+ Common.MAPPER.writeValue(res.getOutputStream(), worlds);
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|