Extract cache get runtime

master
dev 5 months ago
parent 37a5e008f5
commit bc4466942d

@ -33,6 +33,7 @@ Estado al cierre:
- `arts/http/engine-http.ts` delega la validacion preflight de `bodySchema` en `request-validation.ts`.
- `arts/http/engine-http.ts` delega la resolucion final de response/error en `response-resolution.ts`.
- `arts/sium/core/pipe.ts` queda centrado en composicion; factories `refine/transform/codec/meta` viven en `steps.ts`.
- `libs/cach/engine.ts` delega lectura `get()` en `runtime-get.ts`.
- `arts/conn/connection.ts` delega decode/routing de frames entrantes en `connection-message-router.ts`.
- `arts/conn/connection.ts` delega el intento open/auth/flush/join en `connection-connect.ts`.
- `arts/conn/connection.ts` delega current transport, attach/detach y close en `connection-transport-runtime.ts`.

@ -35,6 +35,7 @@ import {
} from './runtime-evaluation.ts';
import { readCacheCurrentEpochs } from './runtime-epochs.ts';
import { buildCacheHitExplain, buildCacheMissExplain } from './runtime-explain.ts';
import { createCacheRuntimeGet } from './runtime-get.ts';
import { invalidateCacheEntry } from './runtime-invalidation.ts';
import { createCacheRuntimeIo } from './runtime-io.ts';
import { systemCacheClock } from './runtime-helpers.ts';
@ -49,7 +50,6 @@ import type {
CacheRuntime,
CacheRuntimeConfig,
ExplainOptions,
GetOptions,
InvalidateOptions,
MutateOptions,
QueryOptions,
@ -207,43 +207,7 @@ export function createCacheRuntime(config: CacheRuntimeConfig): CacheRuntime {
});
}
async function get<T>(key: CacheKey, options: GetOptions): Promise<T | undefined> {
const ctx = await contextFor({
key,
scope: options.scope,
policy: options.policy,
mode: options.mode,
schemaVersion: options.schemaVersion
});
const cached = await adapter.get<T>(ctx.fullKey);
if (!cached) {
emitForContext(CACHE_EVENT_MISS, ctx, { reason: CACHE_DECISION_REASON_CACHE_MISS });
return undefined;
}
const decision = await evaluate(cached, ctx);
if (decision.action === CACHE_DECISION_ACTION_DELETE_AND_FETCH) {
emitForContext(CACHE_EVENT_SCHEMA_MISMATCH, ctx, { reason: decision.reason });
await deletion.safeDelete(ctx.fullKey, ctx.keyHash);
return undefined;
}
if (
decision.action === CACHE_DECISION_ACTION_SERVE ||
decision.action === CACHE_DECISION_ACTION_SERVE_AND_REFRESH
) {
emitForContext(
decision.action === CACHE_DECISION_ACTION_SERVE ? CACHE_EVENT_HIT : CACHE_EVENT_STALE_HIT,
ctx,
{ reason: decision.reason }
);
return cached.value;
}
emitForContext(CACHE_EVENT_MISS, ctx, { reason: decision.reason });
return undefined;
}
const get = createCacheRuntimeGet({ adapter, deletion, contextFor, evaluate, emitForContext });
async function set<T>(key: CacheKey, value: T, options: SetOptions): Promise<void> {
const ctx = await contextFor({

@ -0,0 +1,81 @@
import {
CACHE_DECISION_ACTION_DELETE_AND_FETCH,
CACHE_DECISION_ACTION_SERVE,
CACHE_DECISION_ACTION_SERVE_AND_REFRESH,
CACHE_DECISION_REASON_CACHE_MISS,
CACHE_EVENT_HIT,
CACHE_EVENT_MISS,
CACHE_EVENT_SCHEMA_MISMATCH,
CACHE_EVENT_STALE_HIT
} from './consts.ts';
import type { ResolvedCacheContext } from './runtime-context.ts';
import type { CacheRuntimeDelete } from './runtime-delete.ts';
import type {
CacheAdapter,
CacheDecision,
CacheEnvelope,
CacheEvent,
CacheEventType,
CacheKey,
GetOptions
} from './types.ts';
interface CacheRuntimeGetInput {
readonly adapter: CacheAdapter;
readonly deletion: CacheRuntimeDelete;
contextFor(input: {
readonly key: CacheKey;
readonly scope?: GetOptions['scope'];
readonly policy?: GetOptions['policy'];
readonly mode?: GetOptions['mode'];
readonly schemaVersion?: GetOptions['schemaVersion'];
}): Promise<ResolvedCacheContext>;
evaluate<T>(entry: CacheEnvelope<T>, ctx: ResolvedCacheContext): Promise<CacheDecision>;
emitForContext(
type: CacheEventType,
ctx: ResolvedCacheContext,
patch?: Partial<CacheEvent>
): void;
}
export function createCacheRuntimeGet(runtime: CacheRuntimeGetInput) {
return async function get<T>(key: CacheKey, options: GetOptions): Promise<T | undefined> {
const ctx = await runtime.contextFor({
key,
scope: options.scope,
policy: options.policy,
mode: options.mode,
schemaVersion: options.schemaVersion
});
const cached = await runtime.adapter.get<T>(ctx.fullKey);
if (!cached) {
runtime.emitForContext(CACHE_EVENT_MISS, ctx, {
reason: CACHE_DECISION_REASON_CACHE_MISS
});
return undefined;
}
const decision = await runtime.evaluate(cached, ctx);
if (decision.action === CACHE_DECISION_ACTION_DELETE_AND_FETCH) {
runtime.emitForContext(CACHE_EVENT_SCHEMA_MISMATCH, ctx, { reason: decision.reason });
await runtime.deletion.safeDelete(ctx.fullKey, ctx.keyHash);
return undefined;
}
if (
decision.action === CACHE_DECISION_ACTION_SERVE ||
decision.action === CACHE_DECISION_ACTION_SERVE_AND_REFRESH
) {
runtime.emitForContext(
decision.action === CACHE_DECISION_ACTION_SERVE ? CACHE_EVENT_HIT : CACHE_EVENT_STALE_HIT,
ctx,
{ reason: decision.reason }
);
return cached.value;
}
runtime.emitForContext(CACHE_EVENT_MISS, ctx, { reason: decision.reason });
return undefined;
};
}
Loading…
Cancel
Save

Powered by TurnKey Linux.