Files
omnivore/packages/api/src/datasources/reading_progress_data_source.ts
Jackson Harper c47a720fe4 Do not cache on write, so the most updated item is always available on read
This is mostly an issue on tests where all the queries can be run
in a single process, but if we cache on write, it means the next
read will just have the cached value, instead of the calculated
maximum value.
2024-02-01 09:59:42 +08:00

43 lines
1.2 KiB
TypeScript

import { redisDataSource } from '../redis_data_source'
import {
ReadingProgressCacheItem,
fetchCachedReadingPosition,
keyForCachedReadingPosition,
pushCachedReadingPosition,
} from '../services/cached_reading_position'
export class ReadingProgressDataSource {
private cacheItems: { [id: string]: ReadingProgressCacheItem } = {}
async getReadingProgress(
uid: string,
libraryItemID: string
): Promise<ReadingProgressCacheItem | undefined> {
const cacheKey = `omnivore:reading-progress:${uid}:${libraryItemID}`
const cached = this.cacheItems[cacheKey]
if (cached) {
return cached
}
return fetchCachedReadingPosition(uid, libraryItemID)
}
async updateReadingProgress(
uid: string,
libraryItemID: string,
progress: {
readingProgressPercent: number
readingProgressTopPercent: number | undefined
readingProgressAnchorIndex: number | undefined
}
): Promise<ReadingProgressCacheItem | undefined> {
const cacheItem: ReadingProgressCacheItem = {
uid,
libraryItemID,
updatedAt: new Date().toISOString(),
...progress,
}
await pushCachedReadingPosition(uid, libraryItemID, cacheItem)
return fetchCachedReadingPosition(uid, libraryItemID)
}
}