Persisting cache in PersistentLocalDb

This commit is contained in:
Kai S. K. Engelbart 2020-02-06 21:42:17 +01:00
parent 3f7544cea3
commit fe6cc9f464
4 changed files with 23 additions and 11 deletions

View File

@ -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<T> implements Consumer<T> {
public class Cache<T> implements Consumer<T>, Serializable {
private final Queue<T> elements = new LinkedList<>();
private transient Consumer<T> processor;
private static final Logger logger = EnvoyLog.getLogger(Cache.class.getSimpleName());
private static final long serialVersionUID = 7343544675545545076L;
/**
* Adds an element to the cache.

View File

@ -24,8 +24,8 @@ public abstract class LocalDb {
protected Map<String, User> users = new HashMap<>();
protected List<Chat> chats = new ArrayList<>();
protected IdGenerator idGenerator;
protected Cache<Message> messageCache;
protected Cache<MessageStatusChangeEvent> statusCache;
protected Cache<Message> messageCache = new Cache<>();
protected Cache<MessageStatusChangeEvent> 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.

View File

@ -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() {

View File

@ -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) {