feat: add authentication
This commit is contained in:
17
lib/cache/cache.ts
vendored
17
lib/cache/cache.ts
vendored
@ -10,7 +10,7 @@ const REDIS_HOST = Deno.env.get("REDIS_HOST");
|
||||
const REDIS_PASS = Deno.env.get("REDIS_PASS") || "";
|
||||
const REDIS_PORT = Deno.env.get("REDIS_PORT");
|
||||
|
||||
async function createCache<T>(): Promise<Map<string, T> | Redis> {
|
||||
async function createCache<T>(): Promise<Redis> {
|
||||
if (REDIS_HOST) {
|
||||
const conf: RedisConnectOptions = {
|
||||
hostname: REDIS_HOST,
|
||||
@ -32,6 +32,13 @@ async function createCache<T>(): Promise<Map<string, T> | Redis> {
|
||||
const mockRedis = new Map<string, RedisValue>();
|
||||
|
||||
return {
|
||||
async keys() {
|
||||
return mockRedis.keys();
|
||||
},
|
||||
async delete(key: string) {
|
||||
mockRedis.delete(key);
|
||||
return key;
|
||||
},
|
||||
async set(key: string, value: RedisValue) {
|
||||
mockRedis.set(key, value);
|
||||
return value.toString();
|
||||
@ -74,6 +81,14 @@ type RedisOptions = {
|
||||
expires?: number;
|
||||
};
|
||||
|
||||
export function del(key: string) {
|
||||
return cache.del(key);
|
||||
}
|
||||
|
||||
export function keys(prefix: string) {
|
||||
return cache.keys(prefix);
|
||||
}
|
||||
|
||||
export async function set<T extends RedisValue>(
|
||||
id: string,
|
||||
content: T,
|
||||
|
6
lib/cache/documents.ts
vendored
6
lib/cache/documents.ts
vendored
@ -1,7 +1,7 @@
|
||||
import { Document } from "@lib/documents.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
|
||||
const CACHE_INTERVAL = 20; // 5 seconds;
|
||||
const CACHE_INTERVAL = 60;
|
||||
const CACHE_KEY = "documents";
|
||||
|
||||
export async function getDocuments() {
|
||||
@ -19,12 +19,12 @@ export function setDocuments(documents: Document[]) {
|
||||
}
|
||||
|
||||
export function getDocument(id: string) {
|
||||
return cache.get<string>(CACHE_KEY + "/" + id);
|
||||
return cache.get<string>(CACHE_KEY + ":" + id.replaceAll("/", ":"));
|
||||
}
|
||||
|
||||
export async function setDocument(id: string, content: string) {
|
||||
await cache.set(
|
||||
CACHE_KEY + "/" + id,
|
||||
CACHE_KEY + ":" + id.replaceAll("/", ":"),
|
||||
content,
|
||||
{ expires: CACHE_INTERVAL },
|
||||
);
|
||||
|
14
lib/cache/image.ts
vendored
14
lib/cache/image.ts
vendored
@ -1,4 +1,4 @@
|
||||
import { hash } from "@lib/hash.ts";
|
||||
import { hash } from "@lib/string.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
import { ImageMagick } from "https://deno.land/x/imagemagick_deno@0.0.25/mod.ts";
|
||||
|
||||
@ -13,10 +13,12 @@ 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}`
|
||||
return `${CACHE_KEY}:${url.hostname}:${
|
||||
url.pathname.replaceAll("/", ":")
|
||||
}:${width}:${height}`
|
||||
.replace(
|
||||
"//",
|
||||
"/",
|
||||
"::",
|
||||
":",
|
||||
);
|
||||
}
|
||||
|
||||
@ -44,7 +46,7 @@ export async function getImage({ url, width, height }: ImageCacheOptions) {
|
||||
? JSON.parse(pointerCacheRaw)
|
||||
: pointerCacheRaw;
|
||||
|
||||
const imageContent = await cache.get(pointerCache.id, true);
|
||||
const imageContent = await cache.get(`image:${pointerCache.id}`, true);
|
||||
if (!imageContent) return;
|
||||
|
||||
return {
|
||||
@ -68,7 +70,7 @@ export async function setImage(
|
||||
const cacheKey = getCacheKey({ url, width, height });
|
||||
const pointerId = await hash(cacheKey);
|
||||
|
||||
await cache.set(pointerId, clone);
|
||||
await cache.set(`image:${pointerId}`, clone);
|
||||
cache.expire(pointerId, 60 * 60 * 24);
|
||||
cache.expire(cacheKey, 60 * 60 * 24);
|
||||
|
||||
|
Reference in New Issue
Block a user