Home / Class/ TestGlobalCache Class — drizzle-orm Architecture

TestGlobalCache Class — drizzle-orm Architecture

Architecture documentation for the TestGlobalCache class in mysql-common-cache.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  208fff58_f804_2a07_330d_076472ff343f["TestGlobalCache"]
  cccbc47d_c2bb_d1ee_3a7e_546f7279bf7e["mysql-common-cache.ts"]
  208fff58_f804_2a07_330d_076472ff343f -->|defined in| cccbc47d_c2bb_d1ee_3a7e_546f7279bf7e
  6903657d_c9ae_555c_7896_bd975e5a2553["constructor()"]
  208fff58_f804_2a07_330d_076472ff343f -->|method| 6903657d_c9ae_555c_7896_bd975e5a2553
  f195ffa0_6c36_25c7_f3b7_0773eca6c573["strategy()"]
  208fff58_f804_2a07_330d_076472ff343f -->|method| f195ffa0_6c36_25c7_f3b7_0773eca6c573
  512a7b08_fd6b_66d6_4e5e_f1c982eb6d75["get()"]
  208fff58_f804_2a07_330d_076472ff343f -->|method| 512a7b08_fd6b_66d6_4e5e_f1c982eb6d75
  36c0ccc0_82ea_c367_6f87_d9a50834f614["put()"]
  208fff58_f804_2a07_330d_076472ff343f -->|method| 36c0ccc0_82ea_c367_6f87_d9a50834f614
  2d31e9c7_3522_1366_3101_c4a3b8e55741["onMutate()"]
  208fff58_f804_2a07_330d_076472ff343f -->|method| 2d31e9c7_3522_1366_3101_c4a3b8e55741

Relationship Graph

Source Code

integration-tests/tests/mysql/mysql-common-cache.ts lines 11–69

export class TestGlobalCache extends Cache {
	private globalTtl: number = 1000;
	private usedTablesPerKey: Record<string, string[]> = {};

	constructor(private kv: Keyv = new Keyv()) {
		super();
	}

	override strategy(): 'explicit' | 'all' {
		return 'all';
	}
	override async get(key: string, _tables: string[], _isTag: boolean): Promise<any[] | undefined> {
		const res = await this.kv.get(key) ?? undefined;
		return res;
	}
	override async put(
		key: string,
		response: any,
		tables: string[],
		isTag: boolean,
		config?: CacheConfig,
	): Promise<void> {
		await this.kv.set(key, response, config ? config.ex : this.globalTtl);
		for (const table of tables) {
			const keys = this.usedTablesPerKey[table];
			if (keys === undefined) {
				this.usedTablesPerKey[table] = [key];
			} else {
				keys.push(key);
			}
		}
	}
	override async onMutate(params: MutationOption): Promise<void> {
		const tagsArray = params.tags ? Array.isArray(params.tags) ? params.tags : [params.tags] : [];
		const tablesArray = params.tables ? Array.isArray(params.tables) ? params.tables : [params.tables] : [];

		const keysToDelete = new Set<string>();

		for (const table of tablesArray) {
			const tableName = is(table, Table) ? getTableName(table) : table as string;
			const keys = this.usedTablesPerKey[tableName] ?? [];
			for (const key of keys) keysToDelete.add(key);
		}

		if (keysToDelete.size > 0 || tagsArray.length > 0) {
			for (const tag of tagsArray) {
				await this.kv.delete(tag);
			}

			for (const key of keysToDelete) {
				await this.kv.delete(key);
				for (const table of tablesArray) {
					const tableName = is(table, Table) ? getTableName(table) : table as string;
					this.usedTablesPerKey[tableName] = [];
				}
			}
		}
	}
}

Domain

Frequently Asked Questions

What is the TestGlobalCache class?
TestGlobalCache is a class in the drizzle-orm codebase, defined in integration-tests/tests/mysql/mysql-common-cache.ts.
Where is TestGlobalCache defined?
TestGlobalCache is defined in integration-tests/tests/mysql/mysql-common-cache.ts at line 11.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free