mod_storage_memory/mod_storage_memory.lua
author Kim Alvefur <zash@zash.se>
Tue, 31 Dec 2013 02:17:50 +0100
changeset 1259 fa7e402fcdc1
child 1593 3e4d15ae2133
child 1611 8b997d9cf09e
permissions -rw-r--r--
mod_storage_memory: Simple in-memory only storage backend without persistence
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1259
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
local memory = setmetatable({}, {
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
	__index = function(t, k)
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
		local store = module:shared(k)
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
		t[k] = store;
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
		return store;
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
	end
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
});
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
local keyval_store = {};
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
keyval_store.__index = keyval_store;
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
function keyval_store:get(username)
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
	return self.store[username];
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
end
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
function keyval_store:set(username, data)
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
	self.store[username] = data;
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
	return true;
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
end
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
local stores = {
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
	keyval = keyval_store;
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
}
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
local driver = {};
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
function driver:open(store, typ)
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
	local store_mt = stores[typ or "keyval"];
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
	if store_mt then
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
		return setmetatable({ store = memory[store] }, store_mt);
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
	end
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
	return nil, "unsupported-store";
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
end
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
fa7e402fcdc1 mod_storage_memory: Simple in-memory only storage backend without persistence
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
module:provides("storage", driver);