Skip to content
Snippets Groups Projects
Commit cb205ec3 authored by rswindell's avatar rswindell
Browse files

Implement an (in-memory) Avatar cache - reducing the number of disk accesses

when using features that support avatars (e.g. reading msgs, listing files,
listing BBSes in the BBS list).
When used in the terminal server, the cache is located in bbs.mods.avatar_cache
otherwise it's located in the scope of the load()'d library. Applications must
use the lib's read() method to take advantage of the cache. The other lower
level functions (e.g. read_localuser, read_netuser) by-pass the cache on read
but do update the cache with the result. So, generally, avatars should be only
loaded from disk one time during a session/logon.
parent 2b8e72e0
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,31 @@ const FTN_4D_PATTERN = /^(\d+):(\d+)\/(\d+)\.(\d+)$/; ...@@ -13,6 +13,31 @@ const FTN_4D_PATTERN = /^(\d+):(\d+)\/(\d+)\.(\d+)$/;
const size = defs.width * defs.height * 2; // 2 bytes per cell for char and attributes const size = defs.width * defs.height * 2; // 2 bytes per cell for char and attributes
var cache = {};
if(js.global.bbs) {
if(!bbs.mods.avatar_cache)
bbs.mods.avatar_cache = {};
cache = bbs.mods.avatar_cache;
}
function cache_key(username, netaddr)
{
var key = username;
if(netaddr)
key += ( '@' + netaddr );
return key;
}
function cache_set(username, obj, netaddr)
{
cache[cache_key(username, netaddr)] = obj;
}
function cache_get(username, netaddr)
{
return cache[cache_key(username, netaddr)];
}
function local_library() function local_library()
{ {
return format("%savatars/", system.text_dir); return format("%savatars/", system.text_dir);
...@@ -94,6 +119,7 @@ function write_localuser(usernum, obj) ...@@ -94,6 +119,7 @@ function write_localuser(usernum, obj)
} }
var result = file.iniSetObject("avatar", obj); var result = file.iniSetObject("avatar", obj);
file.close(); file.close();
cache_set(usernum, obj);
return result; return result;
} }
...@@ -111,6 +137,7 @@ function enable_localuser(usernum, enabled) ...@@ -111,6 +137,7 @@ function enable_localuser(usernum, enabled)
obj.disabled = !enabled; obj.disabled = !enabled;
obj.updated = new Date(); obj.updated = new Date();
result = file.iniSetObject("avatar", obj); result = file.iniSetObject("avatar", obj);
cache_set(usernum, obj);
} }
file.close(); file.close();
return result; return result;
...@@ -124,6 +151,7 @@ function remove_localuser(usernum) ...@@ -124,6 +151,7 @@ function remove_localuser(usernum)
} }
var result = file.iniRemoveSection("avatar"); var result = file.iniRemoveSection("avatar");
file.close(); file.close();
cache_set(usernum, undefined);
return result; return result;
} }
...@@ -135,6 +163,7 @@ function write_netuser(username, netaddr, obj) ...@@ -135,6 +163,7 @@ function write_netuser(username, netaddr, obj)
} }
var result = file.iniSetObject(username, obj); var result = file.iniSetObject(username, obj);
file.close(); file.close();
cache_set(username, obj, netaddr);
return result; return result;
} }
...@@ -155,7 +184,7 @@ function read_localuser(usernum) ...@@ -155,7 +184,7 @@ function read_localuser(usernum)
function read_netuser(username, netaddr) function read_netuser(username, netaddr)
{ {
var filename = this.netuser_fname(netaddr); var filename = this.netuser_fname(netaddr);
if(!filename) if(!filename && netaddr.indexOf('@') == -1)
filename = this.bbsuser_fname(netaddr); filename = this.bbsuser_fname(netaddr);
if(!filename) if(!filename)
return false; return false;
...@@ -170,12 +199,18 @@ function read_netuser(username, netaddr) ...@@ -170,12 +199,18 @@ function read_netuser(username, netaddr)
function read(usernum, username, netaddr) function read(usernum, username, netaddr)
{ {
if(parseInt(usernum, 10) >= 1) var usernum = parseInt(usernum, 10);
return read_localuser(usernum); var obj = cache_get(usernum >= 1 ? usernum : username, netaddr);
if(obj !== undefined) // null and false are also valid cached avatar values
return obj;
if(usernum >= 1)
obj = read_localuser(usernum);
else if(!netaddr) else if(!netaddr)
return read_localuser(system.matchuser(username)); obj = read_localuser(system.matchuser(username));
else else
return read_netuser(username, netaddr); obj = read_netuser(username, netaddr);
cache_set(usernum >= 1 ? usernum : username, obj, netaddr);
return obj;
} }
function update_localuser(usernum, data) function update_localuser(usernum, data)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment