-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
93 lines (83 loc) · 2.61 KB
/
index.js
File metadata and controls
93 lines (83 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// kache.js - Avoxel284
// A really simple module used to cache items for code optimization
require("dotenv").config();
const fs = require("fs");
const path = require("path");
const _debug = false;
let _kacheFilePath = process.env["KACHE_PATH"];
if (_kacheFilePath != null) {
_kacheFilePath = path.join(process.cwd(), _kacheFilePath);
if (_debug) console.log(`Creating kache directory: ${_kacheFilePath}`);
fs.mkdir(_kacheFilePath, { recursive: true }, () => {});
_kacheFilePath = path.join(_kacheFilePath, "kache.json");
fs.writeFileSync(_kacheFilePath, "{}");
}
/**
* Direct access to the exposed cache
*/
exports.cache = {};
/**
* Get statistics about your kache
*/
exports.stats = () => {
return {
/** Kache external JSON file size in bytes */
FILE_SIZE: _kacheFilePath ? fs.statSync(_kacheFilePath).size : 0,
/** Amount of items stored */
ITEMS_STORED: Object.keys(exports.cache).length,
};
};
/**
* Stores an item in the cache
*
* @param {String} key Key to index the cache
* @param {any} value Value to enter in cache
*/
exports.store = (key, value) => {
key = key.toString();
if (!exports.cache[key] && _debug) console.log("kache.js // Overwriting value");
exports.cache[key] = value;
if (_kacheFilePath) fs.writeFileSync(_kacheFilePath, JSON.stringify(exports.cache));
return value;
};
/**
* Retrieves an item from the cache
*
* @example
* ```
* let foo = kache.r(bar) || getfoo(bar)
* ```
*
* @param {String} key Key to index the cache
*/
exports.retrieve = (key) => {
if (_debug) console.log(`kache.js // Retrieving value with key: ${key}`, exports.cache[key]);
return exports.cache[key];
};
/**
* An function that basically just makes your code shorter and life easier.
*
* @async Asynchronous function due the the nature that some retriever functions may be asynchronous
* @param {String} key Key to store the retrieved stuff under
* @param {Function} retrieverFunc The retriever function that returns a value. Can be async or not, I'll wait.
* @param {any} args Arguments to be passed to the retriver function (if any)
*/
exports.allInOne = async (key, retrieverFunc, ...args) => {
// The retriever function will be run when calling this function.
// Therefore, retrieverFunc will be any values returned.
key = key.toString();
if (exports.cache[key]) return exports.cache[key];
try {
const value = await retrieverFunc(...args);
if (_debug) console.log("kache.js // Using retriever function");
exports.store(key, value);
return value;
} catch (e) {
console.error(e);
return null;
}
};
// Shorthands
exports.s = exports.store;
exports.r = exports.retrieve;
exports.a = exports.allInOne;