-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed Map Store the default store, vitest added (Fixed #7)
- Loading branch information
1 parent
a960be3
commit 8065aae
Showing
3 changed files
with
47 additions
and
1 deletion.
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,36 @@ | ||
import OTPStore from "./otp_store"; | ||
|
||
class MapStore extends OTPStore { | ||
private _otpMap: Map<string, [number, Date]>; | ||
|
||
constructor() { | ||
super() | ||
this._otpMap = new Map<string, [number, Date]>(); | ||
} | ||
|
||
async get(key: string): Promise<number> { | ||
return (this._otpMap.get(key) ?? [0])[0]; | ||
} | ||
|
||
async set(key: string, value: number, ttl?: number): Promise<string | null> { | ||
try { | ||
this._otpMap.set(key, [value, new Date(Date.now() + (ttl ?? 0))]); | ||
return "OK" | ||
} | ||
catch (e) { | ||
return String(e); | ||
} | ||
} | ||
|
||
async del(key: string): Promise<number | null> { | ||
try { | ||
this._otpMap.delete(key); | ||
return 1; | ||
} | ||
catch (e) { | ||
return -1 | ||
} | ||
} | ||
} | ||
|
||
export default MapStore; |
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,10 @@ | ||
import { expect, test } from 'vitest' | ||
import RedisStore from '../../otp_store/redis_store' | ||
import MapStore from '../../otp_store/map_store' | ||
|
||
test('Test Map Store', async () => { | ||
const mapStore: MapStore = new MapStore(); | ||
|
||
mapStore.set('test', 123, 60).then(() => { }); | ||
mapStore.get('test').then(value => { expect(value).toBe(123) }); | ||
}) |
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