]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/Domain.java
Test+implement: object cache for email and domain.
[gigi.git] / src / org / cacert / gigi / Domain.java
index 21fe668edda1d638beb90287e769758c8b1f860e..5dbbdaf0203d52036a0b504c1c196c2304e94e82 100644 (file)
@@ -3,9 +3,10 @@ package org.cacert.gigi;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+
 import org.cacert.gigi.database.DatabaseConnection;
 
-public class Domain {
+public class Domain implements IdCachable {
 
     private User owner;
 
@@ -62,6 +63,7 @@ public class Domain {
                 ps.setString(2, suffix);
                 ps.execute();
                 id = DatabaseConnection.lastInsertId(ps);
+                myCache.put(this);
             } catch (SQLException e) {
                 throw new GigiApiException(e);
             }
@@ -93,16 +95,6 @@ public class Domain {
         return suffix;
     }
 
-    public static Domain getById(int id) throws IllegalArgumentException {
-        // TODO cache
-        try {
-            Domain e = new Domain(id);
-            return e;
-        } catch (SQLException e) {
-            throw new IllegalArgumentException(e);
-        }
-    }
-
     public void addPing(String type, String config) throws GigiApiException {
         try {
             PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO pingconfig SET domainid=?, type=?, info=?");
@@ -157,4 +149,21 @@ public class Domain {
         }
 
     }
+
+    private static ObjectCache<Domain> myCache = new ObjectCache<>();
+
+    public static Domain getById(int id) throws IllegalArgumentException {
+        Domain em = myCache.get(id);
+        if (em == null) {
+            try {
+                synchronized (Domain.class) {
+                    myCache.put(em = new Domain(id));
+                }
+            } catch (SQLException e1) {
+                throw new IllegalArgumentException(e1);
+            }
+        }
+        return em;
+    }
+
 }