multi-layer caching into memory, refactor db.js (bug 1012790)