Skip to content

Commit

Permalink
feat(helper): ⚡ set redis expire time to 5 days
Browse files Browse the repository at this point in the history
add comments to redis functions; make consistent returns
  • Loading branch information
djdembeck committed Jan 26, 2023
1 parent b09808e commit d65dbeb
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions src/helpers/database/redis/RedisHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,28 @@ export default class RedisHelper {
return parsed
}

async deleteOne() {
/**
* Delete single key from Redis
* @returns {boolean} - True if deleted, false if not
* @throws {Error} - If there's an error
*/
async deleteOne(): Promise<boolean | undefined> {
try {
const deleted = await this.instance?.del(this.key)
return deleted
if (deleted === undefined) return undefined
return deleted === 1
} catch (error) {
const message = getErrorMessage(error)
console.error(message)
throw new Error(ErrorMessageRedisDelete(this.key))
}
}

/**
* Find one key from Redis
* @returns {ParsedObject | undefined} - ParsedObject if found, undefined if not
* @throws {Error} - If there's an error
*/
async findOne(): Promise<ParsedObject | undefined> {
try {
const found = await this.instance?.get(this.key)
Expand All @@ -46,7 +57,13 @@ export default class RedisHelper {
}
}

async findOrCreate(data: ParsedObject | undefined) {
/**
* Find one key from Redis, if not found, create it
* @param {ParsedObject | undefined} data - Data to create if not found
* @returns {ParsedObject | undefined} - ParsedObject if found, undefined if not
* @throws {Error} - If there's an error
*/
async findOrCreate(data: ParsedObject | undefined): Promise<ParsedObject | undefined> {
const found = await this.findOne()
// Return if found
if (found) return found
Expand All @@ -57,9 +74,29 @@ export default class RedisHelper {
}
}

async setOne(data: ParsedObject) {
/**
* Set TTL expiration to 5 days
* @returns {void}
*/
async setExpiration(): Promise<void> {
try {
await this.instance?.expire(this.key, 432000)
} catch (error) {
const message = getErrorMessage(error)
console.error(message)
}
}

/**
* Set one key in Redis
* @param {ParsedObject} data - Data to set
* @returns {string | undefined} - Status if set, null if not
* @throws {Error} - If there's an error
*/
async setOne(data: ParsedObject): Promise<string | undefined> {
try {
const set = await this.instance?.set(this.key, JSON.stringify(data, null, 2))
this.setExpiration()
return set
} catch (error) {
const message = getErrorMessage(error)
Expand Down

0 comments on commit d65dbeb

Please sign in to comment.