X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FDomain.java;fp=src%2Forg%2Fcacert%2Fgigi%2FDomain.java;h=5dbbdaf0203d52036a0b504c1c196c2304e94e82;hb=231b0e94c013f955d3ab3b6c2ecab6908b2b9ca8;hp=21fe668edda1d638beb90287e769758c8b1f860e;hpb=4df25979d16944ddfcc58752cf8b6f7da30c4bc1;p=gigi.git diff --git a/src/org/cacert/gigi/Domain.java b/src/org/cacert/gigi/Domain.java index 21fe668e..5dbbdaf0 100644 --- a/src/org/cacert/gigi/Domain.java +++ b/src/org/cacert/gigi/Domain.java @@ -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 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; + } + }