Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement(caching): implement client-side caching for listing and p…
…rofile data (#52)
- Loading branch information
Showing
4 changed files
with
120 additions
and
11 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,42 @@ | ||
import { Cache } from './Cache' | ||
|
||
test('Properly initialize initial object state', () => { | ||
const cache = new Cache() | ||
expect(JSON.stringify(cache)).toEqual(JSON.stringify({ cache: {} })) | ||
}) | ||
|
||
test('Add cache entry', () => { | ||
const cache = new Cache() | ||
const realDateNow = Date.now.bind(global.Date) | ||
const dateNowStub = jest.fn(() => 3600000) | ||
global.Date.now = dateNowStub | ||
cache.store('test-key', 'test-value') | ||
|
||
expect(cache.retrieve('test-key')).toEqual('test-value') | ||
expect(JSON.stringify(cache)).toEqual( | ||
JSON.stringify({ | ||
cache: { | ||
'test-key': { | ||
data: 'test-value', | ||
expiresOn: 3600000 + 3600000, | ||
}, | ||
}, | ||
}) | ||
) | ||
}) | ||
|
||
test('Cache expiration', () => { | ||
const cache = new Cache() | ||
const realDateNow = Date.now.bind(global.Date) | ||
const dateNowStub = jest.fn(() => 3600000) | ||
global.Date.now = dateNowStub | ||
cache.store('test-key', 'test-value') | ||
|
||
const dateExpireStub = jest.fn(() => 3600000 + 3600000) | ||
global.Date.now = dateExpireStub | ||
|
||
expect(cache.hasExpired('test-key')).toBe(true) | ||
expect(cache.retrieve('test-key')).toBe(false) | ||
|
||
global.Date.now = realDateNow | ||
}) |
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,50 @@ | ||
interface CacheInterface { | ||
[key: string]: CacheData | ||
} | ||
|
||
interface CacheData { | ||
data: any | ||
expiresOn: number | ||
} | ||
|
||
class Cache { | ||
private cache: CacheInterface = {} | ||
|
||
constructor(props?) { | ||
if (props) { | ||
Object.assign(this, props) | ||
} | ||
} | ||
|
||
public store(key: string, data: any) { | ||
const expiresOn = Date.now() + 3600000 | ||
this.cache[key] = { data, expiresOn } | ||
} | ||
|
||
public retrieve(key: string) { | ||
if (this.cache[key]) { | ||
if (this.hasExpired(key)) { | ||
delete this.cache[key] | ||
return false | ||
} | ||
/** | ||
* Cloning object to avoid unintentional mutations | ||
*/ | ||
return JSON.parse(JSON.stringify(this.cache[key].data)) | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
public delete(key: string) { | ||
delete this.cache[key] | ||
} | ||
|
||
public hasExpired(key: string) { | ||
const cacheData = this.cache[key] | ||
return cacheData.expiresOn <= Date.now() | ||
} | ||
} | ||
|
||
const cacheInstance = new Cache() | ||
export { cacheInstance, Cache } |
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