-
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 OTPStore abstract class with implementation of RedisStore
- Loading branch information
1 parent
49af6ae
commit e919024
Showing
5 changed files
with
130 additions
and
58 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,7 @@ | ||
abstract class OTPStore { | ||
abstract get(key: string): Promise<number>; | ||
abstract set(key: string, value: number, ttl?: number): Promise<string | null>; | ||
abstract del(key: string): Promise<number | null>; | ||
} | ||
|
||
export default OTPStore; |
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,28 @@ | ||
import OTPStore from './otp_store' | ||
import { RedisClientType, RedisFunctions, RedisModules, RedisScripts } from '@redis/client'; | ||
|
||
type RedisClient = RedisClientType<RedisFunctions, RedisModules, RedisScripts> | ||
|
||
class RedisStore extends OTPStore { | ||
private client: RedisClient; | ||
|
||
constructor(client: RedisClient) { | ||
super() | ||
this.client = client | ||
} | ||
|
||
async get(key: string): Promise<number> { | ||
const value = await this.client.get(key) | ||
return value ? parseInt(value) : 0 | ||
} | ||
|
||
async set(key: string, value: number, ttl = 60): Promise<string | null> { | ||
return await this.client.set(key, value.toString(), { EX: ttl }) | ||
} | ||
|
||
async del(key: string): Promise<number | null> { | ||
return await this.client.del(key) | ||
} | ||
} | ||
|
||
export default RedisStore |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.