UNPKG

dazjsx

Version:

参考nestjs,基于KOA2的一款轻量级的后端开发框架

54 lines (53 loc) 1.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisService = void 0; const ioredis_1 = __importDefault(require("ioredis")); const util_1 = require("../util"); const log_service_1 = require("./log.service"); class RedisService { constructor() { this.reTry = 10; this.initialize(); } initialize() { if (!this.ioRedis && util_1.Tool.isNotEmptyObject(RedisService.option)) { try { this.ioRedis = new ioredis_1.default(RedisService.option); log_service_1.LogService.info('redis->连接成功'); } catch (error) { log_service_1.LogService.error(error.message); if (--this.reTry > 0) { util_1.Tool.waiting(1).then(() => this.initialize()); } else { process.exit(0); } } } } get redis() { return this.ioRedis; } async get(key) { const data = await this.redis.get(key); try { return JSON.parse(data); } catch (error) { return data; } } async set(key, value, expire) { const ttl = expire && !isNaN(Number(expire)) ? expire : undefined; const val = value && typeof value === 'object' ? JSON.stringify(value) : value; return this.redis.set(key, val, 'EX', ttl); } async del(key) { return this.redis.del(key); } } exports.RedisService = RedisService;