-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
312 additions
and
47 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
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
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
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,15 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CacheConfigService } from './cache-config.service'; | ||
|
||
describe('CacheConfigService', () => { | ||
let service: CacheConfigService; | ||
beforeAll(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CacheConfigService], | ||
}).compile(); | ||
service = module.get<CacheConfigService>(CacheConfigService); | ||
}); | ||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,34 @@ | ||
import { CacheModuleOptions, CacheOptionsFactory, Injectable } from '@nestjs/common'; | ||
// REf: https://github.com/kyle-mccarthy/nest-next-starter/tree/master/src/cache | ||
@Injectable() | ||
export class CacheConfigService implements CacheOptionsFactory { | ||
constructor() {} | ||
|
||
/** | ||
* Example retry strategy for when redis is used for the cache | ||
* This example is only compatible with cache-manager-redis-store because it used node_redis | ||
*/ | ||
public retryStrategy() { | ||
return { | ||
retry_strategy: (options: any) => { | ||
if (options.error && options.error.code === 'ECONNREFUSED') { | ||
return new Error('The server refused the connection'); | ||
} | ||
if (options.total_retry_time > 1000 * 60) { | ||
return new Error('Retry time exhausted'); | ||
} | ||
if (options.attempt > 2) { | ||
return new Error('Max attempts exhausted'); | ||
} | ||
return Math.min(options.attempt * 100, 3000); | ||
}, | ||
}; | ||
} | ||
|
||
public createCacheOptions(): CacheModuleOptions { | ||
return { | ||
ttl: 5, // seconds | ||
max: 10, // maximum number of items in cache | ||
}; | ||
} | ||
} |
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,16 @@ | ||
import { CacheModule as NestCacheModule, Global, Module } from '@nestjs/common'; | ||
import { CacheConfigService } from './cache-config.service'; | ||
import { CacheService } from './cache.service'; | ||
|
||
@Global() | ||
@Module({ | ||
imports: [ | ||
NestCacheModule.registerAsync({ | ||
useClass: CacheConfigService, | ||
inject: [CacheConfigService], | ||
}), | ||
], | ||
providers: [CacheConfigService, CacheService], | ||
exports: [CacheService], | ||
}) | ||
export class CacheModule {} |
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,57 @@ | ||
import { CacheService, ICacheManager } from './cache.service'; | ||
|
||
describe('CacheService', () => { | ||
let service: CacheService; | ||
|
||
let store: any = {}; | ||
|
||
const Manager = jest.fn<ICacheManager>().mockImplementation(() => { | ||
return { | ||
get: jest.fn((key: string) => store[key]), | ||
set: jest.fn((key: string, value: any, options?: { ttl: number }) => { | ||
store[key] = value; | ||
}), | ||
}; | ||
}); | ||
|
||
const manager = new Manager(); | ||
|
||
beforeAll(async () => { | ||
service = new CacheService(manager); | ||
}); | ||
|
||
beforeEach(async () => { | ||
store = {}; | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
it('should call set', () => { | ||
service.set('test', 'test'); | ||
|
||
expect(manager.set).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should call get', () => { | ||
const res = service.get('test'); | ||
|
||
expect(res).toBeUndefined(); | ||
expect(manager.get).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should get set value', () => { | ||
store.testKey = 'test value'; | ||
|
||
expect(service.get('testKey')).toEqual('test value'); | ||
}); | ||
|
||
it('set should overwrite existing value', () => { | ||
store.current = 0; | ||
|
||
expect(store.current).toEqual(0); | ||
service.set('current', 1); | ||
expect(store.current).toEqual(1); | ||
}); | ||
}); |
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,24 @@ | ||
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common'; | ||
|
||
export interface ICacheManager { | ||
store: any; | ||
get(key: string): any; | ||
set(key: string, value: string, options?: { ttl: number }): any; | ||
} | ||
|
||
@Injectable() | ||
export class CacheService { | ||
private cache!: ICacheManager; | ||
|
||
constructor(@Inject(CACHE_MANAGER) cache: ICacheManager) { | ||
this.cache = cache; | ||
} | ||
|
||
public get(key: string): Promise<any> { | ||
return this.cache.get(key); | ||
} | ||
|
||
public set(key: string, value: any, options?: { ttl: number }): Promise<any> { | ||
return this.cache.set(key, value, options); | ||
} | ||
} |
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,2 @@ | ||
export * from './cache.service'; | ||
export * from './cache.module'; |
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
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
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 { DeepPartial, DeleteResult, FindConditions, FindManyOptions, FindOneOptions, UpdateResult } from 'typeorm'; | ||
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity'; | ||
|
||
export interface ICrudService<T> { | ||
getAll(filter?: FindManyOptions<T>): Promise<[T[], number]>; | ||
getOne(id: string | number | FindOneOptions<T> | FindConditions<T>, options?: FindOneOptions<T>): Promise<T>; | ||
create(entity: DeepPartial<T>, ...options: any[]): Promise<T>; | ||
update(id: any, entity: QueryDeepPartialEntity<T>, ...options: any[]): Promise<UpdateResult | T>; | ||
delete(id: any, ...options: any[]): Promise<DeleteResult>; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.