Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:cache): align Redis and MySQL adapter behavior
johaven5c5f01cdeadc
diff --git a/backend/src/infrastructure/cache/adapters/mysql-cache.adapter.ts b/backend/src/infrastructure/cache/adapters/mysql-cache.adapter.tsindex fda760ba..3c6e73b0 100644--- a/backend/src/infrastructure/cache/adapters/mysql-cache.adapter.ts+++ b/backend/src/infrastructure/cache/adapters/mysql-cache.adapter.ts@@ -1,7 +1,7 @@import { Inject, Injectable, Logger } from '@nestjs/common'import { SchedulerRegistry } from '@nestjs/schedule'import { CronJob } from 'cron'-import { and, between, eq, exists, inArray, like, notBetween, SQL, sql } from 'drizzle-orm'+import { and, between, eq, exists, inArray, notBetween, SQL, sql } from 'drizzle-orm'import cluster from 'node:cluster'import { createCacheKeySlug, currentTimeStamp } from '../../../common/shared'import { configuration } from '../../../configuration/config.environment'@@ -61,7 +61,7 @@ export class MysqlCacheAdapter implements Cache {const ks = await this.db.select({ key: cache.key }).from(cache)- .where(and(like(cache.key, pattern.replaceAll('*', '%')), this.whereNotExpired()))+ .where(and(sql`${cache.key} LIKE ${this.toLikePattern(pattern)} ESCAPE '='`, this.whereNotExpired()))return ks.map((k: { key: string }) => k.key)}@@ -90,11 +90,13 @@ export class MysqlCacheAdapter implements Cache {}async mget(keys: string[]): Promise<any[]> {- const vs: { value: any }[] = await this.db- .select({ value: cache.value })+ if (!keys.length) return []+ const entries: { key: string; value: any }[] = await this.db+ .select({ key: cache.key, value: cache.value }).from(cache).where(and(inArray(cache.key, keys), this.whereNotExpired()))- return vs.map((v: { value: any }) => v.value)+ const valuesByKey = new Map(entries.map((entry) => [entry.key, entry.value]))+ return keys.map((key) => valuesByKey.get(key))}async increment(key: string, amount = 1, ttl?: number, minimum?: number): Promise<number> {@@ -195,7 +197,9 @@ export class MysqlCacheAdapter implements Cache {}async mdel(keys: string[]): Promise<boolean> {- return dbCheckAffectedRows(await this.db.delete(cache).where(inArray(cache.key, keys)), keys.length, false)+ if (!keys.length) return false+ const [result] = await this.db.delete(cache).where(inArray(cache.key, keys))+ return result.affectedRows > 0}genSlugKey(...args: any[]): string {@@ -222,6 +226,10 @@ export class MysqlCacheAdapter implements Cache {return data}+ private toLikePattern(pattern: string): string {+ return pattern.replaceAll('=', '==').replaceAll('%', '=%').replaceAll('_', '=_').replaceAll('*', '%')+ }+private async clearExpiredKeys() {try {await this.db.delete(cache).where(this.whereExpired())diff --git a/backend/src/infrastructure/cache/adapters/redis-cache.adapter.ts b/backend/src/infrastructure/cache/adapters/redis-cache.adapter.tsindex cb9a172d..00ea691a 100644--- a/backend/src/infrastructure/cache/adapters/redis-cache.adapter.ts+++ b/backend/src/infrastructure/cache/adapters/redis-cache.adapter.ts@@ -41,7 +41,7 @@ export class RedisCacheAdapter implements Cache {async keys(pattern: string): Promise<string[]> {const matches: string[] = []- for await (const keys of this.client.scanIterator({ MATCH: pattern, COUNT: 100 })) {+ for await (const keys of this.client.scanIterator({ MATCH: this.toGlobPattern(pattern), COUNT: 100 })) {matches.push(...keys)}return matches@@ -52,6 +52,7 @@ export class RedisCacheAdapter implements Cache {}async mget(keys: string[]): Promise<(any | undefined)[]> {+ if (!keys.length) return []return (await this.client.mGet(keys)).map((v) => this.deserialize(v))}@@ -132,7 +133,13 @@ export class RedisCacheAdapter implements Cache {async set(key: string, data: unknown, ttl?: number): Promise<boolean> {const exp = this.getTTL(ttl)- return (await this.client.set(key, this.serialize(data), { expiration: { type: 'EX', value: exp === -1 ? undefined : exp } })) === 'OK'+ const options = exp === this.infiniteExpiration ? {} : { expiration: { type: 'EX' as const, value: exp } }+ try {+ return (await this.client.set(key, this.serialize(data), options)) === 'OK'+ } catch (e) {+ this.logger.error({ tag: this.set.name, msg: `${e}` })+ return false+ }}async del(key: any): Promise<boolean> {@@ -140,6 +147,7 @@ export class RedisCacheAdapter implements Cache {}async mdel(keys: string[]): Promise<boolean> {+ if (!keys.length) return falseconst multi = this.client.multi()for (const key of keys) {multi.unlink(key)@@ -184,4 +192,8 @@ export class RedisCacheAdapter implements Cache {}return JSON.parse(data)}++ private toGlobPattern(pattern: string): string {+ return pattern.replaceAll('\\', '\\\\').replaceAll('?', '\\?').replaceAll('[', '\\[').replaceAll(']', '\\]')+ }}diff --git a/backend/src/infrastructure/cache/cache.e2e-spec.ts b/backend/src/infrastructure/cache/cache.e2e-spec.tsindex e6d0024a..aa2e267b 100644--- a/backend/src/infrastructure/cache/cache.e2e-spec.ts+++ b/backend/src/infrastructure/cache/cache.e2e-spec.ts@@ -56,6 +56,16 @@ describe(Cache.name, () => {expect(values[1]).toBeNull()})+ it('should preserve key order and missing entries when getting multiple values', async () => {+ await cache.set('ordered-first', 'first')+ await cache.set('ordered-second', 'second')++ expect(await cache.mget(['ordered-second', 'ordered-missing', 'ordered-first'])).toEqual(['second', undefined, 'first'])+ expect(await cache.mget([])).toEqual([])++ await cache.mdel(['ordered-first', 'ordered-second'])+ })+it('should delete the key', async () => {expect(await cache.del('foo')).toBe(true)expect(await cache.has('foo')).toBe(false)@@ -72,6 +82,30 @@ describe(Cache.name, () => {expect(await cache.keys('foo*')).toHaveLength(0)})+ it('should report whether at least one key was deleted', async () => {+ await cache.set('delete-existing', true)++ expect(await cache.mdel(['delete-existing', 'delete-missing'])).toBe(true)+ expect(await cache.mdel(['delete-missing'])).toBe(false)+ expect(await cache.mdel([])).toBe(false)+ })++ it('should only use asterisks as key pattern wildcards', async () => {+ const keys = ['pattern_1', 'pattern%2', 'pattern[3]', 'pattern3', 'pattern?', 'pattern=4', 'pattern\\5']+ for (const key of keys) {+ await cache.set(key, true)+ }++ expect(await cache.keys('pattern_*')).toEqual(['pattern_1'])+ expect(await cache.keys('pattern%*')).toEqual(['pattern%2'])+ expect(await cache.keys('pattern[3]')).toEqual(['pattern[3]'])+ expect(await cache.keys('pattern?')).toEqual(['pattern?'])+ expect(await cache.keys('pattern=4')).toEqual(['pattern=4'])+ expect(await cache.keys('pattern\\5')).toEqual(['pattern\\5'])++ await cache.mdel(keys)+ })+it('should create the key & value with a TTL', async () => {expect(await cache.set('foo', 'bar', 1)).toBe(true)expect(await cache.get('foo')).toBe('bar')@@ -80,6 +114,12 @@ describe(Cache.name, () => {expect(await cache.get('foo')).toBeUndefined()})+ it('should create a key without expiration', async () => {+ expect(await cache.set('persistent', 'value', 0)).toBe(true)+ expect(await cache.get('persistent')).toBe('value')+ await cache.del('persistent')+ })+it('should create a slug key from parameters', () => {expect(cache.genSlugKey('foo', 'BAR', 12341)).toBe('foo-bar-12341')})diff --git a/backend/src/infrastructure/cache/cache.service.ts b/backend/src/infrastructure/cache/cache.service.tsindex 3c1175f7..03a52964 100644--- a/backend/src/infrastructure/cache/cache.service.ts+++ b/backend/src/infrastructure/cache/cache.service.ts@@ -12,12 +12,15 @@ export abstract class Cache implements OnModuleInit, OnModuleDestroy {abstract has(key: string): Promise<boolean>/*- pattern must use '*' as wildcard+ pattern supports only '*' as wildcard*/abstract keys(pattern: string): Promise<string[]>abstract get(key: string): Promise<any>+ /*+ values preserve the requested key order; missing keys return undefined+ */abstract mget(keys: string[]): Promise<any[]>abstract increment(key: string, amount?: number, ttl?: number, minimum?: number): Promise<number>@@ -33,6 +36,9 @@ export abstract class Cache implements OnModuleInit, OnModuleDestroy {abstract del(key: string): Promise<boolean>+ /*+ returns true when at least one key was deleted+ */abstract mdel(keys: string[]): Promise<boolean>abstract genSlugKey(...args: any[]): string