Last active
May 18, 2022 23:25
-
-
Save lucasraziel/8929e8b05be4ff8f9db39024521e980f to your computer and use it in GitHub Desktop.
ioredis.ts in Usando NPM Package para criar uma Arquitetura Limpa - Parte 3
This file contains hidden or 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
/* eslint-disable no-restricted-syntax */ | |
import { FakeCacheProvider } from '@fvsystem/cache-template'; | |
import { RedisOptions } from 'ioredis'; | |
class Redis { | |
fakeCache: FakeCacheProvider; | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
constructor(options: RedisOptions) { | |
this.fakeCache = new FakeCacheProvider(); | |
} | |
async set( | |
key: string, | |
value: string, | |
ttlType?: string, | |
ttl?: number | |
): Promise<'OK'> { | |
if (ttl) { | |
await this.fakeCache.save({ key, data: value }, '', { | |
ttlInSeconds: ttl, | |
}); | |
return 'OK'; | |
} | |
await this.fakeCache.save({ key, data: value }, ''); | |
return 'OK'; | |
} | |
async get(key: string): Promise<string | null> { | |
const value = await this.fakeCache.recover<string>(key, ''); | |
return value || null; | |
} | |
async del(key: string): Promise<void> { | |
await this.fakeCache.invalidate(key, ''); | |
} | |
} | |
export default Redis; | |
export { Redis, RedisOptions }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment