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:
Erik Michelson 2020-09-30 23:37:57 +02:00 committed by GitHub
parent 0f31c3b0b4
commit 091b225672
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 156 additions and 31 deletions

View file

@ -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))
})