Add Redis persistence.
This commit is contained in:
parent
74b370f19b
commit
a74f848c5a
1 changed files with 20 additions and 6 deletions
26
source/app.d
26
source/app.d
|
|
@ -18,18 +18,25 @@ Then get the apt key, and install the things:
|
|||
import vibe.d;
|
||||
|
||||
final class Room {
|
||||
string[] messages;
|
||||
RedisDatabase db;
|
||||
string room;
|
||||
RedisList!string messages;
|
||||
ManualEvent messageEvent;
|
||||
|
||||
this() {
|
||||
this(RedisDatabase db, string room) {
|
||||
this.db = db;
|
||||
this.room = room;
|
||||
this.messages = db.getAsList!string("webchat_" ~ room);
|
||||
messageEvent = createManualEvent();
|
||||
}
|
||||
|
||||
void addMessage(string nick, string message) {
|
||||
messages ~= nick ~ ": " ~ message;
|
||||
messages.insertBack(nick ~ ": " ~ message);
|
||||
messageEvent.emit();
|
||||
//messages ~= nick ~ ": " ~ message;
|
||||
}
|
||||
|
||||
void waitForMessage(size_t next_message) {
|
||||
void waitForMessage(long next_message) {
|
||||
while (messages.length <= next_message) {
|
||||
messageEvent.wait();
|
||||
}
|
||||
|
|
@ -37,7 +44,14 @@ final class Room {
|
|||
}
|
||||
|
||||
final class WebChat {
|
||||
private Room[string] rooms;
|
||||
private {
|
||||
Room[string] rooms;
|
||||
RedisDatabase db;
|
||||
}
|
||||
|
||||
this() {
|
||||
db = connectRedis("127.0.0.1").getDatabase(0);
|
||||
}
|
||||
|
||||
// Uses vibe.d magic method names - in this case, GET /
|
||||
void get() {
|
||||
|
|
@ -87,7 +101,7 @@ final class WebChat {
|
|||
|
||||
private Room getOrCreateRoom(string room) {
|
||||
if (auto pr = room in rooms) return *pr;
|
||||
return rooms[room] = new Room;
|
||||
return rooms[room] = new Room(db, room);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue