Skip to content

Commit

Permalink
Merge b9fb1fd into 44d9ac1
Browse files Browse the repository at this point in the history
  • Loading branch information
pasupulaphani committed Mar 14, 2021
2 parents 44d9ac1 + b9fb1fd commit 6a3c364
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 49 deletions.
54 changes: 31 additions & 23 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -82,7 +82,7 @@
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"ts-jest": "^26.5.0",
"typedoc": "^0.20.22",
"typedoc": "^0.20.32",
"typescript": "^4.1.3"
}
}
32 changes: 16 additions & 16 deletions src/RedisStore.ts
Expand Up @@ -13,7 +13,7 @@ const debug = Debug('nodeRedisStore')
* RedisStore
*/
class RedisStore extends RedisPool {
defaulTtlInS: number | undefined
defaultTtlInS: number | undefined

deleteScriptPromise: Promise<any> | null = null

Expand All @@ -24,55 +24,55 @@ class RedisStore extends RedisPool {
* @param
* @param
* @param options.logger - Inject your custom logger
* @param options.defaulTtlInS - Number of seconds to store by default
* @param options.defaultTtlInS - Number of seconds to store by default
*/
constructor({
name,
redisOptions,
poolOptions,
logger,
defaulTtlInS
defaultTtlInS
}: {
name?: string
redisOptions: RedisOptions
poolOptions?: PoolOptions
logger?: Logger
defaulTtlInS?: number
defaultTtlInS?: number
}) {
super({
name: name || `redisStore-${genRandomStr()}`,
redisOptions,
poolOptions,
logger: createLogger(logger)
})
this.defaulTtlInS = validatedTtl(defaulTtlInS)
this.defaultTtlInS = validatedTtl(defaultTtlInS)
}

/**
* Return the defaulTtlInS
* @returns defaulTtlInS
* Return the defaultTtlInS
* @returns defaultTtlInS
*/
getDefaultTtlInS(): number | undefined {
return this.defaulTtlInS
return this.defaultTtlInS
}

/**
* Sets the defaulTtlInS
* Sets the defaultTtlInS
* @param ttl
* @returns defaulTtlInS
* @returns defaultTtlInS
*/
setDefaultTtlInS(ttl: number): number | undefined {
this.defaulTtlInS = validatedTtl(ttl)
return this.defaulTtlInS
this.defaultTtlInS = validatedTtl(ttl)
return this.defaultTtlInS
}

/**
* Unsets the defaulTtlInS
* Unsets the defaultTtlInS
* @param ttl
* @returns true
*/
unsetDefaultTtlInS(): boolean {
this.defaulTtlInS = undefined
this.defaultTtlInS = undefined
return true
}

Expand Down Expand Up @@ -117,7 +117,7 @@ class RedisStore extends RedisPool {
? JSON.stringify(value)
: value

const ttl = validatedTtl(ttlInSeconds, this.defaulTtlInS)
const ttl = validatedTtl(ttlInSeconds, this.defaultTtlInS)
if (ttl) {
return super.sendCommand('setex', [key, ttl, str])
}
Expand All @@ -141,7 +141,7 @@ class RedisStore extends RedisPool {
Array.isArray(value) || isJSON(value, true)
? JSON.stringify(value)
: value
const ttl = validatedTtl(ttlInSeconds, this.defaulTtlInS)
const ttl = validatedTtl(ttlInSeconds, this.defaultTtlInS)

let result = await super.sendCommand('getset', [key, str])
try {
Expand Down
4 changes: 2 additions & 2 deletions src/redisCache.test.ts
Expand Up @@ -142,12 +142,12 @@ describe('redisCache', () => {
const fnToFail = () => {
throw new Error('not be called')
}
const defaulTtlInS = 10
const defaultTtlInS = 10

beforeAll(() => deleteAll())
afterAll(() => unsetDefaultTtlInS())

afterEach(() => setDefaultTtlInS(defaulTtlInS))
afterEach(() => setDefaultTtlInS(defaultTtlInS))

test("set if key doesn't exist", async () => {
const localKey = genRandomStr()
Expand Down
14 changes: 7 additions & 7 deletions src/redisCache.ts
Expand Up @@ -16,7 +16,7 @@ let store: RedisStore
* @param options.redisOptions - opts from [node_redis#options-object-properties]{@link https://github.com/NodeRedis/node_redis#options-object-properties}
* @param options.poolOptions - opts from [node-pool#createpool]{@link https://github.com/coopernurse/node-pool#createpool}
* @param options.logger - Inject your custom logger
* @param options.defaulTtlInS - Number of seconds to store by default
* @param options.defaultTtlInS - Number of seconds to store by default
*/
export const init = (options: {
/** Name your cache store */
Expand All @@ -28,16 +28,16 @@ export const init = (options: {
/** Inject your custom logger */
logger?: Logger
/** Number of seconds to store by default */
defaulTtlInS?: number
defaultTtlInS?: number
}) => {
if (store) return

const { name, logger, defaulTtlInS } = options || {}
const { name, logger, defaultTtlInS } = options || {}
store = new RedisStore({
...options,
name: name || `redisCache-${genRandomStr()}`,
logger: createLogger(logger),
defaulTtlInS: validatedTtl(defaulTtlInS)
defaultTtlInS: validatedTtl(defaultTtlInS)
})
}

Expand Down Expand Up @@ -70,20 +70,20 @@ export const getPoolOptions = (): PoolOptions => getStore().getPoolOptions()
export const getStatus = (): RedisPoolStatus => getStore().status()

/**
* Return the defaulTtlInS
* Return the defaultTtlInS
*/
export const getDefaultTtlInS = (): number | undefined =>
getStore().getDefaultTtlInS()

/**
* Sets the defaulTtlInS
* Sets the defaultTtlInS
* @param ttl - new default ttl in seconds
*/
export const setDefaultTtlInS = (ttl: number): number | undefined =>
getStore().setDefaultTtlInS(ttl)

/**
* Unsets the defaulTtlInS
* Unsets the defaultTtlInS
*/
export const unsetDefaultTtlInS = (): boolean => getStore().unsetDefaultTtlInS()

Expand Down

0 comments on commit 6a3c364

Please sign in to comment.