Skip to content

Commit

Permalink
style(): minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 17, 2020
1 parent d8c4372 commit 8bd7097
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class ConfigModule {
provide: ConfigService,
useFactory: (configService: ConfigService) => {
if (options.cache) {
configService.cachingEnabled = true;
configService.isCacheEnabled = true;
}
return configService;
},
Expand Down
15 changes: 7 additions & 8 deletions lib/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inject, Injectable, Optional } from '@nestjs/common';
import get from 'lodash.get';
import set from 'lodash.set';
import has from 'lodash.has';
import set from 'lodash.set';
import { isUndefined } from 'util';
import {
CONFIGURATION_TOKEN,
Expand All @@ -11,16 +11,16 @@ import { NoInferType } from './types';

@Injectable()
export class ConfigService<K = Record<string, any>> {
get cachingEnabled(): boolean {
return this._cachingEnabled;
get isCacheEnabled(): boolean {
return this._isCacheEnabled;
}

set cachingEnabled(value: boolean) {
this._cachingEnabled = value;
set isCacheEnabled(value: boolean) {
this._isCacheEnabled = value;
}

private readonly cache: K = {} as any;
private _cachingEnabled = false;
private _isCacheEnabled = false;

constructor(
@Optional()
Expand Down Expand Up @@ -53,11 +53,10 @@ export class ConfigService<K = Record<string, any>> {
*/
get<T = any>(propertyPath: keyof K, defaultValue?: T): T | undefined {
if (
this.cachingEnabled &&
this.isCacheEnabled &&
has(this.cache as Record<any, any>, propertyPath)
) {
const cachedValue = this.getFromCache(propertyPath, defaultValue);
/** if cached value was once set as undefined always return default value */
return !isUndefined(cachedValue) ? cachedValue : defaultValue;
}

Expand Down
5 changes: 2 additions & 3 deletions lib/interfaces/config-module-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { ConfigFactory } from './config-factory.interface';

export interface ConfigModuleOptions {
/**
* If "true" values from process.env are read
* only once, the cache will be used after that.
* This will improve the performance.
* If "true", values from the process.env object will be cached in the memory.
* This improves the overall application performance.
* See: https://github.com/nodejs/node/issues/3104
*/
cache?: boolean;
Expand Down

0 comments on commit 8bd7097

Please sign in to comment.