Add support for jobs & queueables, migrations
- Create migration directives & migrators - Modify Cache classes to support array manipulation - Create Redis unit and RedisCache implementation - Create Queueable base class and Queue class that uses Cache backend
This commit is contained in:
59
src/util/cache/InMemCache.ts
vendored
59
src/util/cache/InMemCache.ts
vendored
@@ -1,5 +1,7 @@
|
||||
import { Cache } from './Cache'
|
||||
import { Collection } from '../collection/Collection'
|
||||
import {Awaitable, Maybe} from '../support/types'
|
||||
import {ErrorWithContext} from '../error/ErrorWithContext'
|
||||
|
||||
/**
|
||||
* Base interface for an item stored in a memory cache.
|
||||
@@ -44,4 +46,61 @@ export class InMemCache extends Cache {
|
||||
public async drop(key: string): Promise<void> {
|
||||
this.items = this.items.whereNot('key', '=', key)
|
||||
}
|
||||
|
||||
public pop(key: string): Awaitable<Maybe<string>> {
|
||||
const existing = this.items.firstWhere('key', '=', key)
|
||||
this.items = this.items.where('key', '!=', key)
|
||||
return existing?.item
|
||||
}
|
||||
|
||||
public async increment(key: string, amount?: number): Promise<number> {
|
||||
const next = parseInt((await this.fetch(key)) ?? '0', 10) + (amount ?? 1)
|
||||
await this.put(key, String(next))
|
||||
return next
|
||||
}
|
||||
|
||||
public async decrement(key: string, amount?: number): Promise<number> {
|
||||
const next = parseInt((await this.fetch(key)) ?? '0', 10) - (amount ?? 1)
|
||||
await this.put(key, String(next))
|
||||
return next
|
||||
}
|
||||
|
||||
public arrayPush(key: string, value: string): Awaitable<void> {
|
||||
const existing = this.items.where('key', '=', key).first()
|
||||
const arr = JSON.parse(existing?.item ?? '[]')
|
||||
|
||||
if ( !Array.isArray(arr) ) {
|
||||
throw new ErrorWithContext('Unable to arrayPush: key is not an array', {
|
||||
key,
|
||||
value,
|
||||
})
|
||||
}
|
||||
|
||||
arr.push(value)
|
||||
if ( existing ) {
|
||||
existing.item = JSON.stringify(arr)
|
||||
} else {
|
||||
this.items.push({
|
||||
key,
|
||||
item: JSON.stringify(arr),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public arrayPop(key: string): Awaitable<Maybe<string>> {
|
||||
const existing = this.items.where('key', '=', key).first()
|
||||
const arr = JSON.parse(existing?.item ?? '[]')
|
||||
|
||||
const value = arr.pop()
|
||||
if ( existing ) {
|
||||
existing.item = JSON.stringify(arr)
|
||||
} else {
|
||||
this.items.push({
|
||||
key,
|
||||
item: JSON.stringify(arr),
|
||||
})
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user