UNPKG

@jasonsoft/nestjs-redis

Version:

Redis module for Nest framework (node.js)

139 lines (138 loc) 4.89 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisCacheHelper = void 0; const common_1 = require("@nestjs/common"); const interfaces_1 = require("./interfaces"); const redis_decorator_1 = require("./redis.decorator"); /** * Redis Cache Helper * Added by Jason.Song (成长的小猪) on 2023/03/16 23:59:41 */ let RedisCacheHelper = exports.RedisCacheHelper = class RedisCacheHelper { constructor(redis) { this.redis = redis; } /** * Set specified type data * Added by Jason.Song (成长的小猪) on 2023/03/17 00:01:01 * @param key * @param value * @param expire Set a key's time to live in seconds, for example: 60, "30m", "8h", "7d". Values are interpreted as counts of seconds. If you use strings, make sure to provide time units (minutes, hours, days, etc.) * @returns */ async set(key, value, expire) { let result; if (typeof value === 'object') { result = await this.redis.set(key, Buffer.from(JSON.stringify(value))); } else if (typeof value === 'boolean') { result = await this.redis.set(key, value.toString()); } else { result = await this.redis.set(key, value); } if (expire) { let seconds; if (typeof expire === 'string') { const matchArray = expire.match(/(\d+)([mhd])/); if (!matchArray) { throw new Error('Invalid ttl format, if you use strings, make sure to provide time units (minutes, hours, days, etc.), for example: "30m", "8h", "7d"'); } const [, num, unit] = matchArray; const multiplier = { m: 60, h: 60 * 60, d: 24 * 60 * 60, }; seconds = Math.floor(parseInt(num) * multiplier[unit]); } else { seconds = expire; } await this.redis.expire(key, seconds); } return result === 'OK'; } /** * delete key * Added by Jason.Song (成长的小猪) on 2023/03/17 00:12:53 * @param keys * @returns */ async del(...keys) { return this.redis.del(keys); } /** * Get string type data by key * Added by Jason.Song (成长的小猪) on 2023/03/17 00:13:31 * @param key * @param defaults * @returns */ async getAsStr(key, defaults) { const value = await this.redis.get(key); if (!value) { return defaults; } return value; } /** * Get number type data by key * Added by Jason.Song (成长的小猪) on 2023/03/17 00:14:39 * @param key * @param defaults * @returns */ async getAsNum(key, defaults) { const value = await this.redis.get(key); if (!value) { return defaults; } return +value; } /** * Get boolean type data by key * Added by Jason.Song (成长的小猪) on 2023/03/17 00:18:03 * @param key * @param defaults * @returns */ async getAsBool(key, defaults) { const value = await this.redis.get(key); if (!value) { return defaults; } return ['true', 'True', '1'].includes(value); } /** * Get object type data by key * Added by Jason.Song (成长的小猪) on 2023/03/17 00:21:07 * @param key * @param defaults * @returns */ async getAsObj(key, defaults) { const value = await this.redis.get(key); if (!value) { return defaults; } return JSON.parse(value); } }; exports.RedisCacheHelper = RedisCacheHelper = __decorate([ (0, common_1.Injectable)(), __param(0, (0, redis_decorator_1.InjectRedis)()), __metadata("design:paramtypes", [interfaces_1.Redis]) ], RedisCacheHelper);