Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  upload-cache.ts

  Sprache: JAVA
 

/**
 * Cache `file_info` values returned by the QQ Bot API so identical uploads can be reused
 * before the server-side TTL expires.
 */


import * as crypto from "node:crypto";
import type { ChatScope } from "../types.js";
import { debugLog } from "./log.js";

interface CacheEntry {
  fileInfo: string;
  fileUuid: string;
  expiresAt: number;
}

const cache = new Map<string, CacheEntry>();
const MAX_CACHE_SIZE = 500;

/** Compute an MD5 hash used as part of the cache key. */
export function computeFileHash(data: string | Buffer): string {
  const content = typeof data === "string" ? data : data;
  return crypto.createHash("md5").update(content).digest("hex");
}

/** Build the in-memory cache key. */
function buildCacheKey(
  contentHash: string,
  scope: string,
  targetId: string,
  fileType: number,
): string {
  return `${contentHash}:${scope}:${targetId}:${fileType}`;
}

/** Look up a cached `file_info` value. */
export function getCachedFileInfo(
  contentHash: string,
  scope: ChatScope,
  targetId: string,
  fileType: number,
): string | null {
  const key = buildCacheKey(contentHash, scope, targetId, fileType);
  const entry = cache.get(key);

  if (!entry) {
    return null;
  }

  if (Date.now() >= entry.expiresAt) {
    cache.delete(key);
    return null;
  }

  debugLog(`[upload-cache] Cache HIT: key=${key.slice(040)}..., fileUuid=${entry.fileUuid}`);
  return entry.fileInfo;
}

/** Store an upload result in the cache. */
export function setCachedFileInfo(
  contentHash: string,
  scope: ChatScope,
  targetId: string,
  fileType: number,
  fileInfo: string,
  fileUuid: string,
  ttl: number,
): void {
  if (cache.size >= MAX_CACHE_SIZE) {
    const now = Date.now();
    for (const [k, v] of cache) {
      if (now >= v.expiresAt) {
        cache.delete(k);
      }
    }
    if (cache.size >= MAX_CACHE_SIZE) {
      const keys = Array.from(cache.keys());
      for (let i = 0; i < keys.length / 2; i++) {
        cache.delete(keys[i]);
      }
    }
  }

  const key = buildCacheKey(contentHash, scope, targetId, fileType);
  const safetyMargin = 60;
  const effectiveTtl = Math.max(ttl - safetyMargin, 10);

  cache.set(key, {
    fileInfo,
    fileUuid,
    expiresAt: Date.now() + effectiveTtl * 1000,
  });

  debugLog(
    `[upload-cache] Cache SET: key=${key.slice(040)}..., ttl=${effectiveTtl}s, uuid=${fileUuid}`,
  );
}

/** Return cache stats for diagnostics. */
export function getUploadCacheStats(): { size: number; maxSize: number } {
  return { size: cache.size, maxSize: MAX_CACHE_SIZE };
}

/** Clear the upload cache. */
export function clearUploadCache(): void {
  cache.clear();
  debugLog(`[upload-cache] Cache cleared`);
}

Messung V0.5 in Prozent
C=97 H=90 G=93

¤ Dauer der Verarbeitung: 0.23 Sekunden  (vorverarbeitet am  2026-06-05) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik