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;
ps.setString(2, suffix);
ps.execute();
id = DatabaseConnection.lastInsertId(ps);
+ myCache.put(this);
} catch (SQLException e) {
throw new GigiApiException(e);
}
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=?");
}
}
+
+ 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;
+ }
+
}
import org.cacert.gigi.localisation.Language;
import org.cacert.gigi.util.RandomToken;
-public class EmailAddress {
+public class EmailAddress implements IdCachable {
private String address;
ps.setInt(1, owner.getId());
ps.setString(2, hash);
ps.setString(3, address);
- ps.execute();
- id = DatabaseConnection.lastInsertId(ps);
+ synchronized (EmailAddress.class) {
+ ps.execute();
+ id = DatabaseConnection.lastInsertId(ps);
+ myCache.put(this);
+ }
MailProbe.sendMailProbe(l, "email", id, hash, address);
} catch (SQLException e) {
e.printStackTrace();
}
}
- public static EmailAddress getById(int id) throws IllegalArgumentException {
- // TODO cache
- try {
- EmailAddress e = new EmailAddress(id);
- return e;
- } catch (SQLException e) {
- throw new IllegalArgumentException(e);
- }
- }
-
public boolean isVerified() {
return hash.isEmpty();
}
+
+ private static ObjectCache<EmailAddress> myCache = new ObjectCache<>();
+
+ public static EmailAddress getById(int id) throws IllegalArgumentException {
+ EmailAddress em = myCache.get(id);
+ if (em == null) {
+ try {
+ synchronized (EmailAddress.class) {
+ myCache.put(em = new EmailAddress(id));
+ }
+ } catch (SQLException e1) {
+ throw new IllegalArgumentException(e1);
+ }
+ }
+ return em;
+ }
}
--- /dev/null
+package org.cacert.gigi;
+
+public interface IdCachable {
+
+ public int getId();
+}
--- /dev/null
+package org.cacert.gigi;
+
+import java.lang.ref.WeakReference;
+import java.util.HashMap;
+
+public class ObjectCache<T extends IdCachable> {
+
+ HashMap<Integer, WeakReference<T>> hashmap = new HashMap<>();
+
+ public void put(T c) {
+ hashmap.put(c.getId(), new WeakReference<T>(c));
+ }
+
+ public T get(int id) {
+ WeakReference<T> res = hashmap.get(id);
+ if (res != null) {
+ return res.get();
+ }
+ return null;
+ }
+}
import org.cacert.gigi.util.PasswordHash;
import org.cacert.gigi.util.PasswordStrengthChecker;
-public class User {
+public class User implements IdCachable {
private int id;
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 {
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");
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;
+ }
}
--- /dev/null
+package org.cacert.gigi;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.sql.Date;
+import java.sql.SQLException;
+import java.util.Calendar;
+import java.util.Locale;
+
+import org.cacert.gigi.localisation.Language;
+import org.cacert.gigi.testUtils.ManagedTest;
+import org.junit.Test;
+
+public class TestObjectCache extends ManagedTest {
+
+ int uid = createVerifiedUser("fname", "lname", createUniqueName() + "@example.com", TEST_PASSWORD);
+
+ @Test
+ public void testUserCache() throws SQLException {
+ assertThat(User.getById(uid), is(sameInstance(User.getById(uid))));
+
+ User u = new User();
+ u.setFname("fname");
+ u.setMname("mname");
+ u.setSuffix("suffix");
+ u.setLname("lname");
+ u.setEmail(createUniqueName() + "@example.org");
+ Calendar c = Calendar.getInstance();
+ c.set(1950, 1, 1);
+ u.setDob(new Date(c.getTime().getTime()));
+ u.setPreferredLocale(Locale.ENGLISH);
+ u.insert(TEST_PASSWORD);
+
+ assertThat(u, is(sameInstance(User.getById(u.getId()))));
+ assertThat(User.getById(u.getId()), is(sameInstance(User.getById(u.getId()))));
+
+ }
+
+ @Test
+ public void testDomainCache() throws GigiApiException {
+ Domain d = new Domain(User.getById(uid), "example.org");
+ d.insert();
+
+ assertThat(d, is(sameInstance(Domain.getById(d.getId()))));
+ assertThat(Domain.getById(d.getId()), is(sameInstance(Domain.getById(d.getId()))));
+ }
+
+ @Test
+ public void testEmailCache() {
+ EmailAddress em = new EmailAddress(createUniqueName() + "@example.org", User.getById(uid));
+ em.insert(Language.getInstance(Locale.ENGLISH));
+
+ assertThat(em, is(sameInstance(EmailAddress.getById(em.getId()))));
+ assertThat(EmailAddress.getById(em.getId()), is(sameInstance(EmailAddress.getById(em.getId()))));
+ }
+}