-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement folder caching in PermanentFilesystem
When calling the "navigate" method from PermanentFilesystem, save the result in a cache and try to fetch from cache when looking up the same folder later. This cache is not meant to actually reduce the number of network calls, but rather to provide a somewhat accurate temporary cached value while the browser is waiting for the API to respond back with the true value. As a result, the code is designed in such a way that any objects returned from the `getFolder` method may automatically after the function is called. PER-9463: Add caching to folder navigate calls
- Loading branch information
1 parent
e8c0b7a
commit 7bd51e3
Showing
6 changed files
with
186 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { FolderVO } from '@models/index'; | ||
import { FolderCache } from './folder-cache'; | ||
|
||
describe('FolderCache', () => { | ||
let cache: FolderCache; | ||
beforeEach(() => { | ||
cache = new FolderCache(); | ||
}); | ||
it('should exist', () => { | ||
expect(cache).toBeTruthy(); | ||
}); | ||
it('should return null if a folder is not cached', () => { | ||
cache.saveFolder(new FolderVO({ folder_linkId: 10000 })); | ||
|
||
expect(cache.getFolder({ folder_linkId: 1 })).toBeNull(); | ||
}); | ||
it('should be able to save to the cache', () => { | ||
const folder = new FolderVO({ folder_linkId: 1 }); | ||
cache.saveFolder(folder); | ||
|
||
expect(cache.getFolder({ folder_linkId: 1 })).toEqual(folder); | ||
}); | ||
it('should be able to update an existing cache value', () => { | ||
const folder = new FolderVO({ folder_linkId: 1 }); | ||
cache.saveFolder(folder); | ||
cache.saveFolder( | ||
new FolderVO({ folder_linkId: 1, displayName: 'Unit Test' }) | ||
); | ||
|
||
expect(cache.getFolder({ folder_linkId: 1 }).displayName).toBe('Unit Test'); | ||
}); | ||
|
||
describe('Folder Identifiers', () => { | ||
let folder: FolderVO; | ||
|
||
beforeEach(() => { | ||
folder = new FolderVO({ | ||
folderId: 1, | ||
folder_linkId: 10, | ||
archiveNbr: '1234-test', | ||
}); | ||
cache.saveFolder(folder); | ||
}); | ||
|
||
it('can look up by FolderId', () => { | ||
expect(cache.getFolder({ folderId: 1 })).toEqual(folder); | ||
}); | ||
|
||
it('can look up by folder_linkId', () => { | ||
expect(cache.getFolder({ folder_linkId: 10 })).toEqual(folder); | ||
}); | ||
|
||
it('can look up by ArchiveNbr', () => { | ||
expect(cache.getFolder({ archiveNbr: '1234-test' })).toEqual(folder); | ||
}); | ||
|
||
it('should prioritize folderId over folder_linkId', () => { | ||
expect(cache.getFolder({ folderId: 1, folder_linkId: Infinity })).toEqual( | ||
folder | ||
); | ||
}); | ||
|
||
it('should prioritize folder_linkId over archiveNbr', () => { | ||
expect( | ||
cache.getFolder({ folder_linkId: 10, archiveNbr: 'No Match' }) | ||
).toEqual(folder); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { FolderVO } from '@models/index'; | ||
import { FolderIdentifier } from './types/filesystem-identifier'; | ||
import { KeysOfUnion } from './types/keysofunion'; | ||
|
||
export class FolderCache { | ||
private folders: FolderVO[] = []; | ||
|
||
private fetchFromCache( | ||
query: FolderIdentifier, | ||
property: KeysOfUnion<FolderIdentifier> | ||
): FolderVO | undefined { | ||
if (property in query) { | ||
return this.folders.find((f) => f[property] === query[property]); | ||
} | ||
} | ||
|
||
public getFolder(folder: FolderIdentifier): FolderVO | null { | ||
return ( | ||
this.fetchFromCache(folder, 'folderId') || | ||
this.fetchFromCache(folder, 'folder_linkId') || | ||
this.fetchFromCache(folder, 'archiveNbr') || | ||
null | ||
); | ||
} | ||
|
||
public saveFolder(folder: FolderVO): void { | ||
const cachedFolder = this.getFolder(folder); | ||
if (cachedFolder) { | ||
Object.assign(cachedFolder, folder); | ||
} else { | ||
this.folders.push(folder); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Sourced from this Stack Overflow answer by user Titian Cernicova-Dragomir: | ||
// https://stackoverflow.com/a/49402091 | ||
export type KeysOfUnion<T> = T extends T ? keyof T : never; |