mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 17:55:17 -04:00
Add caching of user-data (#568)
* Add caching of user-data for 600 seconds * Make cache-entry interface commonly usable * Extract revision types * Remove revision-cache rule * Use seconds as cache-time interval (Date.now uses milliseconds) * Fix import error * Extract cache logic into common cache-class * Add cache class that was forgotten to commit in last commit * Start adding unit tests * Fix bug detected during unit-testing * Add unit tests for cache * Made entry-limit test more explicit * Renamed files to lower-case starting letter
This commit is contained in:
parent
0f31c3b0b4
commit
091b225672
8 changed files with 156 additions and 31 deletions
|
@ -1,23 +1,21 @@
|
|||
import { Cache } from '../../components/common/cache/cache'
|
||||
import { defaultFetchConfig, expectResponseCode, getApiUrl } from '../utils'
|
||||
import { Revision, RevisionListEntry } from './types'
|
||||
|
||||
export interface Revision {
|
||||
content: string
|
||||
timestamp: number
|
||||
authors: string[]
|
||||
}
|
||||
|
||||
export interface RevisionListEntry {
|
||||
timestamp: number
|
||||
length: number
|
||||
authors: string[]
|
||||
}
|
||||
const revisionCache = new Cache<string, Revision>(3600)
|
||||
|
||||
export const getRevision = async (noteId: string, timestamp: number): Promise<Revision> => {
|
||||
const cacheKey = `${noteId}:${timestamp}`
|
||||
if (revisionCache.has(cacheKey)) {
|
||||
return revisionCache.get(cacheKey)
|
||||
}
|
||||
const response = await fetch(getApiUrl() + `/notes/${noteId}/revisions/${timestamp}`, {
|
||||
...defaultFetchConfig
|
||||
})
|
||||
expectResponseCode(response)
|
||||
return await response.json() as Promise<Revision>
|
||||
const revisionData = await response.json() as Revision
|
||||
revisionCache.put(cacheKey, revisionData)
|
||||
return revisionData
|
||||
}
|
||||
|
||||
export const getAllRevisions = async (noteId: string): Promise<RevisionListEntry[]> => {
|
||||
|
|
11
src/api/revisions/types.d.ts
vendored
Normal file
11
src/api/revisions/types.d.ts
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
export interface Revision {
|
||||
content: string
|
||||
timestamp: number
|
||||
authors: string[]
|
||||
}
|
||||
|
||||
export interface RevisionListEntry {
|
||||
timestamp: number
|
||||
length: number
|
||||
authors: string[]
|
||||
}
|
|
@ -1,10 +1,18 @@
|
|||
import { Cache } from '../../components/common/cache/cache'
|
||||
import { defaultFetchConfig, expectResponseCode, getApiUrl } from '../utils'
|
||||
import { UserResponse } from './types'
|
||||
|
||||
const cache = new Cache<string, UserResponse>(600)
|
||||
|
||||
export const getUserById = async (userid: string): Promise<UserResponse> => {
|
||||
if (cache.has(userid)) {
|
||||
return cache.get(userid)
|
||||
}
|
||||
const response = await fetch(`${getApiUrl()}/users/${userid}`, {
|
||||
...defaultFetchConfig
|
||||
})
|
||||
expectResponseCode(response)
|
||||
return (await response.json()) as UserResponse
|
||||
const userData = (await response.json()) as UserResponse
|
||||
cache.put(userid, userData)
|
||||
return userData
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue