Merge pull request #12567 from Budibase/fix/try-once-logging
Improving writethrough cache typing and removing useless log statement
This commit is contained in:
commit
dfb64ecc75
|
@ -1,15 +1,16 @@
|
||||||
import { DBTestConfiguration } from "../../../tests/extra"
|
import { DBTestConfiguration } from "../../../tests/extra"
|
||||||
import {
|
import { structures } from "../../../tests"
|
||||||
structures,
|
|
||||||
expectFunctionWasCalledTimesWith,
|
|
||||||
mocks,
|
|
||||||
} from "../../../tests"
|
|
||||||
import { Writethrough } from "../writethrough"
|
import { Writethrough } from "../writethrough"
|
||||||
import { getDB } from "../../db"
|
import { getDB } from "../../db"
|
||||||
|
import { Document } from "@budibase/types"
|
||||||
import tk from "timekeeper"
|
import tk from "timekeeper"
|
||||||
|
|
||||||
tk.freeze(Date.now())
|
tk.freeze(Date.now())
|
||||||
|
|
||||||
|
interface ValueDoc extends Document {
|
||||||
|
value: any
|
||||||
|
}
|
||||||
|
|
||||||
const DELAY = 5000
|
const DELAY = 5000
|
||||||
|
|
||||||
describe("writethrough", () => {
|
describe("writethrough", () => {
|
||||||
|
@ -117,7 +118,7 @@ describe("writethrough", () => {
|
||||||
describe("get", () => {
|
describe("get", () => {
|
||||||
it("should be able to retrieve", async () => {
|
it("should be able to retrieve", async () => {
|
||||||
await config.doInTenant(async () => {
|
await config.doInTenant(async () => {
|
||||||
const response = await writethrough.get(docId)
|
const response = await writethrough.get<ValueDoc>(docId)
|
||||||
expect(response.value).toBe(4)
|
expect(response.value).toBe(4)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -7,7 +7,7 @@ import * as locks from "../redis/redlockImpl"
|
||||||
const DEFAULT_WRITE_RATE_MS = 10000
|
const DEFAULT_WRITE_RATE_MS = 10000
|
||||||
let CACHE: BaseCache | null = null
|
let CACHE: BaseCache | null = null
|
||||||
|
|
||||||
interface CacheItem {
|
interface CacheItem<T extends Document> {
|
||||||
doc: any
|
doc: any
|
||||||
lastWrite: number
|
lastWrite: number
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,10 @@ function makeCacheKey(db: Database, key: string) {
|
||||||
return db.name + key
|
return db.name + key
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeCacheItem(doc: any, lastWrite: number | null = null): CacheItem {
|
function makeCacheItem<T extends Document>(
|
||||||
|
doc: T,
|
||||||
|
lastWrite: number | null = null
|
||||||
|
): CacheItem<T> {
|
||||||
return { doc, lastWrite: lastWrite || Date.now() }
|
return { doc, lastWrite: lastWrite || Date.now() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +38,7 @@ async function put(
|
||||||
) {
|
) {
|
||||||
const cache = await getCache()
|
const cache = await getCache()
|
||||||
const key = doc._id
|
const key = doc._id
|
||||||
let cacheItem: CacheItem | undefined
|
let cacheItem: CacheItem<any> | undefined
|
||||||
if (key) {
|
if (key) {
|
||||||
cacheItem = await cache.get(makeCacheKey(db, key))
|
cacheItem = await cache.get(makeCacheKey(db, key))
|
||||||
}
|
}
|
||||||
|
@ -84,12 +87,12 @@ async function put(
|
||||||
return { ok: true, id: output._id, rev: output._rev }
|
return { ok: true, id: output._id, rev: output._rev }
|
||||||
}
|
}
|
||||||
|
|
||||||
async function get(db: Database, id: string): Promise<any> {
|
async function get<T extends Document>(db: Database, id: string): Promise<T> {
|
||||||
const cache = await getCache()
|
const cache = await getCache()
|
||||||
const cacheKey = makeCacheKey(db, id)
|
const cacheKey = makeCacheKey(db, id)
|
||||||
let cacheItem: CacheItem = await cache.get(cacheKey)
|
let cacheItem: CacheItem<T> = await cache.get(cacheKey)
|
||||||
if (!cacheItem) {
|
if (!cacheItem) {
|
||||||
const doc = await db.get(id)
|
const doc = await db.get<T>(id)
|
||||||
cacheItem = makeCacheItem(doc)
|
cacheItem = makeCacheItem(doc)
|
||||||
await cache.store(cacheKey, cacheItem)
|
await cache.store(cacheKey, cacheItem)
|
||||||
}
|
}
|
||||||
|
@ -123,8 +126,8 @@ export class Writethrough {
|
||||||
return put(this.db, doc, writeRateMs)
|
return put(this.db, doc, writeRateMs)
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(id: string) {
|
async get<T extends Document>(id: string) {
|
||||||
return get(this.db, id)
|
return get<T>(this.db, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
async remove(docOrId: any, rev?: any) {
|
async remove(docOrId: any, rev?: any) {
|
||||||
|
|
|
@ -137,7 +137,6 @@ export async function doWithLock<T>(
|
||||||
const result = await task()
|
const result = await task()
|
||||||
return { executed: true, result }
|
return { executed: true, result }
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
logWarn(`lock type: ${opts.type} error`, e)
|
|
||||||
// lock limit exceeded
|
// lock limit exceeded
|
||||||
if (e.name === "LockError") {
|
if (e.name === "LockError") {
|
||||||
if (opts.type === LockType.TRY_ONCE) {
|
if (opts.type === LockType.TRY_ONCE) {
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 056c2093dbc93d9a10ea9f5050c84a84edd8100c
|
Subproject commit 992486c10044a7495496b97bdf5f454d4020bfba
|
Loading…
Reference in New Issue