Comparing version 0.1.4 to 0.2.0
@@ -8,8 +8,10 @@ /// <reference types="node" /> | ||
} | ||
export interface CacheOption { | ||
ttl: number; | ||
limit?: number; | ||
} | ||
export interface Endpoint { | ||
schema: Joi.Schema; | ||
timeout?: number; | ||
cache?: { | ||
ttl: number; | ||
}; | ||
cacheOption?: CacheOption | false; | ||
handler(payload: any, delegator: Delegator): any; | ||
@@ -36,3 +38,4 @@ } | ||
terminate(): Promise<void>; | ||
private shouldReturnCache(cacheOption, cacheKey); | ||
private executeEndpoint(topic, payload); | ||
} |
@@ -78,3 +78,2 @@ "use strict"; | ||
return; | ||
this.server.close(); | ||
if (this.redis) { | ||
@@ -88,5 +87,25 @@ try { | ||
} | ||
this.server.close(); | ||
delete this.server; | ||
}); | ||
} | ||
shouldReturnCache(cacheOption, cacheKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.redis || !cacheOption) | ||
return false; | ||
if (!cacheOption.limit) | ||
return true; | ||
try { | ||
const cacheCount = yield this.redis.incr(`${cacheKey}::count`); | ||
if (cacheCount === 1) { | ||
yield this.redis.expire(`${cacheKey}::count`, cacheOption.ttl); | ||
} | ||
return cacheCount > cacheOption.limit; | ||
} | ||
catch (err) { | ||
console.error(err); | ||
} | ||
return false; | ||
}); | ||
} | ||
executeEndpoint(topic, payload) { | ||
@@ -103,5 +122,6 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
if (this.redis && endpoint.cache) { | ||
const cacheKey = _getCacheKey(topic, payload); | ||
if (yield this.shouldReturnCache(endpoint.cacheOption, cacheKey)) { | ||
try { | ||
const cache = yield this.redis.get(_getCacheKey(topic, payload)); | ||
const cache = yield this.redis.get(cacheKey); | ||
if (cache) | ||
@@ -130,7 +150,6 @@ return JSON.parse(cache).v; | ||
} | ||
if (this.redis && endpoint.cache) { | ||
if (this.redis && endpoint.cacheOption) { | ||
try { | ||
const key = _getCacheKey(topic, payload); | ||
const value = JSON.stringify({ v: result }); | ||
yield this.redis.set([key, value, 'EX', endpoint.cache.ttl]); | ||
yield this.redis.set([cacheKey, value, 'EX', endpoint.cacheOption.ttl]); | ||
} | ||
@@ -137,0 +156,0 @@ catch (err) { |
{ | ||
"name": "chayen", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "An RPC framework with so much hype going on. such wow.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
239
0
126345