UNPKG

studiocms

Version:

Astro Native CMS for AstroDB. Built from the ground up by the Astro community.

84 lines (83 loc) 3.65 kB
import _logger, { isVerbose } from "studiocms:logger"; import { Effect, genLogger } from "../../../effect.js"; import { CacheContext, isCacheEnabled } from "../utils.js"; import { SDKCore_GET } from "./get.js"; import { SDKCore_PLUGINS } from "./plugins.js"; const cacheId = "__last_updated_at"; const cacheTTL = 30 * 60 * 1e3; class SDKCore_MIDDLEWARES extends Effect.Service()( "studiocms/sdk/SDKCore/modules/middlewares", { dependencies: [SDKCore_GET.Default, SDKCore_PLUGINS.Default], effect: genLogger("studiocms/sdk/SDKCore/modules/middlewares/effect")(function* () { const [ { FolderList, pages, folderTree, siteConfig, pageFolderTree, pluginData }, { pages: updatePages, folderTree: updateFolderTree, pageFolderTree: updatePageFolderTree, folderList: updateFolderList, siteConfig: updateSiteConfig }, { initPluginDataCache } ] = yield* Effect.all([CacheContext, SDKCore_GET, SDKCore_PLUGINS]); const Caches = /* @__PURE__ */ new Map([ ["pages", { cache: pages, updater: () => updatePages(true) }], ["folderList", { cache: FolderList, updater: () => updateFolderList() }], ["folderTree", { cache: folderTree, updater: () => updateFolderTree() }], ["pageFolderTree", { cache: pageFolderTree, updater: () => updatePageFolderTree() }], ["siteConfig", { cache: siteConfig, updater: () => updateSiteConfig() }], ["pluginData", { cache: pluginData, updater: () => initPluginDataCache() }] ]); const logger = _logger.fork("studiocms:middleware/cacheVerification"); const middlewares = { verifyCache: (cacheStore) => genLogger("studiocms/sdk/SDKCore/modules/middlewares/verifyCache")(function* () { const cacheStatus = yield* isCacheEnabled.pipe( Effect.catchAll(() => Effect.succeed(false)) ); if (!cacheStatus) return; isVerbose && logger.info("Verifying caches..."); const todos = []; function createTodoList(message) { isVerbose && logger.info(message); Caches.forEach(({ cache, updater }, name) => { if (cache.size === 0) { isVerbose && logger.info(`Cache "${name}" is empty, updating...`); todos.push(updater()); } else { isVerbose && logger.info(`Cache "${name}" is already populated.`); } }); } const lastCacheUpdate = cacheStore.get(cacheId); if (lastCacheUpdate) { isVerbose && logger.info(`Last cache update was at ${lastCacheUpdate.toISOString()}`); if (Date.now() - lastCacheUpdate.getTime() > cacheTTL) { createTodoList("Cache is stale, updating..."); } else { isVerbose && logger.info("Cache is fresh."); return; } } else { createTodoList("No cache found, updating..."); } if (todos.length === 0) { isVerbose && logger.info("All caches are already populated."); cacheStore.set(cacheId, /* @__PURE__ */ new Date()); return; } isVerbose && logger.info(`Updating caches: ${todos.length} caches to update.`); const start = Date.now(); yield* Effect.all(todos); isVerbose && logger.info(`Cache verification completed in ${Date.now() - start}ms.`); cacheStore.set(cacheId, /* @__PURE__ */ new Date()); }) }; return middlewares; }) } ) { } export { SDKCore_MIDDLEWARES };