mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-06-05 17:14:40 -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
|
@ -2,7 +2,7 @@ import moment from 'moment'
|
|||
import React from 'react'
|
||||
import { ListGroup } from 'react-bootstrap'
|
||||
import { Trans } from 'react-i18next'
|
||||
import { RevisionListEntry } from '../../../../api/revisions'
|
||||
import { RevisionListEntry } from '../../../../api/revisions/types'
|
||||
import { UserResponse } from '../../../../api/users/types'
|
||||
import { ForkAwesomeIcon } from '../../../common/fork-awesome/fork-awesome-icon'
|
||||
import { UserAvatar } from '../../../common/user-avatar/user-avatar'
|
||||
|
|
|
@ -4,7 +4,8 @@ import ReactDiffViewer, { DiffMethod } from 'react-diff-viewer'
|
|||
import { Trans, useTranslation } from 'react-i18next'
|
||||
import { useSelector } from 'react-redux'
|
||||
import { useParams } from 'react-router'
|
||||
import { getAllRevisions, getRevision, Revision, RevisionListEntry } from '../../../../api/revisions'
|
||||
import { getAllRevisions, getRevision } from '../../../../api/revisions'
|
||||
import { Revision, RevisionListEntry } from '../../../../api/revisions/types'
|
||||
import { UserResponse } from '../../../../api/users/types'
|
||||
import { ApplicationState } from '../../../../redux'
|
||||
import { CommonModal, CommonModalProps } from '../../../common/modals/common-modal'
|
||||
|
@ -21,7 +22,6 @@ export const RevisionModal: React.FC<CommonModalProps & RevisionButtonProps> = (
|
|||
const [selectedRevision, setSelectedRevision] = useState<Revision | null>(null)
|
||||
const [error, setError] = useState(false)
|
||||
const revisionAuthorListMap = useRef(new Map<number, UserResponse[]>())
|
||||
const revisionCacheMap = useRef(new Map<number, Revision>())
|
||||
const darkModeEnabled = useSelector((state: ApplicationState) => state.darkMode.darkMode)
|
||||
const { id } = useParams<{ id: string }>()
|
||||
|
||||
|
@ -42,14 +42,8 @@ export const RevisionModal: React.FC<CommonModalProps & RevisionButtonProps> = (
|
|||
if (selectedRevisionTimestamp === null) {
|
||||
return
|
||||
}
|
||||
const cacheEntry = revisionCacheMap.current.get(selectedRevisionTimestamp)
|
||||
if (cacheEntry) {
|
||||
setSelectedRevision(cacheEntry)
|
||||
return
|
||||
}
|
||||
getRevision(id, selectedRevisionTimestamp).then(fetchedRevision => {
|
||||
setSelectedRevision(fetchedRevision)
|
||||
revisionCacheMap.current.set(selectedRevisionTimestamp, fetchedRevision)
|
||||
}).catch(() => setError(true))
|
||||
}, [selectedRevisionTimestamp, id])
|
||||
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
import { Revision } from '../../../../api/revisions'
|
||||
import { Revision } from '../../../../api/revisions/types'
|
||||
import { getUserById } from '../../../../api/users'
|
||||
import { UserResponse } from '../../../../api/users/types'
|
||||
|
||||
const userResponseCache = new Map<string, UserResponse>()
|
||||
|
||||
export const downloadRevision = (noteId: string, revision: Revision | null): void => {
|
||||
if (!revision) {
|
||||
return
|
||||
|
@ -23,15 +21,9 @@ export const getUserDataForRevision = (authors: string[]): UserResponse[] => {
|
|||
if (index > 9) {
|
||||
return
|
||||
}
|
||||
const cacheEntry = userResponseCache.get(author)
|
||||
if (cacheEntry) {
|
||||
users.push(cacheEntry)
|
||||
return
|
||||
}
|
||||
getUserById(author)
|
||||
.then(userData => {
|
||||
users.push(userData)
|
||||
userResponseCache.set(author, userData)
|
||||
})
|
||||
.catch((error) => console.error(error))
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue