UNPKG

@am92/redis

Version:
555 lines (554 loc) 17.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisSdk = void 0; const ClientManager_1 = __importDefault(require("./ClientManager")); const RedisSdkError_1 = require("./RedisSdkError"); const CONFIG_1 = require("./CONFIG"); const ERRORS_1 = require("./ERRORS"); /** * Class to create an SDK which interacts with a Redis Instance * * @class */ class RedisSdk { /** * Redis Config used by the SDK */ CONFIG; /** * Redis Client used by the SDK */ client; /** * Flag identifying if the connection has been established or not */ connected = false; /** * Creates an instance of RedisSdk. * * @constructor * @param [config] Redis SDK Configuration */ constructor(config) { this.CONFIG = { KEY_PREFIX: CONFIG_1.REDIS_CONFIG.KEY_PREFIX || config?.KEY_PREFIX || '', CONNECTION_CONFIG: { ...CONFIG_1.REDIS_CONFIG.CONNECTION_CONFIG, ...config?.CONNECTION_CONFIG } }; this.client = ClientManager_1.default.createClient(this.CONFIG.CONNECTION_CONFIG || {}); // Method Hard-binding this.connect = this.connect.bind(this); this.disconnect = this.disconnect.bind(this); this.getClient = this.getClient.bind(this); this.prefixKey = this.prefixKey.bind(this); this.unprefixKey = this.unprefixKey.bind(this); this.get = this.get.bind(this); this.set = this.set.bind(this); this.expire = this.expire.bind(this); this.expireAt = this.expireAt.bind(this); this.expireTime = this.expireTime.bind(this); this.ttl = this.ttl.bind(this); this.getAndExpire = this.getAndExpire.bind(this); this.setAndExpire = this.setAndExpire.bind(this); this.del = this.del.bind(this); this.keys = this.keys.bind(this); this.delByPattern = this.delByPattern.bind(this); this.incrBy = this.incrBy.bind(this); this.incrByAndExpire = this.incrByAndExpire.bind(this); this.decrBy = this.decrBy.bind(this); this.decrByAndExpire = this.decrByAndExpire.bind(this); this.exists = this.exists.bind(this); this.hSet = this.hSet.bind(this); this.hSetNX = this.hSetNX.bind(this); this.hIncrBy = this.hIncrBy.bind(this); this.hGet = this.hGet.bind(this); this.hGetAll = this.hGetAll.bind(this); this.hKeys = this.hKeys.bind(this); this.hVals = this.hVals.bind(this); this.hLen = this.hLen.bind(this); this.hStrLen = this.hStrLen.bind(this); this.hDel = this.hDel.bind(this); this.hExists = this.hExists.bind(this); this.hmGet = this.hmGet.bind(this); } /** * Establish a connection with the Redis Instance * * @async * @returns */ async connect() { console.info(`[${CONFIG_1.SERVICE} Redis] Establishing Redis Connection...`); await this.client.connect(); const successLogFunc = console.success || console.info; successLogFunc(`[${CONFIG_1.SERVICE} Redis] Redis Connection Established`); this.connected = true; } /** * Releases the connection with the Redis Instance if established * * @async * @param [forced=false] * @returns */ async disconnect(forced = false) { if (!this.connected) { console.error(`[${CONFIG_1.SERVICE} Redis] Disconnection to Redis failed as it is not connected`); return; } await ClientManager_1.default.releaseClient(this.client, forced); this.connected = false; } /** * Returns the Redis client as configured using `CONFIG` * * @returns */ getClient() { if (this.connected) { return this.client; } throw new RedisSdkError_1.RedisSdkError(ERRORS_1.CLIENT_NOT_CONNECTED_ERROR); } /** * Prefixes the Redis key with `CONFIG.KEY_PREFIX` * * @param [key=''] * @returns */ prefixKey(key = '') { if (typeof key !== 'string') { return ''; } const { KEY_PREFIX } = this.CONFIG; const prefixPattern = `${KEY_PREFIX}__`; const hasPrefix = key.indexOf(prefixPattern) === 0; const prefixedKey = (!hasPrefix && `${prefixPattern}${key}`) || key; return prefixedKey; } /** * Unprefixes the Redis key with `CONFIG.KEY_PREFIX` * * @param [key=''] * @returns */ unprefixKey(key = '') { if (typeof key !== 'string') { return ''; } const { KEY_PREFIX } = this.CONFIG; const prefixPattern = `${KEY_PREFIX}__`; const hasPrefix = key.indexOf(prefixPattern) === 0; const unprefixedKey = (!hasPrefix && key.split(prefixPattern)[1]) || key; return unprefixedKey; } /** * Gets the value of a Redis key * * @async * @param key Redis key name * @returns */ async get(key) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const value = await client.get(prefixedKey); return value; } /** * Sets the value for a Redis key * * @async * @param key Redis key name * @param value Value to be stored in Redis * @param options Options as mentioned by 'set' method of Redis package * @returns */ async set(key, value, options) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const returnValue = await client.set(prefixedKey, value, options); return returnValue; } /** * Sets the expiry of Redis key in seconds * * @async * @param key Redis key name * @param ttlInSecs Redis key expiry in seconds * @param [mode] Conditions when expiry should be set * @returns */ async expire(key, ttlInSecs, mode) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const isSet = await client.expire(prefixedKey, ttlInSecs, mode); return isSet; } /** * Sets the expiry of Redis key to Unix timestamp * * @async * @param key Redis key name * @param ttlInSecs Redis key expiry in seconds * @param [mode] Conditions when expiry should be set * @returns */ async expireAt(key, unixInSecs, mode) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const isSet = await client.expireAt(prefixedKey, unixInSecs, mode); return isSet; } /** * Get the expiry of Redis key as Unix timestamp * * @async * @param key Redis key name * @returns */ async expireTime(key) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const unixInSecs = await client.expireTime(prefixedKey); return unixInSecs; } /** * Get the expiry of Redis key in seconds. * * @async * @param key Redis key name * @returns */ async ttl(key) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const ttlInSecs = await client.ttl(prefixedKey); return ttlInSecs; } /** * Gets the value of a Redis key and sets the expiry of Redis key * * @async * @param key Redis key name * @param ttlInSecs Redis key expiry in seconds * @returns */ async getAndExpire(key, ttlInSecs) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const value = await client.get(prefixedKey); if (ttlInSecs) { await client.expire(prefixedKey, ttlInSecs); } return value; } /** * Sets the value of a Redis key and sets the expiry of Redis key * * @async * @param key Redis key name * @param value Value to be stored in Redis * @param ttlInSecs Redis key expiry in seconds * @param options Options as mentioned by 'set' method of Redis package * @returns */ async setAndExpire(key, value, ttlInSecs, options) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const returnValue = await client.set(prefixedKey, value, options); if (ttlInSecs) { await client.expire(prefixedKey, ttlInSecs); } return returnValue; } /** * Deletes a Redis key or keys * * @async * @param keys Redis key name or Array of Redis key names * @returns */ async del(keys) { const client = this.getClient(); const prefixedKeys = keys instanceof Array ? keys.map(this.prefixKey) : this.prefixKey(keys); const deleteCount = await client.del(prefixedKeys); return deleteCount; } /** * Gets all Redis keys for a given key pattern * * @async * @param pattern Redis key pattern * @returns */ async keys(pattern) { const client = this.getClient(); const searchPatten = this.prefixKey(pattern); const prefixedKeys = await client.keys(searchPatten); const values = prefixedKeys.map(this.unprefixKey); return values; } /** * Deletes all Redis keys for a given key pattern * * @async * @param pattern Redis key pattern * @returns */ async delByPattern(pattern) { const foundKeys = await this.keys(pattern); const deleteCount = await this.del(foundKeys); return deleteCount; } /** * Increments the value of a Redis key * * @async * @param key Redis key name * @param [value=0] Redis value to be incremented by * @returns */ async incrBy(key, value = 0) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const incrValue = await client.incrBy(prefixedKey, value); return incrValue; } /** * Increments the value of a Redis key and sets the expiry of Redis key * * @async * @param key Redis key name * @param [value=0] Redis value to be incremented by * @param ttlInSecs Redis key expiry in seconds * @returns */ async incrByAndExpire(key, value = 0, ttlInSecs) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const incrValue = await client.incrBy(prefixedKey, value || 0); if (ttlInSecs) { await client.expire(prefixedKey, ttlInSecs); } return incrValue; } /** * Decrements the value of a Redis key * * @async * @param key Redis key name * @param [value=0] Redis value to be decremented by * @returns */ async decrBy(key, value = 0) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const decrValue = await client.decrBy(prefixedKey, value); return decrValue; } /** * Decrements the value of a Redis key and sets the expiry of Redis key * * @async * @param key Redis key name * @param [value=0] Redis value to be decremented by * @param ttlInSecs Redis key expiry in seconds * @returns */ async decrByAndExpire(key, value = 0, ttlInSecs) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const decrValue = await client.decrBy(prefixedKey, value || 0); if (ttlInSecs) { await client.expire(prefixedKey, ttlInSecs); } return decrValue; } /** * Checks if Redis keys exist or not * * @async * @param [keys=[]] Array of Redis key names * @returns */ async exists(keys) { const client = this.getClient(); const prefixedKeys = (keys || []).map(this.prefixKey); const keyCount = await client.exists(prefixedKeys); return keyCount; } /** * Creates or modifies the value of a field in a hash * * @async * @param key Redis key name of hash * @param field Hash field * @param value Hash value * @returns */ async hSet(key, field, value) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const isSet = await client.hSet(prefixedKey, field, value); return isSet; } /** * Sets the value of a field in a hash only when the field doesn't exist * * @async * @param key Redis key name of hash * @param field Hash field * @param value Hash value * @returns */ async hSetNX(key, field, value) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const isSet = await client.hSetNX(prefixedKey, field, value); return isSet; } /** * Increments the integer value of a field in a hash by a number. Uses 0 as initial value if the field doesn't exist * * @async * @param key Redis key name of hash * @param field Hash field * @param [value=0] Value to be incremented by * @returns */ async hIncrBy(key, field, value = 0) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const incrValue = await client.hIncrBy(prefixedKey, field, value); return incrValue; } /** * Returns the value of a field in a hash * * @async * @param key Redis key name of hash * @param field Hash field * @returns */ async hGet(key, field) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const value = await client.hGet(prefixedKey, field); return value; } /** * Returns all fields and values in a hash * * @async * @param key Redis key name of hash * @returns */ async hGetAll(key) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const hash = await client.hGetAll(prefixedKey); return hash; } /** * Returns all fields in a hash * * @async * @param key Redis key name of hash * @returns */ async hKeys(key) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const fields = await client.hKeys(prefixedKey); return fields; } /** * Returns all values in a hash * * @async * @param key Redis key name of hash * @returns */ async hVals(key) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const values = await client.hVals(prefixedKey); return values; } /** * Returns the number of fields in a hash * * @async * @param key Redis key name of hash * @returns */ async hLen(key) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const fieldCount = await client.hLen(prefixedKey); return fieldCount; } /** * Returns the length of the value of a field * * @async * @param key Redis key name of hash * @param field Hash field * @returns */ async hStrLen(key, field) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const length = await client.hStrLen(prefixedKey, field); return length; } /** * Deletes one or more fields and their values from a hash. Deletes the hash if no fields remain * * @async * @param key Redis key name of hash * @param field Hash field * @returns */ async hDel(key, field) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const isDel = await client.hDel(prefixedKey, field); return isDel; } /** * Determines whether a field exists in a hash * * @async * @param key Redis key name of hash * @param field Hash field * @returns */ async hExists(key, field) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const exists = await client.hExists(prefixedKey, field); return exists; } /** * Returns the values of all fields in a hash * * @async * @param key Redis key name of hash * @param fields Hash fields * @returns */ async hmGet(key, fields) { const client = this.getClient(); const prefixedKey = this.prefixKey(key); const exists = await client.hmGet(prefixedKey, fields); return exists; } } exports.RedisSdk = RedisSdk;