Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

ioredis

import IORedis from 'ioredis'
import { Msgpack } from '@libs/lib.msgpack'

export class Redis {
  private db: number

  constructor(db: number) {
    this.setDB(db)
  }

  private setDB(db: number) {
    this.db = db
  }

  private getDB(): number {
    return this.db
  }

  private redis(): IORedis.Redis {
    return new IORedis({
      host: process.env.REDIS_HOST as string,
      port: parseInt(process.env.REDIS_PORT as any),
      enableAutoPipelining: true,
      enableOfflineQueue: true,
      noDelay: true,
      db: this.getDB()
    })
  }

  async existCacheData(key: string): Promise<number> {
    const res: number = await this.redis().exists(key)
    return res
  }

  async hexistCacheData(key: string, field: string): Promise<IORedis.BooleanResponse> {
    const res: IORedis.BooleanResponse = await this.redis().hexists(key, field)
    return res
  }

  async delCacheData(key: string): Promise<number> {
    const res: number = await this.redis().del(key)
    return res
  }

  async hdelCacheData(key: string, field: string): Promise<number> {
    const res: number = await this.redis().hdel(key, field)
    return res
  }

  async setCacheData(key: string, data: Record<string, any> | Record<string, any>[], type: 'json' | 'buffer'): Promise<number | Buffer> {
    await this.redis().expire(key, 7200)
    let res: any
    if (type == 'json') res = await this.redis().set(key, JSON.stringify(data))
    if (type == 'buffer') res = await this.redis().setBuffer(key, Msgpack.pack(data))
    return res
  }

  async hsetCacheData(key: string, field: string, data: Record<string, any> | Record<string, any>[], type: 'json' | 'buffer'): Promise<number | Buffer> {
    await this.redis().expire(key, 7200)
    let res: any
    if (type == 'json') res = await this.redis().hset(key, JSON.stringify(data), field)
    if (type == 'buffer') res = await this.redis().hsetBuffer(key, field, Msgpack.pack(data))
    return res
  }

  async getCacheData(key: string, type: 'json' | 'buffer'): Promise<number | Buffer> {
    let res: any
    if (type == 'json') res = await this.redis().get(key)
    if (type == 'buffer') res = await this.redis().getBuffer(key)
    return res
  }

  async hgetCacheData(key: string, field: string, type: 'json' | 'buffer'): Promise<any> {
    let res: any
    if (type == 'json') res = JSON.parse(await this.redis().hget(key, field))
    if (type == 'buffer') res = Msgpack.unpack(await this.redis().hgetBuffer(key, field))
    return res
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: split a column of lists pandas 
Typescript :: regexp in typescript types 
Typescript :: list pop multiple elements python 
Typescript :: reflect-metadata 
Typescript :: whats the internet 
Typescript :: jquery tscroll up 
Typescript :: how to make the inputs become a sum python 
Typescript :: Git command to check for any conflicts between new and old versions on your repository 
Typescript :: how to mark plots octave 
Typescript :: google fonts roboto 
Cpp :: ue4 iterate tmap c++ 
Cpp :: sfml mouse position 
Cpp :: remove last letter in string c++ 
Cpp :: c++ message box error 
Cpp :: include all libraries in c++ 
Cpp :: remove element by index from vector c++ 
Cpp :: fibonacci series in c++ recursive 
Cpp :: remove all element of vector c++ 
Cpp :: hello world c++ visual studio 
Cpp :: how to iterate in string in c++ 
Cpp :: ue4 c++ array 
Cpp :: can you chnage the address of a pointer 
Cpp :: matrix layout in C++ 
Cpp :: delete 2d dynamic array c++ 
Cpp :: custom comparator in set of struct 
Cpp :: initialize all elements of vector to 0 c++ 
Cpp :: qlabel font color 
Cpp :: equal_range in C++ 
Cpp :: how to make a hello world program in c++ 
Cpp :: what is _asm in C++ 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =