diff --git a/src/main/java/envoy/client/data/Cache.java b/src/main/java/envoy/client/data/Cache.java index 458cb0a..80ef26f 100644 --- a/src/main/java/envoy/client/data/Cache.java +++ b/src/main/java/envoy/client/data/Cache.java @@ -1,5 +1,6 @@ package envoy.client.data; +import java.io.Serializable; import java.util.LinkedList; import java.util.Queue; import java.util.function.Consumer; @@ -18,12 +19,13 @@ import envoy.client.util.EnvoyLog; * @author Kai S. K. Engelbart * @since Envoy v0.3-alpha */ -public class Cache implements Consumer { +public class Cache implements Consumer, Serializable { private final Queue elements = new LinkedList<>(); private transient Consumer processor; private static final Logger logger = EnvoyLog.getLogger(Cache.class.getSimpleName()); + private static final long serialVersionUID = 7343544675545545076L; /** * Adds an element to the cache. diff --git a/src/main/java/envoy/client/data/LocalDb.java b/src/main/java/envoy/client/data/LocalDb.java index a606856..8034e2a 100644 --- a/src/main/java/envoy/client/data/LocalDb.java +++ b/src/main/java/envoy/client/data/LocalDb.java @@ -24,8 +24,8 @@ public abstract class LocalDb { protected Map users = new HashMap<>(); protected List chats = new ArrayList<>(); protected IdGenerator idGenerator; - protected Cache messageCache; - protected Cache statusCache; + protected Cache messageCache = new Cache<>(); + protected Cache statusCache = new Cache<>(); /** * Initializes a storage space for a user-specific list of chats. @@ -52,12 +52,12 @@ public abstract class LocalDb { public void loadUsers() throws Exception {} /** - * Loads all chat data of the client user. + * Loads all data of the client user. * * @throws Exception if the loading process failed * @since Envoy v0.3-alpha */ - public void loadChats() throws Exception {} + public void loadUserData() throws Exception {} /** * Loads the ID generator. Any exception thrown during this process is ignored. diff --git a/src/main/java/envoy/client/data/PersistentLocalDb.java b/src/main/java/envoy/client/data/PersistentLocalDb.java index 7052c60..d6e594a 100644 --- a/src/main/java/envoy/client/data/PersistentLocalDb.java +++ b/src/main/java/envoy/client/data/PersistentLocalDb.java @@ -23,7 +23,7 @@ import envoy.util.SerializationUtils; */ public class PersistentLocalDb extends LocalDb { - private File localDBDir, localDBFile, usersFile, idGeneratorFile; + private File localDBDir, localDBFile, usersFile, idGeneratorFile, messageCacheFile, statusCacheFile; /** * Initializes an empty local database without a directory. All changes made to @@ -63,7 +63,9 @@ public class PersistentLocalDb extends LocalDb { @Override public void initializeUserStorage() { if (user == null) throw new NullPointerException("Client user is null"); - localDBFile = new File(localDBDir, user.getId() + ".db"); + localDBFile = new File(localDBDir, user.getId() + ".db"); + messageCacheFile = new File(localDBDir, user.getId() + "_message_cache.db"); + statusCacheFile = new File(localDBDir, user.getId() + "_status_cache.db"); } @Override @@ -71,8 +73,12 @@ public class PersistentLocalDb extends LocalDb { // Save users SerializationUtils.write(usersFile, users); - // Save chats - if (user != null) SerializationUtils.write(localDBFile, chats); + // Save user data + if (user != null) { + SerializationUtils.write(localDBFile, chats); + SerializationUtils.write(messageCacheFile, messageCache); + SerializationUtils.write(statusCacheFile, statusCache); + } // Save id generator if (hasIdGenerator()) SerializationUtils.write(idGeneratorFile, idGenerator); @@ -82,7 +88,11 @@ public class PersistentLocalDb extends LocalDb { public void loadUsers() throws ClassNotFoundException, IOException { users = SerializationUtils.read(usersFile, HashMap.class); } @Override - public void loadChats() throws ClassNotFoundException, IOException { chats = SerializationUtils.read(localDBFile, ArrayList.class); } + public void loadUserData() throws ClassNotFoundException, IOException { + chats = SerializationUtils.read(localDBFile, ArrayList.class); + messageCache = SerializationUtils.read(messageCacheFile, Cache.class); + statusCache = SerializationUtils.read(statusCacheFile, Cache.class); + } @Override public void loadIdGenerator() { diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 1c68e41..46f7951 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -138,7 +138,7 @@ public class Startup { // Initialize chats in local database try { localDb.initializeUserStorage(); - localDb.loadChats(); + localDb.loadUserData(); } catch (FileNotFoundException e) { // The local database file has not yet been created, probably first login } catch (Exception e) {