feat: better layout in a lot of places
This commit is contained in:
16
lib/cache/cache.ts
vendored
16
lib/cache/cache.ts
vendored
@ -54,7 +54,19 @@ export function expire(id: string, seconds: number) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function set<T extends RedisValue>(id: string, content: T) {
|
||||
type RedisOptions = {
|
||||
expires?: number;
|
||||
};
|
||||
|
||||
export async function set<T extends RedisValue>(
|
||||
id: string,
|
||||
content: T,
|
||||
options?: RedisOptions,
|
||||
) {
|
||||
console.log("[cache] storing ", { id });
|
||||
return await cache.set(id, content);
|
||||
const res = await cache.set(id, content);
|
||||
if (options?.expires) {
|
||||
await expire(id, options.expires);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
46
lib/cache/documents.ts
vendored
46
lib/cache/documents.ts
vendored
@ -1,57 +1,31 @@
|
||||
import { Document } from "@lib/documents.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
|
||||
type DocumentsCache = {
|
||||
lastUpdated: number;
|
||||
documents: Document[];
|
||||
};
|
||||
|
||||
const CACHE_INTERVAL = 5000; // 5 seconds;
|
||||
const CACHE_INTERVAL = 20; // 5 seconds;
|
||||
const CACHE_KEY = "documents";
|
||||
|
||||
export async function getDocuments() {
|
||||
const docs = await cache.get<DocumentsCache>(CACHE_KEY);
|
||||
if (!docs) return;
|
||||
|
||||
if (Date.now() > docs.lastUpdated + CACHE_INTERVAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
return docs.documents;
|
||||
const res = await cache.get<string>(CACHE_KEY);
|
||||
if (res) return JSON.parse(res);
|
||||
return;
|
||||
}
|
||||
|
||||
export function setDocuments(documents: Document[]) {
|
||||
return cache.set(
|
||||
CACHE_KEY,
|
||||
JSON.stringify({
|
||||
lastUpdated: Date.now(),
|
||||
documents,
|
||||
}),
|
||||
JSON.stringify(documents),
|
||||
{ expires: CACHE_INTERVAL },
|
||||
);
|
||||
}
|
||||
|
||||
type DocumentCache = {
|
||||
lastUpdated: number;
|
||||
content: string;
|
||||
};
|
||||
|
||||
export async function getDocument(id: string) {
|
||||
const doc = await cache.get<DocumentCache>(CACHE_KEY + "/" + id);
|
||||
if (!doc) return;
|
||||
|
||||
if (Date.now() > doc.lastUpdated + CACHE_INTERVAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
return doc.content;
|
||||
export function getDocument(id: string) {
|
||||
return cache.get<string>(CACHE_KEY + "/" + id);
|
||||
}
|
||||
|
||||
export async function setDocument(id: string, content: string) {
|
||||
await cache.set(
|
||||
CACHE_KEY + "/" + id,
|
||||
JSON.stringify({
|
||||
lastUpdated: Date.now(),
|
||||
content,
|
||||
}),
|
||||
content,
|
||||
{ expires: CACHE_INTERVAL },
|
||||
);
|
||||
}
|
||||
|
1
lib/cache/image.ts
vendored
1
lib/cache/image.ts
vendored
@ -12,7 +12,6 @@ const CACHE_KEY = "images";
|
||||
|
||||
function getCacheKey({ url: _url, width, height }: ImageCacheOptions) {
|
||||
const url = new URL(_url);
|
||||
|
||||
return `${CACHE_KEY}/${url.hostname}/${url.pathname}/${width}/${height}`
|
||||
.replace(
|
||||
"//",
|
||||
|
Reference in New Issue
Block a user