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
import { Data } from '@fvsystem/cache-template'; | |
import RedisCacheProvider from '../src/provider/RedisCacheProvider'; | |
jest.mock('ioredis'); | |
let redisCacheProvider: RedisCacheProvider; | |
describe('RedisCacheProvider', () => { | |
beforeEach(() => { | |
redisCacheProvider = new RedisCacheProvider({}); |
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(); |
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
import RedisCacheProvider from './provider/RedisCacheProvider'; | |
export default RedisCacheProvider; |
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
import { CacheTemplate, Data, SaveOptions } from '@fvsystem/cache-template'; | |
import Redis, { RedisOptions } from 'ioredis'; | |
import debug from 'debug'; | |
const log = debug('fvsystem-template-cache'); | |
export default class RedisCacheProvider implements CacheTemplate { | |
private client: Redis; | |
constructor(options: RedisOptions) { |
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
import Data from './dto/data'; | |
import SaveOptions from './dto/saveOptions'; | |
import CacheTemplate from './template/CacheTemplate'; | |
import FakeCacheProvider from './fake/FakeCacheProvider'; | |
export { Data, CacheTemplate, FakeCacheProvider, SaveOptions }; | |
export default CacheTemplate; |
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
it('should be able to remove value after some time', async () => { | |
const data: Data<number> = { | |
key: 'test', | |
data: 1, | |
}; | |
jest.useFakeTimers(); | |
fakeCacheProvider.save(data, 'app:', { ttlInSeconds: 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
async save( | |
value: Data, | |
prefix: string, | |
options?: SaveOptions | |
): Promise<void> { | |
this.cache.set(`${prefix}${value.key}`, value.data); | |
if (options?.ttlInSeconds) { | |
setTimeout(() => { | |
this.invalidate(value.key, prefix); | |
}, options.ttlInSeconds * 1000); |
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
import Data from '../dto/data'; | |
import SaveOptions from '../dto/saveOptions'; | |
export default interface ICacheProvider { | |
save(value: Data, prefix: string, options?: SaveOptions): Promise<void>; | |
invalidate(key: string, prefix: string): Promise<void>; | |
recover<T = unknown>(key: string, prefix: string): Promise<T | null>; | |
} |
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
export default interface SaveOptions { | |
ttlInSeconds?: number; | |
} |
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
import { FakeCacheProvider, Data } from '../src/index'; | |
let fakeCacheProvider: FakeCacheProvider; | |
describe('Tests', () => { | |
beforeEach(() => { | |
fakeCacheProvider = new FakeCacheProvider(); | |
}); | |
it('should be abble to add and recover value', async () => { | |
const data: Data<number> = { |
NewerOlder