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.
43 lines
1.2 KiB
TypeScript
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)
|
|
}
|
|
}
|