Cache Key Generator (shared/cache-key.ts)

airi · evals/tts-speech-formula/eval/shared/cache-key.ts · typescript · MIT
dev-tools, database, security, voice, fw:node, tag:utility

Creates a deterministic 32‑character SHA‑256 based cache key from input strings, enabling consistent caching of evaluation results.

// Source: https://github.com/moeru-ai/airi | License: MIT | Copyright 2024-PRESENT Neko Ayaka
// Provided via justclaw.it.com; keep this notice with the code.
import { createHash } from 'node:crypto'

/**
 * Builds one cache key segment from every input that changes a cached answer.
 *
 * @example
 * cacheKey('zh', '$x$')
 * // => 'b5f3…' (32 hex characters)
 */
export function cacheKey(...parts: string[]): string {
  return createHash('sha256').update(JSON.stringify(parts)).digest('hex').slice(0, 32)
}

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key