Skip to content

Instantly share code, notes, and snippets.

@rikkimax
Created December 6, 2013 14:29
Show Gist options
  • Save rikkimax/7825614 to your computer and use it in GitHub Desktop.
Save rikkimax/7825614 to your computer and use it in GitHub Desktop.
module common.session.mongo;
import vibe.http.session;
import vibe.db.mongo.mongo;
class MongoSessionStore : SessionStore {
protected {
MongoClient client;
MongoCollection collection;
}
this(MongoClient client, string database, string table) {
this.client = client;
collection = client.getCollection(database ~ "." ~ table);
}
this(string ip, ushort port, string database, string table) {
client = connectMongoDB(ip, port);
collection = client.getCollection(database ~ "." ~ table);
}
this(string ip, string database, string table) {
client = connectMongoDB(ip);
collection = client.getCollection(database ~ "." ~ table);
}
@property MongoClient mongoClient() {
return client;
}
Session create() {
return open(BsonObjectID.generate().toString());
}
/// Opens an existing session.
Session open(string id) {
if (id != "") {
auto val = collection.count(Bson(["_id" : Bson(id)]));
if (val == 0) {
collection.insert(Bson(["_id" : Bson(id)]));
}
return createSessionInstance(id);
} else {
return null;
}
}
/// Sets a name/value pair for a given session.
void set(string id, string name, string value) {
name = escapeValues(name);
Bson valueb = getBson(id);
valueb[name] = value;
collection.update(["_id" : id], valueb);
}
/// Returns the value for a given session key.
string get(string id, string name, string defaultVal = null) const {
MongoSessionStore mss = cast(MongoSessionStore)this;
name = escapeValues(name);
Bson val = mss.getBson(id)[name];
if (val.type == Bson.Type.null_) {
return defaultVal;
}
return cast(string)val;
}
private Bson getBson(string id) {
return collection.findOne(Bson(["_id" : Bson(id)]));
}
/// Determines if a certain session key is set.
bool isKeySet(string id, string key) const {
MongoSessionStore mss = cast(MongoSessionStore)this;
key = escapeValues(key);
return mss.getBson(id)[key].type != Bson.Type.null_;
}
/// Terminates the given sessiom.
void destroy(string id) {
collection.remove(Bson(["_id" : Bson(id)]));
}
/// Iterates all key/value pairs stored in the given session.
int delegate(int delegate(ref string key, ref string value)) iterateSession(string id) {
assert(collection.count(Bson(["_id" : Bson(id)])) != 0, "session not in store");
int iterator(int delegate(ref string key, ref string value) del) {
Bson val = getBson(id);
foreach(ref string key, ref Bson value; val) {
string k = unescapeValues(key), v = cast(string)value;
if(auto ret = del(k, v) != 0)
return ret;
}
return 0;
}
return &iterator;
}
}
private {
string escapeValues(string text) {
if (text.length > 0)
if(text[0] == '$')
text = "\\" ~ text;
return text;
}
string unescapeValues(string text) {
if (text.length > 1)
if(text[0 .. 1] == "\\$")
text = text[2 .. $];
return text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment