-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #626 from namecheap/perf/fix-cpu-perf
perf: fix cpu usage in cache
- Loading branch information
Showing
41 changed files
with
628 additions
and
327 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
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 |
---|---|---|
|
@@ -9,3 +9,4 @@ node_modules | |
registry/lde/dbfiles | ||
.yalc | ||
yalc.lock | ||
.clinic |
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
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,65 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { BrowserCacheStorage } from './BrowserCacheStorage'; | ||
|
||
describe('BrowserCacheStorage', () => { | ||
let cacheStorage: BrowserCacheStorage; | ||
let mockLocalStorage: Storage; | ||
|
||
const sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(() => { | ||
// Mock the localStorage API | ||
mockLocalStorage = { | ||
getItem: sandbox.stub(), | ||
setItem: sandbox.stub(), | ||
removeItem: sandbox.stub(), | ||
clear: sandbox.stub(), | ||
} as unknown as Storage; | ||
|
||
cacheStorage = new BrowserCacheStorage(mockLocalStorage); | ||
}); | ||
|
||
afterEach(() => { | ||
// Restore the default sandbox here | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('getItem', () => { | ||
it('should return parsed data from localStorage if key exists', () => { | ||
const key = 'testKey'; | ||
const value = { data: 'testData' }; | ||
|
||
// Mock localStorage.getItem to return stringified data | ||
(mockLocalStorage.getItem as sinon.SinonStub).withArgs(key).returns(JSON.stringify(value)); | ||
|
||
const result = cacheStorage.getItem<typeof value>(key); | ||
|
||
sandbox.assert.calledWith(mockLocalStorage.getItem as sinon.SinonStub, key); | ||
expect(result).to.deep.equal(value); | ||
}); | ||
|
||
it('should return null if key does not exist in localStorage', () => { | ||
const key = 'missingKey'; | ||
|
||
// Mock localStorage.getItem to return null | ||
(mockLocalStorage.getItem as sinon.SinonStub).withArgs(key).returns(null); | ||
|
||
const result = cacheStorage.getItem(key); | ||
|
||
sandbox.assert.calledWith(mockLocalStorage.getItem as sinon.SinonStub, key); | ||
expect(result).to.be.null; | ||
}); | ||
}); | ||
|
||
describe('setItem', () => { | ||
it('should store stringified data in localStorage', () => { | ||
const key = 'testKey'; | ||
const value = { data: 'testData', cachedAt: Date.now() }; | ||
|
||
cacheStorage.setItem(key, value); | ||
|
||
sandbox.assert.calledWith(mockLocalStorage.setItem as sinon.SinonStub, key, JSON.stringify(value)); | ||
}); | ||
}); | ||
}); |
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,14 @@ | ||
import { CacheResult, CacheStorage } from '../../common/types/CacheWrapper'; | ||
|
||
export class BrowserCacheStorage implements CacheStorage { | ||
constructor(private readonly storage: Storage) {} | ||
|
||
getItem<T>(key: string): CacheResult<T> { | ||
const cache = this.storage.getItem(key); | ||
return cache ? JSON.parse(cache) : null; | ||
} | ||
|
||
setItem(key: string, cache: CacheResult<unknown>): void { | ||
this.storage.setItem(key, JSON.stringify(cache)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
import type { Logger } from 'ilc-plugins-sdk'; | ||
import type { IlcLogger } from 'ilc-plugins-sdk/browser'; | ||
import { DefaultCacheWrapper } from '../../common/DefaultCacheWrapper'; | ||
import { BrowserCacheStorage } from './BrowserCacheStorage'; | ||
import Registry from './Registry'; | ||
|
||
export const registryFactory = (logger: IlcLogger) => | ||
new Registry(new DefaultCacheWrapper(new BrowserCacheStorage(window.localStorage), logger as Logger)); |
Oops, something went wrong.