@melledijkstra/storage 1.0.0
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.cjs +103 -0
- package/dist/index.d.cts +83 -0
- package/dist/index.d.mts +83 -0
- package/dist/index.mjs +94 -0
- package/package.json +29 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
let _melledijkstra_toolbox = require("@melledijkstra/toolbox");
|
|
2
|
+
|
|
3
|
+
//#region src/memory.ts
|
|
4
|
+
const logger = new _melledijkstra_toolbox.Logger("cache");
|
|
5
|
+
const cache = {};
|
|
6
|
+
const SEC_30 = 30 * 1e3;
|
|
7
|
+
const MIN_1 = 60 * 1e3;
|
|
8
|
+
const MIN_3 = 180 * 1e3;
|
|
9
|
+
const MIN_5 = 300 * 1e3;
|
|
10
|
+
const MIN_10 = 600 * 1e3;
|
|
11
|
+
const MIN_15 = 900 * 1e3;
|
|
12
|
+
function get(key) {
|
|
13
|
+
const cachedItem = cache[key];
|
|
14
|
+
if (cachedItem) if (Date.now() - cachedItem.timestamp > cachedItem.ttl) delete cache[key];
|
|
15
|
+
else {
|
|
16
|
+
logger.log("Cache hit:", key);
|
|
17
|
+
return cachedItem.data;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function set(key, value, ttl = Infinity) {
|
|
21
|
+
cache[key] = {
|
|
22
|
+
data: value,
|
|
23
|
+
timestamp: Date.now(),
|
|
24
|
+
ttl: ttl ?? MIN_5
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Curried `withCache`: first call it with your options,
|
|
29
|
+
* then call the returned function with your async function.
|
|
30
|
+
*/
|
|
31
|
+
function withCache(originalFunc, options = {}) {
|
|
32
|
+
const defaultTTL = 300 * 1e3;
|
|
33
|
+
const cachedFunction = async (...args) => {
|
|
34
|
+
const cacheKey = options?.key ?? originalFunc.name;
|
|
35
|
+
const cacheTTL = options?.ttl ?? defaultTTL;
|
|
36
|
+
const cachedData = get(cacheKey);
|
|
37
|
+
if (cachedData !== void 0) return cachedData;
|
|
38
|
+
const result = await originalFunc(...args);
|
|
39
|
+
set(cacheKey, result, cacheTTL);
|
|
40
|
+
return result;
|
|
41
|
+
};
|
|
42
|
+
return cachedFunction;
|
|
43
|
+
}
|
|
44
|
+
var MemoryCache = class {
|
|
45
|
+
logger = new _melledijkstra_toolbox.Logger("MemoryCache");
|
|
46
|
+
_cache = {};
|
|
47
|
+
get(key) {
|
|
48
|
+
const cachedItem = this._cache[key];
|
|
49
|
+
if (!cachedItem) return;
|
|
50
|
+
if (Date.now() - cachedItem.timestamp > cachedItem.ttl) delete this._cache[key];
|
|
51
|
+
else return cachedItem.data;
|
|
52
|
+
}
|
|
53
|
+
async set(key, value, ttl = Infinity) {
|
|
54
|
+
this._cache[key] = {
|
|
55
|
+
data: value,
|
|
56
|
+
timestamp: Date.now(),
|
|
57
|
+
ttl
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
async delete(key) {
|
|
61
|
+
delete this._cache[key];
|
|
62
|
+
}
|
|
63
|
+
async clear() {
|
|
64
|
+
this._cache = {};
|
|
65
|
+
}
|
|
66
|
+
isExpired(key) {
|
|
67
|
+
const cachedItem = this._cache[key];
|
|
68
|
+
if (!cachedItem) return true;
|
|
69
|
+
if (Date.now() - cachedItem.timestamp > cachedItem.ttl) {
|
|
70
|
+
delete this._cache[key];
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
has(key) {
|
|
76
|
+
const cachedItem = this._cache[key];
|
|
77
|
+
if (!cachedItem) return false;
|
|
78
|
+
if (this.isExpired(key)) return false;
|
|
79
|
+
if (Date.now() - cachedItem.timestamp > cachedItem.ttl) {
|
|
80
|
+
delete this._cache[key];
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
keys() {
|
|
86
|
+
return Object.keys(this._cache);
|
|
87
|
+
}
|
|
88
|
+
size() {
|
|
89
|
+
return Object.keys(this._cache).length;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
exports.MIN_1 = MIN_1;
|
|
95
|
+
exports.MIN_10 = MIN_10;
|
|
96
|
+
exports.MIN_15 = MIN_15;
|
|
97
|
+
exports.MIN_3 = MIN_3;
|
|
98
|
+
exports.MIN_5 = MIN_5;
|
|
99
|
+
exports.MemoryCache = MemoryCache;
|
|
100
|
+
exports.SEC_30 = SEC_30;
|
|
101
|
+
exports.get = get;
|
|
102
|
+
exports.set = set;
|
|
103
|
+
exports.withCache = withCache;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Logger } from "@melledijkstra/toolbox";
|
|
2
|
+
|
|
3
|
+
//#region src/storage.interface.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Abstract interface for various storage solutions
|
|
7
|
+
* Supports: localStorage, sessionStorage, cookies, IndexedDB, memory, etc.
|
|
8
|
+
*/
|
|
9
|
+
interface IStorage {
|
|
10
|
+
/**
|
|
11
|
+
* Retrieve a value from storage
|
|
12
|
+
* @template T - Type of the stored value
|
|
13
|
+
* @param key - Storage key
|
|
14
|
+
* @returns The stored value or undefined if not found
|
|
15
|
+
*/
|
|
16
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
17
|
+
/**
|
|
18
|
+
* Store a value in storage
|
|
19
|
+
* @template T - Type of the value to store
|
|
20
|
+
* @param key - Storage key
|
|
21
|
+
* @param value - Value to store
|
|
22
|
+
* @param ttl - Time to live in milliseconds (optional)
|
|
23
|
+
*/
|
|
24
|
+
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Delete a specific item from storage
|
|
27
|
+
* @param key - Storage key to delete
|
|
28
|
+
*/
|
|
29
|
+
delete(key: string): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Clear all items from storage
|
|
32
|
+
*/
|
|
33
|
+
clear(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Check if a key exists in storage
|
|
36
|
+
* @param key - Storage key
|
|
37
|
+
* @returns True if key exists, false otherwise
|
|
38
|
+
*/
|
|
39
|
+
has(key: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Get all keys stored in storage
|
|
42
|
+
* @returns Array of all keys
|
|
43
|
+
*/
|
|
44
|
+
keys(): string[];
|
|
45
|
+
/**
|
|
46
|
+
* Get the number of items in storage
|
|
47
|
+
* @returns Number of stored items
|
|
48
|
+
*/
|
|
49
|
+
size(): number;
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/memory.d.ts
|
|
53
|
+
declare const SEC_30: number;
|
|
54
|
+
declare const MIN_1: number;
|
|
55
|
+
declare const MIN_3: number;
|
|
56
|
+
declare const MIN_5: number;
|
|
57
|
+
declare const MIN_10: number;
|
|
58
|
+
declare const MIN_15: number;
|
|
59
|
+
declare function get(key: string): unknown;
|
|
60
|
+
declare function set(key: string, value: unknown, ttl?: number): void;
|
|
61
|
+
type CacheOptions = {
|
|
62
|
+
key?: string;
|
|
63
|
+
ttl?: number;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Curried `withCache`: first call it with your options,
|
|
67
|
+
* then call the returned function with your async function.
|
|
68
|
+
*/
|
|
69
|
+
declare function withCache<T, A extends unknown[]>(originalFunc: (...args: A) => Promise<T>, options?: CacheOptions): (...args: A) => Promise<T>;
|
|
70
|
+
declare class MemoryCache implements IStorage {
|
|
71
|
+
logger: Logger;
|
|
72
|
+
private _cache;
|
|
73
|
+
get<T>(key: string): T | undefined;
|
|
74
|
+
set(key: string, value: unknown, ttl?: number): Promise<void>;
|
|
75
|
+
delete(key: string): Promise<void>;
|
|
76
|
+
clear(): Promise<void>;
|
|
77
|
+
isExpired(key: string): boolean;
|
|
78
|
+
has(key: string): boolean;
|
|
79
|
+
keys(): string[];
|
|
80
|
+
size(): number;
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
export { IStorage, MIN_1, MIN_10, MIN_15, MIN_3, MIN_5, MemoryCache, SEC_30, get, set, withCache };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Logger } from "@melledijkstra/toolbox";
|
|
2
|
+
|
|
3
|
+
//#region src/storage.interface.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Abstract interface for various storage solutions
|
|
7
|
+
* Supports: localStorage, sessionStorage, cookies, IndexedDB, memory, etc.
|
|
8
|
+
*/
|
|
9
|
+
interface IStorage {
|
|
10
|
+
/**
|
|
11
|
+
* Retrieve a value from storage
|
|
12
|
+
* @template T - Type of the stored value
|
|
13
|
+
* @param key - Storage key
|
|
14
|
+
* @returns The stored value or undefined if not found
|
|
15
|
+
*/
|
|
16
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
17
|
+
/**
|
|
18
|
+
* Store a value in storage
|
|
19
|
+
* @template T - Type of the value to store
|
|
20
|
+
* @param key - Storage key
|
|
21
|
+
* @param value - Value to store
|
|
22
|
+
* @param ttl - Time to live in milliseconds (optional)
|
|
23
|
+
*/
|
|
24
|
+
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Delete a specific item from storage
|
|
27
|
+
* @param key - Storage key to delete
|
|
28
|
+
*/
|
|
29
|
+
delete(key: string): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Clear all items from storage
|
|
32
|
+
*/
|
|
33
|
+
clear(): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Check if a key exists in storage
|
|
36
|
+
* @param key - Storage key
|
|
37
|
+
* @returns True if key exists, false otherwise
|
|
38
|
+
*/
|
|
39
|
+
has(key: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Get all keys stored in storage
|
|
42
|
+
* @returns Array of all keys
|
|
43
|
+
*/
|
|
44
|
+
keys(): string[];
|
|
45
|
+
/**
|
|
46
|
+
* Get the number of items in storage
|
|
47
|
+
* @returns Number of stored items
|
|
48
|
+
*/
|
|
49
|
+
size(): number;
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/memory.d.ts
|
|
53
|
+
declare const SEC_30: number;
|
|
54
|
+
declare const MIN_1: number;
|
|
55
|
+
declare const MIN_3: number;
|
|
56
|
+
declare const MIN_5: number;
|
|
57
|
+
declare const MIN_10: number;
|
|
58
|
+
declare const MIN_15: number;
|
|
59
|
+
declare function get(key: string): unknown;
|
|
60
|
+
declare function set(key: string, value: unknown, ttl?: number): void;
|
|
61
|
+
type CacheOptions = {
|
|
62
|
+
key?: string;
|
|
63
|
+
ttl?: number;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Curried `withCache`: first call it with your options,
|
|
67
|
+
* then call the returned function with your async function.
|
|
68
|
+
*/
|
|
69
|
+
declare function withCache<T, A extends unknown[]>(originalFunc: (...args: A) => Promise<T>, options?: CacheOptions): (...args: A) => Promise<T>;
|
|
70
|
+
declare class MemoryCache implements IStorage {
|
|
71
|
+
logger: Logger;
|
|
72
|
+
private _cache;
|
|
73
|
+
get<T>(key: string): T | undefined;
|
|
74
|
+
set(key: string, value: unknown, ttl?: number): Promise<void>;
|
|
75
|
+
delete(key: string): Promise<void>;
|
|
76
|
+
clear(): Promise<void>;
|
|
77
|
+
isExpired(key: string): boolean;
|
|
78
|
+
has(key: string): boolean;
|
|
79
|
+
keys(): string[];
|
|
80
|
+
size(): number;
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
export { IStorage, MIN_1, MIN_10, MIN_15, MIN_3, MIN_5, MemoryCache, SEC_30, get, set, withCache };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Logger } from "@melledijkstra/toolbox";
|
|
2
|
+
|
|
3
|
+
//#region src/memory.ts
|
|
4
|
+
const logger = new Logger("cache");
|
|
5
|
+
const cache = {};
|
|
6
|
+
const SEC_30 = 30 * 1e3;
|
|
7
|
+
const MIN_1 = 60 * 1e3;
|
|
8
|
+
const MIN_3 = 180 * 1e3;
|
|
9
|
+
const MIN_5 = 300 * 1e3;
|
|
10
|
+
const MIN_10 = 600 * 1e3;
|
|
11
|
+
const MIN_15 = 900 * 1e3;
|
|
12
|
+
function get(key) {
|
|
13
|
+
const cachedItem = cache[key];
|
|
14
|
+
if (cachedItem) if (Date.now() - cachedItem.timestamp > cachedItem.ttl) delete cache[key];
|
|
15
|
+
else {
|
|
16
|
+
logger.log("Cache hit:", key);
|
|
17
|
+
return cachedItem.data;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function set(key, value, ttl = Infinity) {
|
|
21
|
+
cache[key] = {
|
|
22
|
+
data: value,
|
|
23
|
+
timestamp: Date.now(),
|
|
24
|
+
ttl: ttl ?? MIN_5
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Curried `withCache`: first call it with your options,
|
|
29
|
+
* then call the returned function with your async function.
|
|
30
|
+
*/
|
|
31
|
+
function withCache(originalFunc, options = {}) {
|
|
32
|
+
const defaultTTL = 300 * 1e3;
|
|
33
|
+
const cachedFunction = async (...args) => {
|
|
34
|
+
const cacheKey = options?.key ?? originalFunc.name;
|
|
35
|
+
const cacheTTL = options?.ttl ?? defaultTTL;
|
|
36
|
+
const cachedData = get(cacheKey);
|
|
37
|
+
if (cachedData !== void 0) return cachedData;
|
|
38
|
+
const result = await originalFunc(...args);
|
|
39
|
+
set(cacheKey, result, cacheTTL);
|
|
40
|
+
return result;
|
|
41
|
+
};
|
|
42
|
+
return cachedFunction;
|
|
43
|
+
}
|
|
44
|
+
var MemoryCache = class {
|
|
45
|
+
logger = new Logger("MemoryCache");
|
|
46
|
+
_cache = {};
|
|
47
|
+
get(key) {
|
|
48
|
+
const cachedItem = this._cache[key];
|
|
49
|
+
if (!cachedItem) return;
|
|
50
|
+
if (Date.now() - cachedItem.timestamp > cachedItem.ttl) delete this._cache[key];
|
|
51
|
+
else return cachedItem.data;
|
|
52
|
+
}
|
|
53
|
+
async set(key, value, ttl = Infinity) {
|
|
54
|
+
this._cache[key] = {
|
|
55
|
+
data: value,
|
|
56
|
+
timestamp: Date.now(),
|
|
57
|
+
ttl
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
async delete(key) {
|
|
61
|
+
delete this._cache[key];
|
|
62
|
+
}
|
|
63
|
+
async clear() {
|
|
64
|
+
this._cache = {};
|
|
65
|
+
}
|
|
66
|
+
isExpired(key) {
|
|
67
|
+
const cachedItem = this._cache[key];
|
|
68
|
+
if (!cachedItem) return true;
|
|
69
|
+
if (Date.now() - cachedItem.timestamp > cachedItem.ttl) {
|
|
70
|
+
delete this._cache[key];
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
has(key) {
|
|
76
|
+
const cachedItem = this._cache[key];
|
|
77
|
+
if (!cachedItem) return false;
|
|
78
|
+
if (this.isExpired(key)) return false;
|
|
79
|
+
if (Date.now() - cachedItem.timestamp > cachedItem.ttl) {
|
|
80
|
+
delete this._cache[key];
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
keys() {
|
|
86
|
+
return Object.keys(this._cache);
|
|
87
|
+
}
|
|
88
|
+
size() {
|
|
89
|
+
return Object.keys(this._cache).length;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
export { MIN_1, MIN_10, MIN_15, MIN_3, MIN_5, MemoryCache, SEC_30, get, set, withCache };
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@melledijkstra/storage",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "Melle Dijkstra",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"require": "./dist/index.cjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@melledijkstra/toolbox": "1.0.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@melledijkstra/config": "1.0.0"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"dev": "tsdown --watch",
|
|
27
|
+
"build": "tsdown"
|
|
28
|
+
}
|
|
29
|
+
}
|