Home / Class/ UpstashCache Class — drizzle-orm Architecture

UpstashCache Class — drizzle-orm Architecture

Architecture documentation for the UpstashCache class in cache.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  418ebf5b_65ad_1cd4_79ba_2c99e874ea49["UpstashCache"]
  44f92f64_5c49_a0a8_3c18_2e6651af97f8["cache.ts"]
  418ebf5b_65ad_1cd4_79ba_2c99e874ea49 -->|defined in| 44f92f64_5c49_a0a8_3c18_2e6651af97f8
  3b21c2b9_2aa3_2033_cf1b_1f421845fa80["constructor()"]
  418ebf5b_65ad_1cd4_79ba_2c99e874ea49 -->|method| 3b21c2b9_2aa3_2033_cf1b_1f421845fa80
  fb493bfc_7744_1256_6440_7fb6f9529516["strategy()"]
  418ebf5b_65ad_1cd4_79ba_2c99e874ea49 -->|method| fb493bfc_7744_1256_6440_7fb6f9529516
  6b1b6075_1cea_b0cb_153b_1fcd11a7bf16["toInternalConfig()"]
  418ebf5b_65ad_1cd4_79ba_2c99e874ea49 -->|method| 6b1b6075_1cea_b0cb_153b_1fcd11a7bf16
  242a537d_d218_d2bf_f6bb_32151abfb86e["get()"]
  418ebf5b_65ad_1cd4_79ba_2c99e874ea49 -->|method| 242a537d_d218_d2bf_f6bb_32151abfb86e
  ac7a72b9_8a56_d022_b35e_119e88a6e847["put()"]
  418ebf5b_65ad_1cd4_79ba_2c99e874ea49 -->|method| ac7a72b9_8a56_d022_b35e_119e88a6e847
  4548e6ce_bedc_ec09_3adb_957a67415b1c["onMutate()"]
  418ebf5b_65ad_1cd4_79ba_2c99e874ea49 -->|method| 4548e6ce_bedc_ec09_3adb_957a67415b1c

Relationship Graph

Source Code

drizzle-orm/src/cache/upstash/cache.ts lines 60–202

export class UpstashCache extends Cache {
	static override readonly [entityKind]: string = 'UpstashCache';
	/**
	 * Prefix for sets which denote the composite table names for each unique table
	 *
	 * Example: In the composite table set of "table1", you may find
	 * `${compositeTablePrefix}table1,table2` and `${compositeTablePrefix}table1,table3`
	 */
	private static compositeTableSetPrefix = '__CTS__';
	/**
	 * Prefix for hashes which map hash or tags to cache values
	 */
	private static compositeTablePrefix = '__CT__';
	/**
	 * Key which holds the mapping of tags to composite table names
	 *
	 * Using this tagsMapKey, you can find the composite table name for a given tag
	 * and get the cache value for that tag:
	 *
	 * ```ts
	 * const compositeTable = redis.hget(tagsMapKey, 'tag1')
	 * console.log(compositeTable) // `${compositeTablePrefix}table1,table2`
	 *
	 * const cachevalue = redis.hget(compositeTable, 'tag1')
	 */
	private static tagsMapKey = '__tagsMap__';
	/**
	 * Queries whose auto invalidation is false aren't stored in their respective
	 * composite table hashes because those hashes are deleted when a mutation
	 * occurs on related tables.
	 *
	 * Instead, they are stored in a separate hash with the prefix
	 * `__nonAutoInvalidate__` to prevent them from being deleted when a mutation
	 */
	private static nonAutoInvalidateTablePrefix = '__nonAutoInvalidate__';

	private luaScripts: {
		getByTagScript: Script;
		onMutateScript: Script;
	};

	private internalConfig: { seconds: number; hexOptions?: ExpireOptions };

	constructor(public redis: Redis, config?: CacheConfig, protected useGlobally?: boolean) {
		super();
		this.internalConfig = this.toInternalConfig(config);
		this.luaScripts = {
			getByTagScript: this.redis.createScript(getByTagScript, { readonly: true }),
			onMutateScript: this.redis.createScript(onMutateScript),
		};
	}

	public strategy() {
		return this.useGlobally ? 'all' : 'explicit';
	}

	private toInternalConfig(config?: CacheConfig): { seconds: number; hexOptions?: ExpireOptions } {
		return config
			? {
				seconds: config.ex!,
				hexOptions: config.hexOptions,
			}
			: {
				seconds: 1,
			};
	}

	override async get(
		key: string,
		tables: string[],
		isTag: boolean = false,
		isAutoInvalidate?: boolean,
	): Promise<any[] | undefined> {
		if (!isAutoInvalidate) {
			const result = await this.redis.hget(UpstashCache.nonAutoInvalidateTablePrefix, key);
			return result === null ? undefined : result as any[];
		}

		if (isTag) {
			const result = await this.luaScripts.getByTagScript.exec([UpstashCache.tagsMapKey], [key]);
			return result === null ? undefined : result as any[];

Domain

Frequently Asked Questions

What is the UpstashCache class?
UpstashCache is a class in the drizzle-orm codebase, defined in drizzle-orm/src/cache/upstash/cache.ts.
Where is UpstashCache defined?
UpstashCache is defined in drizzle-orm/src/cache/upstash/cache.ts at line 60.

Analyze Your Own Codebase

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

Try Supermodel Free