@quantos/micro-request
Advanced tools
Comparing version 1.0.9 to 1.1.0
@@ -5,3 +5,5 @@ import { Entity } from './interfaces/entity'; | ||
static getMany<Y, T extends Entity<Y>>(ids: Y[], cachePrefix?: string): Promise<T[]>; | ||
static set<Y, T extends Entity<Y>>(id: Y, data: T, cachePrefix?: string): Promise<void>; | ||
static set<Y, T extends Entity<Y>>(id: Y, data: T, cachePrefix?: string, options?: { | ||
ttl?: number; | ||
}): Promise<void>; | ||
} |
@@ -11,18 +11,16 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RedisCache = void 0; | ||
const redis_helper_1 = require("@quantos/redis-helper"); | ||
const redis_1 = __importDefault(require("redis")); | ||
const redis_1 = require("redis"); | ||
let redisClient; | ||
let _redisEnabled = false; | ||
if (process.env.REDIS_PORT && process.env.REDIS_HOST) { | ||
console.log('[@quantos/micro-request][Init redis client] Creating client on ' + process.env.REDIS_HOST + ':' + process.env.REDIS_PORT); | ||
redisClient = redis_1.default.createClient(Number.parseInt(process.env.REDIS_PORT), process.env.REDIS_HOST, { enable_offline_queue: false }); | ||
redisClient.on('ready', () => { | ||
console.log('[@quantos/micro-request][Init redis client] Creating client on ' + process.env.REDIS_URL); | ||
redisClient = (0, redis_1.createClient)({ url: process.env.REDIS_URL }); | ||
redisClient | ||
.connect() | ||
.then(() => { | ||
_redisEnabled = true; | ||
}); | ||
redisClient.on('error', (error) => { | ||
}) | ||
.catch((error) => { | ||
console.error('[@quantos/micro-request][Init redis client] ' + error); | ||
@@ -38,5 +36,5 @@ _redisEnabled = false; | ||
} | ||
const cache = new redis_helper_1.RedisValueHelper({ client: redisClient, prefix: cachePrefix, disableListeners: true }); | ||
const cached = yield cache.getCached(`${id}`); | ||
if (cached) { | ||
const data = yield redisClient.get(`${cachePrefix}:${id}`); | ||
if (data && data.length) { | ||
const cached = JSON.parse(data); | ||
return cached; | ||
@@ -51,10 +49,13 @@ } | ||
} | ||
const cache = new redis_helper_1.RedisValueHelper({ client: redisClient, prefix: cachePrefix, disableListeners: true }); | ||
const cacheds = yield cache.getCacheds(ids.map(String)); | ||
if (cacheds) { | ||
return cacheds; | ||
const cacheds = []; | ||
for (const key of ids) { | ||
const cached = yield this.get(key); | ||
if (cached) { | ||
cacheds.push(cached); | ||
} | ||
} | ||
return cacheds; | ||
}); | ||
} | ||
static set(id, data, cachePrefix) { | ||
static set(id, data, cachePrefix, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -64,4 +65,7 @@ if (!_redisEnabled) { | ||
} | ||
const cache = new redis_helper_1.RedisValueHelper({ client: redisClient, prefix: cachePrefix, disableListeners: true }); | ||
yield cache.setCached(`${id}`, data); | ||
try { | ||
yield redisClient.set(`${cachePrefix}:${id}`, JSON.stringify(data), { EX: options === null || options === void 0 ? void 0 : options.ttl }); | ||
} | ||
catch (e) { | ||
} | ||
}); | ||
@@ -68,0 +72,0 @@ } |
{ | ||
"name": "@quantos/micro-request", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "Quantos http client with cache, retry and fallback mechanism", | ||
@@ -12,11 +12,11 @@ "author": "fabriziotognetto <info@quantos.it>", | ||
"devDependencies": { | ||
"@types/redis": "^2.8.28", | ||
"ts-loader": "^5.3.3", | ||
"@types/redis": "^4.0.11", | ||
"ts-loader": "^9.4.2", | ||
"tslint": "^6.1.3", | ||
"typescript": "^4.0.5" | ||
"typescript": "^4.9.5" | ||
}, | ||
"dependencies": { | ||
"@quantos/redis-helper": "^1.0.10", | ||
"axios": "^0.21.1" | ||
"redis": "^4.6.5", | ||
"axios": "^1.3.4" | ||
} | ||
} |
@@ -1,43 +0,58 @@ | ||
import { RedisValueHelper } from '@quantos/redis-helper'; | ||
import redis from 'redis'; | ||
import { createClient, RedisClientType } from 'redis'; | ||
import { Entity } from './interfaces/entity'; | ||
/** Init a redis client */ | ||
let redisClient: redis.RedisClient; | ||
let redisClient: RedisClientType; | ||
let _redisEnabled = false; | ||
if (process.env.REDIS_PORT && process.env.REDIS_HOST) { | ||
console.log('[@quantos/micro-request][Init redis client] Creating client on ' + process.env.REDIS_HOST + ':' + process.env.REDIS_PORT); | ||
redisClient = redis.createClient(Number.parseInt(process.env.REDIS_PORT), process.env.REDIS_HOST, {enable_offline_queue: false}); | ||
redisClient.on('ready', () => { | ||
_redisEnabled = true; | ||
console.log('[@quantos/micro-request][Init redis client] Creating client on ' + process.env.REDIS_URL); | ||
redisClient = createClient({ url: process.env.REDIS_URL }); | ||
redisClient | ||
.connect() | ||
.then(() => { | ||
_redisEnabled = true; | ||
}) | ||
.catch((error: any) => { | ||
console.error('[@quantos/micro-request][Init redis client] ' + error); | ||
_redisEnabled = false; | ||
}); | ||
redisClient.on('error', (error: any) => { | ||
console.error('[@quantos/micro-request][Init redis client] ' + error); | ||
_redisEnabled = false; | ||
}); | ||
} | ||
export class RedisCache { | ||
static async get<T>(id: any, cachePrefix?: string): Promise<T | null> { | ||
if (!_redisEnabled) { return null; } | ||
const cache = new RedisValueHelper<T>({ client: redisClient, prefix: cachePrefix, disableListeners: true }); | ||
const cached = await cache.getCached(`${id}`); | ||
if (cached) { return cached; } | ||
static async get<T>(id: any, cachePrefix?: string): Promise<T | null> { | ||
if (!_redisEnabled) { | ||
return null; | ||
} | ||
const data = await redisClient.get(`${cachePrefix}:${id}`); | ||
if (data && data.length) { | ||
const cached: T = JSON.parse(data); | ||
return cached; | ||
} | ||
} | ||
static async getMany<Y, T extends Entity<Y>>(ids: Y[], cachePrefix?: string): Promise<T[]> { | ||
if (!_redisEnabled) { return null; } | ||
const cache = new RedisValueHelper<T>({ client: redisClient, prefix: cachePrefix, disableListeners: true }); | ||
const cacheds = await cache.getCacheds(ids.map(String)); | ||
if (cacheds) { return cacheds; } | ||
static async getMany<Y, T extends Entity<Y>>(ids: Y[], cachePrefix?: string): Promise<T[]> { | ||
if (!_redisEnabled) { | ||
return null; | ||
} | ||
const cacheds: T[] = []; | ||
for (const key of ids) { | ||
const cached = await this.get<T>(key); | ||
if (cached) { | ||
cacheds.push(cached); | ||
} | ||
} | ||
return cacheds; | ||
} | ||
static async set<Y, T extends Entity<Y>>(id: Y, data: T, cachePrefix?: string): Promise<void> { | ||
if (!_redisEnabled) { return; } | ||
const cache = new RedisValueHelper<T>({ client: redisClient, prefix: cachePrefix, disableListeners: true }); | ||
await cache.setCached(`${id}`, data); | ||
static async set<Y, T extends Entity<Y>>(id: Y, data: T, cachePrefix?: string, options?: { ttl?: number }): Promise<void> { | ||
if (!_redisEnabled) { | ||
return; | ||
} | ||
try { | ||
await redisClient.set(`${cachePrefix}:${id}`, JSON.stringify(data), { EX: options?.ttl }); | ||
} catch (e) { | ||
// do nothing | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
782
5
36741
+ Addedredis@^4.6.5
+ Added@redis/bloom@1.2.0(transitive)
+ Added@redis/client@1.6.0(transitive)
+ Added@redis/graph@1.1.1(transitive)
+ Added@redis/json@1.0.7(transitive)
+ Added@redis/search@1.2.0(transitive)
+ Added@redis/time-series@1.1.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaxios@1.7.7(transitive)
+ Addedcluster-key-slot@1.1.2(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedform-data@4.0.0(transitive)
+ Addedgeneric-pool@3.9.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedproxy-from-env@1.1.0(transitive)
+ Addedredis@4.7.0(transitive)
+ Addedyallist@4.0.0(transitive)
- Removed@quantos/redis-helper@^1.0.10
- Removed@quantos/redis-helper@1.0.10(transitive)
- Removedaxios@0.21.4(transitive)
- Removeddenque@1.5.1(transitive)
- Removedredis@3.1.2(transitive)
- Removedredis-commands@1.7.0(transitive)
- Removedredis-errors@1.2.0(transitive)
- Removedredis-parser@3.0.0(transitive)
Updatedaxios@^1.3.4