Skip to content

Commit

Permalink
Completed OTPStore abstract class with implementation of RedisStore
Browse files Browse the repository at this point in the history
  • Loading branch information
MananGandhi1810 committed Jul 27, 2024
1 parent 49af6ae commit e919024
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 58 deletions.
58 changes: 0 additions & 58 deletions index.ts

This file was deleted.

7 changes: 7 additions & 0 deletions otp_store/otp_store.ts
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;
28 changes: 28 additions & 0 deletions otp_store/redis_store.ts
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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@
"@types/node": "^20.14.12",
"tsup": "^8.2.3",
"typescript": "^5.5.4"
},
"dependencies": {
"@redis/client": "^1.5.17",
"redis": "^4.6.15"
}
}
91 changes: 91 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e919024

Please sign in to comment.