Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

feat(backend:cache): add atomic bounded counter increments

johavenc17282520535
diff --git a/backend/src/infrastructure/cache/adapters/mysql-cache.adapter.ts b/backend/src/infrastructure/cache/adapters/mysql-cache.adapter.ts
index 9038d681..85ef0877 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 } from 'drizzle-orm'
+import { and, between, eq, exists, inArray, like, notBetween, SQL, sql } from 'drizzle-orm'
import cluster from 'node:cluster'
import { createCacheKeySlug, currentTimeStamp } from '../../../common/shared'
import { configuration } from '../../../configuration/config.environment'
@@ -96,6 +96,35 @@ export class MysqlCacheAdapter implements Cache {
return vs.map((v: { value: any }) => v.value)
}
+ async increment(key: string, amount = 1, ttl?: number, minimum?: number): Promise<number> {
+ const now = currentTimeStamp()
+ const exp = this.getTTL(ttl)
+ const initialValue = minimum === undefined ? amount : Math.max(amount, minimum)
+ const incrementedValue = sql`IF(${cache.expiration} BETWEEN 0 AND ${now}, ${initialValue}, COALESCE(CAST(JSON_UNQUOTE(JSON_EXTRACT(${cache.value}, '$')) AS SIGNED), 0) + ${amount})`
+ const value = minimum === undefined ? incrementedValue : sql`GREATEST(${incrementedValue}, ${minimum})`
+ try {
+ /*
+ * INSERT ... ON DUPLICATE KEY UPDATE atomically creates or updates the counter while locking its row.
+ * Expired values restart from initialValue, and GREATEST optionally prevents the result from falling below minimum.
+ * On update, LAST_INSERT_ID(value) both stores the computed value and exposes it through the connection-scoped
+ * insertId returned by the driver, avoiding an additional SELECT. A new row returns initialValue directly.
+ */
+ const [result]: any = await this.db
+ .insert(cache)
+ .values({ key, value: initialValue, expiration: exp } satisfies MysqlCache)
+ .onDuplicateKeyUpdate({
+ set: {
+ value: sql`LAST_INSERT_ID(${value})`,
+ expiration: exp
+ } as Partial<MysqlCache>
+ })
+ return result.affectedRows === 1 && Number(result.insertId) === 0 ? initialValue : Number(result.insertId)
+ } catch (e) {
+ this.logger.error({ tag: this.increment.name, msg: `${e}` })
+ throw e
+ }
+ }
+
async set(key: string, data: unknown, ttl?: number): Promise<boolean> {
data = this.serialize(data)
const exp = this.getTTL(ttl)
diff --git a/backend/src/infrastructure/cache/adapters/redis-cache.adapter.ts b/backend/src/infrastructure/cache/adapters/redis-cache.adapter.ts
index d244f7ea..64bf920e 100644
--- a/backend/src/infrastructure/cache/adapters/redis-cache.adapter.ts
+++ b/backend/src/infrastructure/cache/adapters/redis-cache.adapter.ts
@@ -52,6 +52,36 @@ export class RedisCacheAdapter implements Cache {
return (await this.client.mGet(keys)).map((v) => this.deserialize(v))
}
+ async increment(key: string, amount = 1, ttl?: number, minimum?: number): Promise<number> {
+ const exp = this.getTTL(ttl)
+ if (minimum !== undefined) {
+ const value = await this.client.eval(
+ `local value = redis.call('INCRBY', KEYS[1], ARGV[1])
+ if value < tonumber(ARGV[2]) then
+ value = tonumber(ARGV[2])
+ redis.call('SET', KEYS[1], value)
+ end
+ if tonumber(ARGV[3]) == -1 then
+ redis.call('PERSIST', KEYS[1])
+ else
+ redis.call('EXPIRE', KEYS[1], ARGV[3])
+ end
+ return value`,
+ { keys: [key], arguments: [String(amount), String(minimum), String(exp)] }
+ )
+ return Number(value)
+ }
+ const multi = this.client.multi()
+ multi.incrBy(key, amount)
+ if (exp === this.infiniteExpiration) {
+ multi.persist(key)
+ } else {
+ multi.expire(key, exp)
+ }
+ const [value] = await multi.exec()
+ return Number(value)
+ }
+
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'
diff --git a/backend/src/infrastructure/cache/cache.service.ts b/backend/src/infrastructure/cache/cache.service.ts
index 747fb90b..fe2668c1 100644
--- a/backend/src/infrastructure/cache/cache.service.ts
+++ b/backend/src/infrastructure/cache/cache.service.ts
@@ -19,6 +19,8 @@ export abstract class Cache implements OnModuleInit, OnModuleDestroy {
abstract mget(keys: string[]): Promise<any[]>
+ abstract increment(key: string, amount?: number, ttl?: number, minimum?: number): Promise<number>
+
/* ttl (seconds):
- 0: infinite expiration
- undefined: default ttl