Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Last active April 25, 2024 21:53
Show Gist options
  • Save BananaAcid/daebe5d0ecda9698a82b071e8a48b13a to your computer and use it in GitHub Desktop.
Save BananaAcid/daebe5d0ecda9698a82b071e8a48b13a to your computer and use it in GitHub Desktop.
A memory storage class -- code snippet
// "MEMORY STORAGE" ... simplistic
let StoreMemory:{[id:string]:{created:number, alive?:number, data:{[key:string]:any}}} = {};
// StoreMemory clean up (invalidate logins after cookie maxage)
setInterval( () => {
let ttl = h3SessionCookieConfig?.maxAge || 60 * 5;
let now = Date.now();
let max = ttl * 1000;
for (let key in StoreMemory) {
let alive = StoreMemory[key].alive || StoreMemory[key].created;
if (alive + max < now) {
delete StoreMemory[key];
console.log('StoreMemory cleared sessionId:', key);
}
}
}, 60 * 1000);
let NabilSessionStore = StoreMemory;
(globalThis as any).NabilSessionStore = NabilSessionStore;
let storeRef = NabilSessionStore;
return {
id,
data: store?.data || {}, // contains { ... UserDatabase.data:{}, isAuthenticated:bool }
meta: {
created: store?.created,
alive: store?.alive || store?.created,
maxAge: (h3SessionCookieConfig.maxAge || 0) * 1000,
},
clear: () => { if (store) { delete storeRef[id as string]; clear(); return true; } return false; },
keepAlive: () => { if (store) { store.alive = Date.now(); return true; } return false; },
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment