-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ttl and createdAt instead of maxAge and storage takes care …
…of staled entries
- Loading branch information
1 parent
b45fd54
commit be5ee1e
Showing
13 changed files
with
152 additions
and
34 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
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,6 @@ | ||
import { MemoryStorage } from '../../src/storage'; | ||
import { testStorage } from './storages'; | ||
|
||
describe('tests common storages', () => { | ||
testStorage('memory', MemoryStorage); | ||
}); |
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,60 @@ | ||
import { CacheStorage } from '../../src/storage/types'; | ||
|
||
export function testStorage(name: string, Storage: { new (): CacheStorage }) { | ||
it(`tests ${name} storage methods`, async () => { | ||
const storage = new Storage(); | ||
|
||
const result = await storage.get('key'); | ||
|
||
expect(result).not.toBeNull(); | ||
expect(result.state).toBe('empty'); | ||
|
||
await storage.set('key', { | ||
state: 'cached', | ||
createdAt: Date.now(), | ||
ttl: 1000 * 60 * 5, | ||
data: { body: 'data', headers: {} } | ||
}); | ||
|
||
const result2 = await storage.get('key'); | ||
|
||
expect(result2).not.toBeNull(); | ||
expect(result2.state).toBe('cached'); | ||
expect(result2.data?.body).toBe('data'); | ||
|
||
await storage.remove('key'); | ||
|
||
const result3 = await storage.get('key'); | ||
|
||
expect(result3).not.toBeNull(); | ||
expect(result3.state).toBe('empty'); | ||
}); | ||
|
||
it(`tests ${name} storage staling`, async () => { | ||
jest.useFakeTimers('modern'); | ||
const storage = new Storage(); | ||
|
||
await storage.set('key', { | ||
state: 'cached', | ||
createdAt: Date.now(), | ||
ttl: 1000 * 60 * 5, // 5 Minutes | ||
data: { body: 'data', headers: {} } | ||
}); | ||
|
||
const result = await storage.get('key'); | ||
|
||
expect(result).not.toBeNull(); | ||
expect(result.state).toBe('cached'); | ||
expect(result.data?.body).toBe('data'); | ||
|
||
// Advance 6 minutes in time | ||
jest.setSystemTime(Date.now() + 1000 * 60 * 6); | ||
|
||
const result2 = await storage.get('key'); | ||
|
||
expect(result2).not.toBeNull(); | ||
expect(result2.state).toBe('empty'); | ||
|
||
jest.useRealTimers(); | ||
}); | ||
} |
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,11 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { LocalCacheStorage, SessionCacheStorage } from '../../src/storage'; | ||
import { testStorage } from './storages'; | ||
|
||
describe('tests web storages', () => { | ||
testStorage('local-storage', LocalCacheStorage); | ||
testStorage('session-storage', SessionCacheStorage); | ||
}); |
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