31 changed files with 143 additions and 195 deletions
Binary file not shown.
@ -1,13 +1,14 @@ |
|||||
|
import { getCache, setCache } from "#server/utils/context"; |
||||
import { getStats } from "../../service/scheduler"; |
import { getStats } from "../../service/scheduler"; |
||||
import { getJobCount } from "../../scheduler/engine"; |
import { getJobCount } from "../../scheduler/engine"; |
||||
|
|
||||
export default defineWrappedResponseHandler(async (event) => { |
export default defineWrappedResponseHandler(async (event) => { |
||||
const cacheKey = 'scheduler:stats' |
const cacheKey = 'scheduler:stats' |
||||
const cached = await event.context.cache.get<{ totalTasks: number, enabledTasks: number, last24hExecutions: number, activeJobs: number }>(cacheKey) |
const cached = await getCache<{ totalTasks: number, enabledTasks: number, last24hExecutions: number, activeJobs: number }>(cacheKey) |
||||
if (cached) return R.success({ ...cached, activeJobs: getJobCount() }) |
if (cached) return R.success({ ...cached, activeJobs: getJobCount() }) |
||||
|
|
||||
const stats = await getStats(); |
const stats = await getStats(); |
||||
const result = { ...stats, activeJobs: getJobCount() } |
const result = { ...stats, activeJobs: getJobCount() } |
||||
await event.context.cache.set(cacheKey, result, 60) |
await setCache(cacheKey, result, 60) |
||||
return R.success(result); |
return R.success(result); |
||||
}); |
}); |
||||
@ -1,22 +0,0 @@ |
|||||
import { createCache } from "cache"; |
|
||||
|
|
||||
const cache = createCache({ |
|
||||
// redis: {
|
|
||||
// host: process.env.REDIS_HOST ?? '127.0.0.1',
|
|
||||
// port: Number(process.env.REDIS_PORT ?? 6379),
|
|
||||
// password: process.env.REDIS_PASSWORD,
|
|
||||
// db: Number(process.env.REDIS_DB ?? 0),
|
|
||||
// },
|
|
||||
// defaultTtl: 300,
|
|
||||
memory: true, |
|
||||
}); |
|
||||
|
|
||||
if (import.meta.dev) { |
|
||||
console.log("plugin: 00.cache"); |
|
||||
} |
|
||||
|
|
||||
export default defineNitroPlugin((nitroApp) => { |
|
||||
nitroApp.hooks.hook("request", (event) => { |
|
||||
event.context.cache = cache; |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,68 +0,0 @@ |
|||||
|
|
||||
import { createCache } from "cache"; |
|
||||
import { createAuthContext } from "../service/auth/context"; |
|
||||
import { getGlobalConfigValue, getMergedConfigValue, getUserConfigValue } from "../service/config"; |
|
||||
import type { KnownConfigKey, KnownConfigValue } from "../service/config/registry"; |
|
||||
|
|
||||
if (import.meta.dev) { |
|
||||
console.log("plugin: 01.context"); |
|
||||
} |
|
||||
|
|
||||
type ConfigContext = { |
|
||||
getGlobal: <K extends KnownConfigKey>(key: K) => Promise<KnownConfigValue<K>>; |
|
||||
getUser: <K extends KnownConfigKey>(key: K) => Promise<KnownConfigValue<K> | undefined>; |
|
||||
get: <K extends KnownConfigKey>(key: K) => Promise<KnownConfigValue<K>>; |
|
||||
}; |
|
||||
|
|
||||
interface CachedConfigCache { |
|
||||
get<T>(key: string): Promise<T | null>; |
|
||||
set<T>(key: string, value: T, ttl?: number): Promise<void>; |
|
||||
del(key: string): Promise<void>; |
|
||||
} |
|
||||
|
|
||||
declare module "h3" { |
|
||||
interface H3EventContext { |
|
||||
auth: ReturnType<typeof createAuthContext>; |
|
||||
config: ConfigContext; |
|
||||
cache: ReturnType<typeof createCache>; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
export default defineNitroPlugin((nitroApp) => { |
|
||||
nitroApp.hooks.hook("request", async (event) => { |
|
||||
event.context.auth = createAuthContext(event); |
|
||||
|
|
||||
const cache = event.context.cache; |
|
||||
|
|
||||
event.context.config = { |
|
||||
getGlobal: async <K extends KnownConfigKey>(key: K) => { |
|
||||
const cacheKey = `config:global:${key}`; |
|
||||
const cached = await cache.get<KnownConfigValue<K>>(cacheKey); |
|
||||
if (cached !== null) return cached; |
|
||||
const value = await getGlobalConfigValue(key); |
|
||||
await cache.set(cacheKey, value); |
|
||||
return value; |
|
||||
}, |
|
||||
getUser: async <K extends KnownConfigKey>(key: K) => { |
|
||||
const user = await event.context.auth.getCurrent(); |
|
||||
const cacheKey = `config:user:${user?.id ?? "anonymous"}:${key}`; |
|
||||
const cached = await cache.get<KnownConfigValue<K>>(cacheKey); |
|
||||
if (cached !== null) return cached; |
|
||||
const value = await getUserConfigValue(user?.id, key); |
|
||||
if (value !== undefined) { |
|
||||
await cache.set(cacheKey, value); |
|
||||
} |
|
||||
return value; |
|
||||
}, |
|
||||
get: async <K extends KnownConfigKey>(key: K) => { |
|
||||
const user = await event.context.auth.getCurrent(); |
|
||||
const cacheKey = `config:merged:${user?.id ?? "anonymous"}:${key}`; |
|
||||
const cached = await cache.get<KnownConfigValue<K>>(cacheKey); |
|
||||
if (cached !== null) return cached; |
|
||||
const value = await getMergedConfigValue(user?.id, key); |
|
||||
await cache.set(cacheKey, value); |
|
||||
return value; |
|
||||
}, |
|
||||
}; |
|
||||
}); |
|
||||
}) |
|
||||
@ -0,0 +1,56 @@ |
|||||
|
import type { H3Event } from "h3"; |
||||
|
import { createCache } from "cache"; |
||||
|
import { createAuthContext } from "../service/auth/context"; |
||||
|
import { getGlobalConfigValue, getMergedConfigValue, getUserConfigValue } from "../service/config"; |
||||
|
import type { KnownConfigKey, KnownConfigValue } from "../service/config/registry"; |
||||
|
|
||||
|
// ============= Cache =============
|
||||
|
const _cache = createCache({ |
||||
|
memory: true, |
||||
|
}); |
||||
|
|
||||
|
export const cache = _cache; |
||||
|
|
||||
|
export async function getCache<T>(key: string): Promise<T | null> { |
||||
|
return _cache.get<T>(key); |
||||
|
} |
||||
|
|
||||
|
export async function setCache<T>(key: string, value: T, ttl?: number): Promise<void> { |
||||
|
return _cache.set(key, value, ttl); |
||||
|
} |
||||
|
|
||||
|
export async function delCache(key: string): Promise<void> { |
||||
|
return _cache.del(key); |
||||
|
} |
||||
|
|
||||
|
// ============= Auth =============
|
||||
|
export function getAuth(event: H3Event) { |
||||
|
return createAuthContext(event); |
||||
|
} |
||||
|
|
||||
|
export async function getCurrentUser(event: H3Event) { |
||||
|
const auth = getAuth(event); |
||||
|
return auth.getCurrent(); |
||||
|
} |
||||
|
|
||||
|
export async function requireUser(event: H3Event) { |
||||
|
const auth = getAuth(event); |
||||
|
return auth.requireUser(); |
||||
|
} |
||||
|
|
||||
|
// ============= Config =============
|
||||
|
export async function getConfigGlobal<K extends KnownConfigKey>(key: K): Promise<KnownConfigValue<K>> { |
||||
|
return getGlobalConfigValue(key); |
||||
|
} |
||||
|
|
||||
|
export async function getConfigUser<K extends KnownConfigKey>(event: H3Event, key: K): Promise<KnownConfigValue<K> | undefined> { |
||||
|
const auth = getAuth(event); |
||||
|
const user = await auth.getCurrent(); |
||||
|
return getUserConfigValue(user?.id ?? undefined, key); |
||||
|
} |
||||
|
|
||||
|
export async function getConfig<K extends KnownConfigKey>(event: H3Event, key: K): Promise<KnownConfigValue<K>> { |
||||
|
const auth = getAuth(event); |
||||
|
const user = await auth.getCurrent(); |
||||
|
return getMergedConfigValue(user?.id ?? undefined, key); |
||||
|
} |
||||
Loading…
Reference in new issue