]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/User.java
Test+implement: object cache for email and domain.
[gigi.git] / src / org / cacert / gigi / User.java
index 842f2f20e11a071d2943aed124772d8119a95547..41396ae102e53ea121ad67364d764f78f3af6469 100644 (file)
@@ -13,7 +13,7 @@ import org.cacert.gigi.util.Notary;
 import org.cacert.gigi.util.PasswordHash;
 import org.cacert.gigi.util.PasswordStrengthChecker;
 
-public class User {
+public class User implements IdCachable {
 
     private int id;
 
@@ -129,8 +129,11 @@ public class User {
         query.setString(6, name.suffix);
         query.setDate(7, new java.sql.Date(dob.getTime()));
         query.setString(8, locale.toString());
-        query.execute();
-        id = DatabaseConnection.lastInsertId(query);
+        synchronized (User.class) {
+            query.execute();
+            id = DatabaseConnection.lastInsertId(query);
+            myCache.put(this);
+        }
     }
 
     public void changePassword(String oldPass, String newPass) throws GigiApiException {
@@ -253,10 +256,6 @@ public class User {
         return points;
     }
 
-    public static User getById(int id) {
-        return new User(id);
-    }
-
     public EmailAddress[] getEmails() {
         try {
             PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
@@ -495,4 +494,15 @@ public class User {
         update.executeUpdate();
     }
 
+    private static ObjectCache<User> myCache = new ObjectCache<>();
+
+    public static User getById(int id) {
+        User u = myCache.get(id);
+        if (u == null) {
+            synchronized (User.class) {
+                myCache.put(u = new User(id));
+            }
+        }
+        return u;
+    }
 }