Can this Java cache be safer or faster?
public class Cache {
private long lastupdateTime = 0L;
private String cachedData = null;
public String getData() {
long now = System.currentTimeMillis();
if (now - lastupdateTime < 30000) {
return cachedData;
}
else {
synchronized (this) {
if (now - lastupdateTime < 30000) {
return cachedData;
}
else {
lastupdateTime = now;
cachedData = getDataFromDatabase();
return cachedData;
}
}
}
}
private String getDataFromDatabase() { ... }
}