@spytecgps/lambda-utils 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/utils/cache.d.ts +23 -0
- package/dist/utils/cache.js +58 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +5 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare class LambdaCache {
|
|
2
|
+
private collectionName;
|
|
3
|
+
/**
|
|
4
|
+
* @param {String} collectionName (not required) - The collection key used to store the cache values.
|
|
5
|
+
* If not provide default collection name uses :
|
|
6
|
+
* ${process.env.AWS_LAMBDA_FUNCTION_NAME}-${process.env.AWS_LAMBDA_FUNCTION_VERSION}
|
|
7
|
+
* */
|
|
8
|
+
constructor(collectionName?: string);
|
|
9
|
+
/**
|
|
10
|
+
* @param {String} key (required) - cache key
|
|
11
|
+
* @param {Object} value (required) - cache value
|
|
12
|
+
* @param {Number} expire (required) - cache expiration time (seconds)
|
|
13
|
+
* */
|
|
14
|
+
set(key: string, value: unknown, ttl: number): void;
|
|
15
|
+
/**
|
|
16
|
+
* @param {String} key (required) - cache key to get
|
|
17
|
+
* */
|
|
18
|
+
get(key: string): unknown;
|
|
19
|
+
/**
|
|
20
|
+
* @param {String} key (required) - cache key to remove
|
|
21
|
+
* */
|
|
22
|
+
remove(key: any): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LambdaCache = void 0;
|
|
4
|
+
var LambdaCache = /** @class */ (function () {
|
|
5
|
+
/**
|
|
6
|
+
* @param {String} collectionName (not required) - The collection key used to store the cache values.
|
|
7
|
+
* If not provide default collection name uses :
|
|
8
|
+
* ${process.env.AWS_LAMBDA_FUNCTION_NAME}-${process.env.AWS_LAMBDA_FUNCTION_VERSION}
|
|
9
|
+
* */
|
|
10
|
+
function LambdaCache(collectionName) {
|
|
11
|
+
this.collectionName = collectionName !== null && collectionName !== void 0 ? collectionName : process.env.AWS_LAMBDA_FUNCTION_NAME + "-" + process.env.AWS_LAMBDA_FUNCTION_VERSION;
|
|
12
|
+
if (!global['CACHE_STORAGE']) {
|
|
13
|
+
global['CACHE_STORAGE'] = {};
|
|
14
|
+
}
|
|
15
|
+
if (!global['CACHE_STORAGE'][this.collectionName]) {
|
|
16
|
+
global['CACHE_STORAGE'][this.collectionName] = new Map();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @param {String} key (required) - cache key
|
|
21
|
+
* @param {Object} value (required) - cache value
|
|
22
|
+
* @param {Number} expire (required) - cache expiration time (seconds)
|
|
23
|
+
* */
|
|
24
|
+
LambdaCache.prototype.set = function (key, value, ttl) {
|
|
25
|
+
var expire = 1000 * ttl + Date.now();
|
|
26
|
+
global['CACHE_STORAGE'][this.collectionName].set(key, { value: value, expire: expire });
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* @param {String} key (required) - cache key to get
|
|
30
|
+
* */
|
|
31
|
+
LambdaCache.prototype.get = function (key) {
|
|
32
|
+
if (!key) {
|
|
33
|
+
throw new Error('key is required!');
|
|
34
|
+
}
|
|
35
|
+
var record = global['CACHE_STORAGE'][this.collectionName].get(key);
|
|
36
|
+
if (!record) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
if (!record.expire || record.expire > Date.now()) {
|
|
40
|
+
return record.value;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return this.remove(key);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* @param {String} key (required) - cache key to remove
|
|
48
|
+
* */
|
|
49
|
+
LambdaCache.prototype.remove = function (key) {
|
|
50
|
+
var record = global['CACHE_STORAGE'][this.collectionName].get(key);
|
|
51
|
+
if (!record) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
global['CACHE_STORAGE'][this.collectionName].delete(key);
|
|
55
|
+
};
|
|
56
|
+
return LambdaCache;
|
|
57
|
+
}());
|
|
58
|
+
exports.LambdaCache = LambdaCache;
|