Skip to content

Commit

Permalink
Swap lifetime cache out for persistent cache
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Oct 31, 2024
1 parent 4da0ff8 commit f37372f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
52 changes: 45 additions & 7 deletions src/cache.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import NodeCache from "node-cache";
const cache = new NodeCache({ stdTTL: 60 * 60 });
// NOTE: This cache is for values that can be safely cached for the entirety of
// the application's lifecycle, e.g., the karma count
export const lifetimeCache = new NodeCache();
export default cache;

import { join } from "path";
Expand All @@ -19,7 +16,50 @@ const dbPath = join(process.env.CACHE_DIR, "database.db");
const db = new Database(dbPath);
db.pragma("journal_mode = WAL");

function initialize(messages) {
export const lifetimeCache = {
set: (key, value) => {
const insert = db.prepare(
`INSERT OR REPLACE INTO ltCache (key, value) VALUES (?, ?)`,
);
insert.run(key, JSON.stringify(value));
},

get: (key) => {
const query = db.prepare(`SELECT value FROM ltCache WHERE key = ?`);
const row = query.get(key);
return row && row.value ? JSON.parse(row.value) : null;
},

has: (key) => {
const query = db.prepare(`SELECT 1 FROM ltCache WHERE key = ?`);
const row = query.get(key);
return !!row;
},
keys: (prefix) => {
const query = db.prepare(`SELECT key FROM ltCache WHERE key LIKE ?`);
return query.all(prefix + "%").map((row) => row.key);
},
};

export function initializeLtCache() {
const exists = db
.prepare(
`SELECT name FROM sqlite_master WHERE type='table' AND name='ltCache'`,
)
.get();
if (exists) {
log("Aborting cache.initializeLtCache early because table already exists");
return true;
}

log("Creating ltCache table");
db.exec(`CREATE TABLE IF NOT EXISTS ltCache (
key TEXT PRIMARY KEY,
value TEXT)`);
return false;
}

export function initialize(messages) {
let isSetup = true;
const tables = ["fingerprints", "submissions", "upvotes", "comments"];
tables.forEach((table) => {
Expand Down Expand Up @@ -670,7 +710,7 @@ export function listNewest(limit = 30, lookbackUnixTime) {
return submissionsWithUpvotes;
}

function insertMessage(message) {
export function insertMessage(message) {
const { type, href, index, title, timestamp, signature, signer, identity } =
message;

Expand Down Expand Up @@ -730,5 +770,3 @@ function insertMessage(message) {
throw new Error("Unsupported message type");
}
}

export { initialize, insertMessage };
2 changes: 1 addition & 1 deletion src/karma.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function count(messages, endDate) {

export function all() {
const map = {};
for (const key of cache.keys()) {
for (const key of cache.keys(`${karmaPrefix}-`)) {
if (key.includes(karmaPrefix)) {
const [_, address] = key.split(`${karmaPrefix}-`);
map[address] = cache.get(key);
Expand Down
4 changes: 4 additions & 0 deletions src/launch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import * as feed from "./views/feed.mjs";
import * as feeds from "./feeds.mjs";
import * as moderation from "./views/moderation.mjs";

// NOTE: Initializing the lifetime cache as a first order is important as it
// is widely used throughout the application.
cache.initializeLtCache();

const reconcileMode = env.NODE_ENV === "reconcile";
const productionMode = env.NODE_ENV === "production";
const devMode = env.NODE_ENV === "dev";
Expand Down

0 comments on commit f37372f

Please sign in to comment.