diff --git a/build.js b/build.js deleted file mode 100644 index e69de29..0000000 diff --git a/dev.js b/dev.js deleted file mode 100644 index f565d90..0000000 --- a/dev.js +++ /dev/null @@ -1,2 +0,0 @@ -import cachu from './src/mjs/index.js' -// import cachu from 'cachu' \ No newline at end of file diff --git a/dev/instance.mjs b/dev/instance.mjs new file mode 100644 index 0000000..1d3cf02 --- /dev/null +++ b/dev/instance.mjs @@ -0,0 +1,25 @@ +import { Cachu } from '../src/instance.mjs' + +const cachu = new Cachu({ + maxAge: 300 +}) + +async function dev() { + await cachu.set(1, 'other') + + setTimeout(async () => { + await cachu.set(2, 'item') + }, 250) + + setTimeout(async () => { + await cachu.set(3, 'item') + }, 500) + + setTimeout(async () => { + console.log(await cachu.purgeManyByCondition(item => { + return item[1] === 'other' + })) + console.log(cachu.store) + }, 1000) +} +dev() \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 45a8e33..f8daf08 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,12 +9,12 @@ "version": "2.0.0", "license": "Apache-2.0", "devDependencies": { - "fs-extra": "^10.0.0", + "esbuild": "^0.13.11", "jest": "^27.3.1", "terser": "^5.9.0" }, "engines": { - "node": ">=16.12.0", + "node": ">=16.13.0", "npm": ">=8.1.0" }, "funding": { @@ -1626,6 +1626,256 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, + "node_modules/esbuild": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.13.11.tgz", + "integrity": "sha512-LeKT6wgEJX+U78L8O6Me71NtV18pO2iGJv3hBeOPMk+wArxHvQqv/cU8uZ97i+rge25DOrLNKBlzN3ofyoS1sg==", + "dev": true, + "hasInstallScript": true, + "bin": { + "esbuild": "bin/esbuild" + }, + "optionalDependencies": { + "esbuild-android-arm64": "0.13.11", + "esbuild-darwin-64": "0.13.11", + "esbuild-darwin-arm64": "0.13.11", + "esbuild-freebsd-64": "0.13.11", + "esbuild-freebsd-arm64": "0.13.11", + "esbuild-linux-32": "0.13.11", + "esbuild-linux-64": "0.13.11", + "esbuild-linux-arm": "0.13.11", + "esbuild-linux-arm64": "0.13.11", + "esbuild-linux-mips64le": "0.13.11", + "esbuild-linux-ppc64le": "0.13.11", + "esbuild-netbsd-64": "0.13.11", + "esbuild-openbsd-64": "0.13.11", + "esbuild-sunos-64": "0.13.11", + "esbuild-windows-32": "0.13.11", + "esbuild-windows-64": "0.13.11", + "esbuild-windows-arm64": "0.13.11" + } + }, + "node_modules/esbuild-android-arm64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-android-arm64/-/esbuild-android-arm64-0.13.11.tgz", + "integrity": "sha512-5D6wfGwqbYcsj2Rtsdilt7TFi1i2vo/OcDNc+bBzdveQ0dgsXq07ImnHZ1fjKCjvQrjZz2oDCvKyMso3YyTEtQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ] + }, + "node_modules/esbuild-darwin-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-darwin-64/-/esbuild-darwin-64-0.13.11.tgz", + "integrity": "sha512-5SLEfyBVyTjTWBD8bMUsCsFmiiYQQhcA29mjOfooPVbk8Xq4pc0Cl8l+XQStO5ntnX5xh8su+z4LGCJ15rE2Gw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/esbuild-darwin-arm64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-darwin-arm64/-/esbuild-darwin-arm64-0.13.11.tgz", + "integrity": "sha512-davfRUaAqC1dAaN/Re0yG7wHj30EMw0EzzCoUfIAzuiuwHTJk9vwCZ+eVVz5n7cDy/mtaYLUJTNcmeVnperGIQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ] + }, + "node_modules/esbuild-freebsd-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-freebsd-64/-/esbuild-freebsd-64-0.13.11.tgz", + "integrity": "sha512-xQS/TuL7nBWwhYilIUHGhce0dLLMG+fsn8X6r78rDVEuSZPoTmP4ClVkM6uW+3OaKMBrel1VUXnFJ79Mg/n+lw==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ] + }, + "node_modules/esbuild-freebsd-arm64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-freebsd-arm64/-/esbuild-freebsd-arm64-0.13.11.tgz", + "integrity": "sha512-xU5G1UL/TWhgar9qAjDmnFVRWI//XFTHrG3SCpyhSMgOqrlD0ySgW1SSXEBVHfQu0JIEKQZ0uQpreS0pXt20/A==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ] + }, + "node_modules/esbuild-linux-32": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-32/-/esbuild-linux-32-0.13.11.tgz", + "integrity": "sha512-P2a4eEtpRP6KHzTY30xEVOFpIFVZoa48/bjB+V5jXGRk5iKSsX4Akq3amXotz66GDBSEiaz1uSJ1Nu80HfA1Mw==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/esbuild-linux-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-64/-/esbuild-linux-64-0.13.11.tgz", + "integrity": "sha512-Lk5JfLpQhaLGK+h4WFc8GQNP+uX5olqQLcomROlKUjxuAn+T+8+EI6CEX5FANu/Y/QlzC57zfvkxqRRUAZ9bdg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/esbuild-linux-arm": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-arm/-/esbuild-linux-arm-0.13.11.tgz", + "integrity": "sha512-K+pYBQr5AAyqe0D4GSFzZF3qnoUf2HG6W4ox8L5dYe20fTfq6o9CaRQpcp7nCYzM1ZSOJR1gK/Xzsn1YRp5lpg==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/esbuild-linux-arm64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-arm64/-/esbuild-linux-arm64-0.13.11.tgz", + "integrity": "sha512-wOPaXH5Z0qkT2dsm7nWJ44jysXUVQ3IS6a0lXprE3AYLt7PCqATrlXOMwuQngJEMqxxvM9svaI3lTqU+E9sxlQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/esbuild-linux-mips64le": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-mips64le/-/esbuild-linux-mips64le-0.13.11.tgz", + "integrity": "sha512-pupByBqNyyfFYfsIpprkie62NdZ424LmMhWWMfLav59DLOoG3Tn9DBam5lLeVX4eW1SCZgsGcfBAoaReCK7TZg==", + "cpu": [ + "mips64el" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/esbuild-linux-ppc64le": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-ppc64le/-/esbuild-linux-ppc64le-0.13.11.tgz", + "integrity": "sha512-6F5n+XPLBfLjzSWvY32hS0b9UxLMtrcGD+1fCAdzXGSyT4Z35LsDTPZtFHHdczfvaGqsoIaEwbdihoOTg86uow==", + "cpu": [ + "ppc64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ] + }, + "node_modules/esbuild-netbsd-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-netbsd-64/-/esbuild-netbsd-64-0.13.11.tgz", + "integrity": "sha512-0/DInmj0wcFewOmK8Ek8dkMLRev3/9eZyYhruqhZdjWq4hfi9APnn3gOxgj3dDo3Q4Dhc8pFqA9SpxD5KXGH2Q==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "netbsd" + ] + }, + "node_modules/esbuild-openbsd-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-openbsd-64/-/esbuild-openbsd-64-0.13.11.tgz", + "integrity": "sha512-QkWZt2D/XGD3gejRTX0mQx3oPuYTdSpLJ4H9cQlsec/WNCRW17DDqb61UE38PozMKO2EfpE+znWc/uLoA7tyGA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "openbsd" + ] + }, + "node_modules/esbuild-sunos-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-sunos-64/-/esbuild-sunos-64-0.13.11.tgz", + "integrity": "sha512-mQ6BjPBsajbSzqMJZHodS2Z2UCtrUftFQR/cWPMyL/y7sF/OJwgpG4vif1jxCPB/rgU239fTyIJnwQHblqpkjQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "sunos" + ] + }, + "node_modules/esbuild-windows-32": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-windows-32/-/esbuild-windows-32-0.13.11.tgz", + "integrity": "sha512-CBrsaverrBed3wo+fz9uMtmEaaetUGv29rXbZipFX1wqlFOFFmrA3l97P/4MLOIP8NlOZheVLwEdKWiT5dIV4Q==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/esbuild-windows-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-windows-64/-/esbuild-windows-64-0.13.11.tgz", + "integrity": "sha512-Lg3XTGO8jfYEJnnErqbMeXlRd4SiB2gYqsbH828cy8z52fsGOWEfmVyyzRYHFnMOVXHnV9njk8aj/gvdzhXtuQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ] + }, + "node_modules/esbuild-windows-arm64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-windows-arm64/-/esbuild-windows-arm64-0.13.11.tgz", + "integrity": "sha512-D9k0+gZhyTreG8UDYV4O/TTA9zcvnCKI+FnoS+Qun+2nD9CDXkLkA2jSJIPAme1dzQoMJpxwkOVWjLXKkMDc+Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ] + }, "node_modules/escalade": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", @@ -1818,29 +2068,6 @@ "node": ">= 6" } }, - "node_modules/fs-extra": { - "version": "10.0.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.0.tgz", - "integrity": "sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==", - "dev": true, - "dependencies": { - "graceful-fs": "^4.2.0", - "jsonfile": "^6.0.1", - "universalify": "^2.0.0" - }, - "engines": { - "node": ">=12" - } - }, - "node_modules/fs-extra/node_modules/universalify": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", - "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", - "dev": true, - "engines": { - "node": ">= 10.0.0" - } - }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -2928,27 +3155,6 @@ "node": ">=6" } }, - "node_modules/jsonfile": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", - "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", - "dev": true, - "dependencies": { - "universalify": "^2.0.0" - }, - "optionalDependencies": { - "graceful-fs": "^4.1.6" - } - }, - "node_modules/jsonfile/node_modules/universalify": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", - "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", - "dev": true, - "engines": { - "node": ">= 10.0.0" - } - }, "node_modules/kleur": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", @@ -5318,6 +5524,150 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, + "esbuild": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.13.11.tgz", + "integrity": "sha512-LeKT6wgEJX+U78L8O6Me71NtV18pO2iGJv3hBeOPMk+wArxHvQqv/cU8uZ97i+rge25DOrLNKBlzN3ofyoS1sg==", + "dev": true, + "requires": { + "esbuild-android-arm64": "0.13.11", + "esbuild-darwin-64": "0.13.11", + "esbuild-darwin-arm64": "0.13.11", + "esbuild-freebsd-64": "0.13.11", + "esbuild-freebsd-arm64": "0.13.11", + "esbuild-linux-32": "0.13.11", + "esbuild-linux-64": "0.13.11", + "esbuild-linux-arm": "0.13.11", + "esbuild-linux-arm64": "0.13.11", + "esbuild-linux-mips64le": "0.13.11", + "esbuild-linux-ppc64le": "0.13.11", + "esbuild-netbsd-64": "0.13.11", + "esbuild-openbsd-64": "0.13.11", + "esbuild-sunos-64": "0.13.11", + "esbuild-windows-32": "0.13.11", + "esbuild-windows-64": "0.13.11", + "esbuild-windows-arm64": "0.13.11" + } + }, + "esbuild-android-arm64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-android-arm64/-/esbuild-android-arm64-0.13.11.tgz", + "integrity": "sha512-5D6wfGwqbYcsj2Rtsdilt7TFi1i2vo/OcDNc+bBzdveQ0dgsXq07ImnHZ1fjKCjvQrjZz2oDCvKyMso3YyTEtQ==", + "dev": true, + "optional": true + }, + "esbuild-darwin-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-darwin-64/-/esbuild-darwin-64-0.13.11.tgz", + "integrity": "sha512-5SLEfyBVyTjTWBD8bMUsCsFmiiYQQhcA29mjOfooPVbk8Xq4pc0Cl8l+XQStO5ntnX5xh8su+z4LGCJ15rE2Gw==", + "dev": true, + "optional": true + }, + "esbuild-darwin-arm64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-darwin-arm64/-/esbuild-darwin-arm64-0.13.11.tgz", + "integrity": "sha512-davfRUaAqC1dAaN/Re0yG7wHj30EMw0EzzCoUfIAzuiuwHTJk9vwCZ+eVVz5n7cDy/mtaYLUJTNcmeVnperGIQ==", + "dev": true, + "optional": true + }, + "esbuild-freebsd-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-freebsd-64/-/esbuild-freebsd-64-0.13.11.tgz", + "integrity": "sha512-xQS/TuL7nBWwhYilIUHGhce0dLLMG+fsn8X6r78rDVEuSZPoTmP4ClVkM6uW+3OaKMBrel1VUXnFJ79Mg/n+lw==", + "dev": true, + "optional": true + }, + "esbuild-freebsd-arm64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-freebsd-arm64/-/esbuild-freebsd-arm64-0.13.11.tgz", + "integrity": "sha512-xU5G1UL/TWhgar9qAjDmnFVRWI//XFTHrG3SCpyhSMgOqrlD0ySgW1SSXEBVHfQu0JIEKQZ0uQpreS0pXt20/A==", + "dev": true, + "optional": true + }, + "esbuild-linux-32": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-32/-/esbuild-linux-32-0.13.11.tgz", + "integrity": "sha512-P2a4eEtpRP6KHzTY30xEVOFpIFVZoa48/bjB+V5jXGRk5iKSsX4Akq3amXotz66GDBSEiaz1uSJ1Nu80HfA1Mw==", + "dev": true, + "optional": true + }, + "esbuild-linux-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-64/-/esbuild-linux-64-0.13.11.tgz", + "integrity": "sha512-Lk5JfLpQhaLGK+h4WFc8GQNP+uX5olqQLcomROlKUjxuAn+T+8+EI6CEX5FANu/Y/QlzC57zfvkxqRRUAZ9bdg==", + "dev": true, + "optional": true + }, + "esbuild-linux-arm": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-arm/-/esbuild-linux-arm-0.13.11.tgz", + "integrity": "sha512-K+pYBQr5AAyqe0D4GSFzZF3qnoUf2HG6W4ox8L5dYe20fTfq6o9CaRQpcp7nCYzM1ZSOJR1gK/Xzsn1YRp5lpg==", + "dev": true, + "optional": true + }, + "esbuild-linux-arm64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-arm64/-/esbuild-linux-arm64-0.13.11.tgz", + "integrity": "sha512-wOPaXH5Z0qkT2dsm7nWJ44jysXUVQ3IS6a0lXprE3AYLt7PCqATrlXOMwuQngJEMqxxvM9svaI3lTqU+E9sxlQ==", + "dev": true, + "optional": true + }, + "esbuild-linux-mips64le": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-mips64le/-/esbuild-linux-mips64le-0.13.11.tgz", + "integrity": "sha512-pupByBqNyyfFYfsIpprkie62NdZ424LmMhWWMfLav59DLOoG3Tn9DBam5lLeVX4eW1SCZgsGcfBAoaReCK7TZg==", + "dev": true, + "optional": true + }, + "esbuild-linux-ppc64le": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-linux-ppc64le/-/esbuild-linux-ppc64le-0.13.11.tgz", + "integrity": "sha512-6F5n+XPLBfLjzSWvY32hS0b9UxLMtrcGD+1fCAdzXGSyT4Z35LsDTPZtFHHdczfvaGqsoIaEwbdihoOTg86uow==", + "dev": true, + "optional": true + }, + "esbuild-netbsd-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-netbsd-64/-/esbuild-netbsd-64-0.13.11.tgz", + "integrity": "sha512-0/DInmj0wcFewOmK8Ek8dkMLRev3/9eZyYhruqhZdjWq4hfi9APnn3gOxgj3dDo3Q4Dhc8pFqA9SpxD5KXGH2Q==", + "dev": true, + "optional": true + }, + "esbuild-openbsd-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-openbsd-64/-/esbuild-openbsd-64-0.13.11.tgz", + "integrity": "sha512-QkWZt2D/XGD3gejRTX0mQx3oPuYTdSpLJ4H9cQlsec/WNCRW17DDqb61UE38PozMKO2EfpE+znWc/uLoA7tyGA==", + "dev": true, + "optional": true + }, + "esbuild-sunos-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-sunos-64/-/esbuild-sunos-64-0.13.11.tgz", + "integrity": "sha512-mQ6BjPBsajbSzqMJZHodS2Z2UCtrUftFQR/cWPMyL/y7sF/OJwgpG4vif1jxCPB/rgU239fTyIJnwQHblqpkjQ==", + "dev": true, + "optional": true + }, + "esbuild-windows-32": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-windows-32/-/esbuild-windows-32-0.13.11.tgz", + "integrity": "sha512-CBrsaverrBed3wo+fz9uMtmEaaetUGv29rXbZipFX1wqlFOFFmrA3l97P/4MLOIP8NlOZheVLwEdKWiT5dIV4Q==", + "dev": true, + "optional": true + }, + "esbuild-windows-64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-windows-64/-/esbuild-windows-64-0.13.11.tgz", + "integrity": "sha512-Lg3XTGO8jfYEJnnErqbMeXlRd4SiB2gYqsbH828cy8z52fsGOWEfmVyyzRYHFnMOVXHnV9njk8aj/gvdzhXtuQ==", + "dev": true, + "optional": true + }, + "esbuild-windows-arm64": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/esbuild-windows-arm64/-/esbuild-windows-arm64-0.13.11.tgz", + "integrity": "sha512-D9k0+gZhyTreG8UDYV4O/TTA9zcvnCKI+FnoS+Qun+2nD9CDXkLkA2jSJIPAme1dzQoMJpxwkOVWjLXKkMDc+Q==", + "dev": true, + "optional": true + }, "escalade": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", @@ -5457,25 +5807,6 @@ "mime-types": "^2.1.12" } }, - "fs-extra": { - "version": "10.0.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.0.tgz", - "integrity": "sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==", - "dev": true, - "requires": { - "graceful-fs": "^4.2.0", - "jsonfile": "^6.0.1", - "universalify": "^2.0.0" - }, - "dependencies": { - "universalify": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", - "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", - "dev": true - } - } - }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -6305,24 +6636,6 @@ "minimist": "^1.2.5" } }, - "jsonfile": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", - "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", - "dev": true, - "requires": { - "graceful-fs": "^4.1.6", - "universalify": "^2.0.0" - }, - "dependencies": { - "universalify": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", - "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", - "dev": true - } - } - }, "kleur": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/kleur/-/kleur-3.0.3.tgz", diff --git a/package.json b/package.json index 929476e..e398d81 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,11 @@ "name": "cachu", "version": "2.0.0", "description": "Simple, minimalistic key-value cache.", - "main": "dist/index.js", + "main": "build/index.js", "scripts": { - "test": "jest dev.js" + "build": "esbuild src/index.mjs --bundle --platform=node --outfile=build/index.js && terser build/index.js --output build/index.js", + "test": "node dev/instance.mjs" }, - "types": "./types/index.d.ts", "funding": { "type": "individual", "url": "https://ko-fi.com/azury" @@ -29,13 +29,12 @@ }, "homepage": "https://github.com/azurystudios/cachu#readme", "devDependencies": { - "fs-extra": "^10.0.0", + "esbuild": "^0.13.11", "jest": "^27.3.1", "terser": "^5.9.0" }, "engines": { "node": ">=16.13.0", "npm": ">=8.1.0" - }, - "type": "module" + } } diff --git a/src/global.js b/src/global.js deleted file mode 100644 index b0c1ff7..0000000 --- a/src/global.js +++ /dev/null @@ -1,189 +0,0 @@ -let store = [] -, maxAmount = Infinity -, maxAge = Infinity - -async function getGlobalMaxAmount() { - return maxAmount -} - -async function getGlobalMaxAge() { - return maxAge -} - -async function global(options) { - if (typeof options !== 'object') return false - - maxAmount = options.maxAmount ?? Infinity - maxAge = options.maxAge ?? Infinity -} - -async function setItem(key, value) { - if (!key || !value) return false - - const index = store.findIndex(i => i.key === key) - if (index !== -1) return false - - if (store.length === max) { - const max = store.reduce((prev, current) => (prev.maxAge < current.maxAge) ? prev : current) - const i = store.findIndex(i => i.key === max.key) - store.splice(i, 1) - } - - const item = { - key: key, - value: value, - createdAt: Date.now() - } - - store = store.filter(i => (Date.now() - i.createdAt < maxAge * 1000) && (i.key !== key)) - store.push(item) - - return true -} - -async function getItem(key) { - if (!key) return null - - const index = store.findIndex(i => i.key === key) - if (index === -1) return null - - store = store.filter(i => (Date.now() - i.createdAt < maxAge * 1000) || (i.key === key)) - - const item = store[index] - item.createdAt = Date.now() - - return item.value -} - -async function updateItem(key, value) { - if (!key || !value) return false - - const index = store.findIndex(i => i.key === key) - if (index === -1) return false - - store[index] = { - key: key, - value: value, - createdAt: Date.now() - } - - return true -} - -async function viewItem(key) { - if (!key) return null - - const index = store.findIndex(i => i.key === key) - if (index === -1) return null - - const item = store[index] - return item.value -} - -async function hasItem(key) { - try { - if (!key) return false - - const index = store.findIndex(i => i.key === key) - if (index === -1) return false - } catch { - return false - } - - return true -} - -async function deleteItem(key) { - try { - if (!key) return false - - const index = store.findIndex(i => i.key === key) - if (index === -1) return false - - store.splice(index, 1) - } catch { - return false - } - - return true -} - -async function prune() { - try { - store = store.filter(i => Date.now() - i.createdAt < maxAge * 1000) - } catch { - return false - } - - return true -} - -async function purge() { - store = [] - - return true -} - -async function getItems() { - try { - return store.map(i => delete i.createdAt) - } catch { - return null - } -} - -async function getAmountOfItems() { - return store.length -} - -async function getItemsByCondition(condition) { - if (!condition) return null - - return store.filter(i => !condition(i)) -} - -async function purgeItemsByCondition(condition) { - if (!condition) return false - - store = store.filter(i => !condition(i)) - - return true -} - -async function getValuesOfItems() { - if (store.length === 0) return null - - return store.map(i => { - delete i.key - delete i.createdAt - }) -} - -async function getKeysOfItems() { - if (store.length === 0) return null - - return store.map(i => { - delete i.value - delete i.createdAt - }) -} - -export { - getGlobalMaxAmount, - getGlobalMaxAge, - global, - setItem, - getItem, - updateItem, - viewItem, - hasItem, - deleteItem, - prune, - purge, - getItems, - getAmountOfItems, - getItemsByCondition, - purgeItemsByCondition, - getValuesOfItems, - getKeysOfItems -} \ No newline at end of file diff --git a/src/global.mjs b/src/global.mjs new file mode 100644 index 0000000..81f81f1 --- /dev/null +++ b/src/global.mjs @@ -0,0 +1,195 @@ +let store = [] +, maxAmount = Infinity +, maxAge = Infinity + +export default async (config) => { + if (typeof config !== 'object') return + + maxAmount = config.maxAmount ?? Infinity + maxAge = config.maxAge ?? Infinity +} + +export const set = async (key, value) => { + // validate params + if (!key || !value) return false + + // validate uniqueness of key + const index = store.findIndex(i => i[0] === key) + if (index !== -1) return false + + // remove outdated items + store = store.filter(i => Date.now() - i[2] < maxAge * 1000) + + // remove oldest item if store holds too many items + if (store.length === maxAmount) { + const max = store.reduce((previous, current) => (previous[2] < current[2]) ? previous : current) + store.splice(store.findIndex(i => i[0] === max[0]), 1) + } + + // add new item + store.push([key, value, Date.now()]) + + return true +} + +export const get = async (key) => { + // validate params + if (!key) return null + + // remove outdated items + store = store.filter(i => Date.now() - i[2] < maxAge * 1000) + + // search for item + const index = store.findIndex(i => i[0] === key) + if (index === -1) return null + + // get item + const item = store[index] + + // return value of item + return item[1] +} + +export const view = async (key) => { + // validate params + if (!key) return null + + // remove outdated items + store = store.filter(i => (Date.now() - i[2] < maxAge * 1000) || (i[0] === key)) + + // search for item + const index = store.findIndex(i => i[0] === key) + if (index === -1) return null + + // get item + const item = store[index] + + // return value of item + return item[1] +} + +export const update = async (key, value) => { + // validate params + if (!key || !value) return false + + // search for item + const index = store.findIndex(i => i[0] === key) + if (index === -1) return false + + // update item + store[index] = [key, value, Date.now()] + + return true +} + +export const has = async (key) => { + // validate params + if (!key) return false + + // search for item + const index = store.findIndex(i => i[0] === key) + if (index === -1) return false + + return true +} + +export const purge = async (key) => { + // validate params + if (!key) return false + + // search for item + const index = store.findIndex(i => i[0] === key) + if (index === -1) return false + + // remove item + store.splice(index, 1) + + return true +} + +export const prune = async () => { + // remove outdated items + if (store.length > 0) store = store.filter(i => Date.now() - i[2] < maxAge * 1000) + + return true +} + +export const destroy = async () => { + // reset store + store = [] + + return true +} + +export const purgeMany = async (keys) => { + // validate params + if (typeof keys !== 'object') return false + + // purge each item + for (const key of keys) + await purge(key) + + return true +} + +export const getMany = async (keys) => { + // validate params + if (typeof keys !== 'object') return + + // create the array + const items = [] + + // get each item + for (const key of keys) { + const item = await get(key) + if (item !== null || item !== undefined) items.push(item) + } + + return items +} + +export const purgeManyByCondition = async (condition) => { + // validate params + if (typeof condition !== 'function') return false + + // purge each item + store = store.filter(item => !condition(item)) + + return true +} + +export const getManyByCondition = async (condition) => { + // validate params + if (typeof condition !== 'function') return false + + // return items + return store.filter(item => condition(item)) +} + +export const getAmountOfItems = async () => { + return store.length +} + +export const getValuesOfItems = async () => { + if (store.length === 0) return + + return store.map(item => item[1]) +} + +export const getKeysOfItems = async () => { + if (store.length === 0) return + + return store.map(item => item[0]) +} + +export const each = async (action) => { + // validate params + if (!action) return false + + // execute action on each item + store.forEach(async item => { + await action(item) + }) + + return true +} \ No newline at end of file diff --git a/src/index.js b/src/index.js deleted file mode 100644 index f9658e3..0000000 --- a/src/index.js +++ /dev/null @@ -1,21 +0,0 @@ -export { instance } from './instance.js' - -import global from './global.js' -export default global - -export { - setItem, - getItem, - updateItem, - viewItem, - hasItem, - deleteItem, - prune, - purge, - getItems, - getAmountOfItems, - getItemsByCondition, - purgeItemsByCondition, - getValuesOfItems, - getKeysOfItems -} from './global.js' \ No newline at end of file diff --git a/src/index.mjs b/src/index.mjs new file mode 100644 index 0000000..693ffab --- /dev/null +++ b/src/index.mjs @@ -0,0 +1,23 @@ +export { Cachu } from './instance.mjs' + +import global from './global.mjs' +export default global + +export { + set, + get, + update, + view, + purge, + has, + prune, + destroy, + purgeMany, + getMany, + getAmountOfItems, + getManyByCondition, + purgeManyByCondition, + getValuesOfItems, + getKeysOfItems, + each +} from './global.mjs' \ No newline at end of file diff --git a/src/instance.js b/src/instance.js deleted file mode 100644 index 8aa33a6..0000000 --- a/src/instance.js +++ /dev/null @@ -1,162 +0,0 @@ -import { getGlobalMaxAmount, getGlobalMaxAge } from './global.js' - -export class instance { - constructor(options) { - if (typeof options !== 'object') return false - - this.maxAmount = options.maxAmount ?? await getGlobalMaxAmount() - this.maxAge = options.maxAge ?? await getGlobalMaxAge() - this.store = [] - } - - setItem = async () => { - if (!key || !value) return false - - const index = this.store.findIndex(i => i.key === key) - if (index !== -1) return false - - if (this.store.length === this.maxAmount) { - const max = this.store.reduce((prev, current) => (prev.maxAge < current.maxAge) ? prev : current) - const i = this.store.findIndex(i => i.key === max.key) - this.store.splice(i, 1) - } - - const item = { - key: key, - value: value, - createdAt: Date.now() - } - - this.store = this.store.filter(i => (Date.now() - i.createdAt < this.maxAge * 1000) && (i.key !== key)) - this.store.push(item) - - return true - } - - getItem = async (key) => { - if (!key) return null - - const index = this.store.findIndex(i => i.key === key) - if (index === -1) return null - - this.store = this.store.filter(i => (Date.now() - i.createdAt < this.maxAge * 1000) || (i.key === key)) - - const item = this.store[index] - item.createdAt = Date.now() - - return item.value - } - - updateItem = async (key, value) => { - if (!key || !value) return false - - const index = this.store.findIndex(i => i.key === key) - if (index === -1) return false - - this.store[index] = { - key: key, - value: value, - createdAt: Date.now() - } - - return true - } - - viewItem = async (key) => { - if (!key) return null - - const index = this.store.findIndex(i => i.key === key) - if (index === -1) return null - - const item = this.store[index] - return item.value - } - - hasItem = async (key) => { - try { - if (!key) return false - - const index = this.store.findIndex(i => i.key === key) - if (index === -1) return false - } catch { - return false - } - - return true - } - - deleteItem = async (key) => { - try { - if (!key) return false - - const index = this.store.findIndex(i => i.key === key) - if (index === -1) return false - - this.store.splice(index, 1) - } catch { - return false - } - - return true - } - - prune = async () => { - try { - this.store = this.store.filter(i => Date.now() - i.createdAt < this.maxAge * 1000) - } catch { - return false - } - - return true - } - - purge = async () => { - this.store = [] - - return true - } - - getItems = async () => { - try { - return this.store.map(i => delete i.createdAt) - } catch { - return null - } - } - - getAmountOfItems = async () => { - return this.store.length - } - - getItemsByCondition = async (condition) => { - if (!condition) return null - - return this.store.filter(i => !condition(i)) - } - - purgeItemsByCondition = async (condition) => { - if (!condition) return false - - this.store = this.store.filter(i => !condition(i)) - - return true - } - - getValuesOfItems = async () => { - if (this.store.length === 0) return null - - return this.store.map(i => { - delete i.key - delete i.createdAt - }) - } - - getKeysOfItems = async () => { - if (this.store.length === 0) return null - - return this.store.map(i => { - delete i.value - delete i.createdAt - }) - } -} diff --git a/src/instance.mjs b/src/instance.mjs new file mode 100644 index 0000000..b754792 --- /dev/null +++ b/src/instance.mjs @@ -0,0 +1,196 @@ +export class Cachu { + constructor(config) { + // validate config + if (typeof config !== 'object' || (config.maxAmount && typeof config.maxAmount !== 'number') || (config.maxAge && typeof config.maxAge !== 'number')) return + + // set config + this.maxAmount = config.maxAmount ?? Infinity + this.maxAge = config.maxAge ?? Infinity + this.store = [] + } + + set = async (key, value) => { + // validate params + if (!key || !value) return false + + // validate uniqueness of key + const index = this.store.findIndex(i => i[0] === key) + if (index !== -1) return false + + // remove outdated items + this.store = this.store.filter(i => Date.now() - i[2] < this.maxAge * 1000) + + // remove oldest item if store holds too many items + if (this.store.length === this.maxAmount) { + const max = this.store.reduce((previous, current) => (previous[2] < current[2]) ? previous : current) + this.store.splice(this.store.findIndex(i => i[0] === max[0]), 1) + } + + // add new item + this.store.push([key, value, Date.now()]) + + return true + } + + get = async (key) => { + // validate params + if (!key) return null + + // remove outdated items + this.store = this.store.filter(i => Date.now() - i[2] < this.maxAge * 1000) + + // search for item + const index = this.store.findIndex(i => i[0] === key) + if (index === -1) return null + + // get item + const item = this.store[index] + + // return value of item + return item[1] + } + + view = async (key) => { + // validate params + if (!key) return null + + // remove outdated items + this.store = this.store.filter(i => (Date.now() - i[2] < this.maxAge * 1000) || (i[0] === key)) + + // search for item + const index = this.store.findIndex(i => i[0] === key) + if (index === -1) return null + + // get item + const item = this.store[index] + + // return value of item + return item[1] + } + + update = async (key, value) => { + // validate params + if (!key || !value) return false + + // search for item + const index = this.store.findIndex(i => i[0] === key) + if (index === -1) return false + + // update item + this.store[index] = [key, value, Date.now()] + + return true + } + + has = async (key) => { + // validate params + if (!key) return false + + // search for item + const index = this.store.findIndex(i => i[0] === key) + if (index === -1) return false + + return true + } + + purge = async (key) => { + // validate params + if (!key) return false + + // search for item + const index = this.store.findIndex(i => i[0] === key) + if (index === -1) return false + + // remove item + this.store.splice(index, 1) + + return true + } + + prune = async () => { + // remove outdated items + if (this.store.length > 0) this.store = this.store.filter(i => Date.now() - i[2] < this.maxAge * 1000) + + return true + } + + destroy = async () => { + // reset store + this.store = [] + + return true + } + + purgeMany = async (keys) => { + // validate params + if (typeof keys !== 'object') return false + + // purge each item + for (const key of keys) + await this.purge(key) + + return true + } + + getMany = async (keys) => { + // validate params + if (typeof keys !== 'object') return + + // create the array + const items = [] + + // get each item + for (const key of keys) { + const item = await this.get(key) + if (item !== null || item !== undefined) items.push(item) + } + + return items + } + + purgeManyByCondition = async (condition) => { + // validate params + if (typeof condition !== 'function') return false + + // purge each item + this.store = this.store.filter(item => !condition(item)) + + return true + } + + getManyByCondition = async (condition) => { + // validate params + if (typeof condition !== 'function') return false + + // return items + return this.store.filter(item => condition(item)) + } + + getAmountOfItems = async () => { + return this.store.length + } + + getValuesOfItems = async () => { + if (this.store.length === 0) return + + return this.store.map(item => item[1]) + } + + getKeysOfItems = async () => { + if (this.store.length === 0) return + + return this.store.map(item => item[0]) + } + + each = async (action) => { + // validate params + if (!action) return false + + // execute action on each item + this.store.forEach(async item => { + await action(item) + }) + + return true + } +} diff --git a/types/cachu.d.ts b/types/cachu.d.ts deleted file mode 100644 index cc3815b..0000000 --- a/types/cachu.d.ts +++ /dev/null @@ -1,162 +0,0 @@ -export default class cachu { - private max: Number - private maxAge: Number - private store: Array - - constructor(max?: Number, maxAge?: Number) { - this.max = max ?? Infinity - this.maxAge = maxAge ?? Infinity - this.store = [Object] - } - - setItem = async (key: String|Number, value: any): Promise => { - if (!key || !value) return false - - const index: Number = this.store.findIndex((i: any) => i.key === key) - if (index !== -1) return false - - if (this.store.length === this.max) { - const max: any = this.store.reduce((prev: any, current: any) => (prev.maxAge < current.maxAge) ? prev : current) - const i = this.store.findIndex((i: any) => i.key === max.key) - this.store.splice(i, 1) - } - - const item: Object = { - key: key, - value: value, - createdAt: Date.now() - } - - this.store = this.store.filter((i: any) => (Date.now() - i.createdAt < this.maxAge) && (i.key !== key)) - this.store.push(item) - - return true - } - - getItem = async (key: String|Number): Promise => { - if (!key) return null - - const index = this.store.findIndex((i: any) => i.key === key) - if (index === -1) return null - - this.store = this.store.filter((i: any) => (Date.now() - i.createdAt < this.maxAge) || (i.key === key)) - - const item: any = this.store[index] - item.createdAt = Date.now() - - return item.value - } - - updateItem = async (key: String|Number, value: any): Promise => { - if (!key || !value) return false - - const index = this.store.findIndex((i: any) => i.key === key) - if (index === -1) return false - - this.store[index] = { - key: key, - value: value, - createdAt: Date.now() - } - - return true - } - - viewItem = async (key: String|Number): Promise => { - if (!key) return null - - const index = this.store.findIndex((i: any) => i.key === key) - if (index === -1) return null - - const item: any = this.store[index] - return item.value - } - - hasItem = async (key: String|Number): Promise => { - try { - if (!key) return false - - const index = this.store.findIndex((i: any) => i.key === key) - if (index === -1) return false - } catch { - return false - } - - return true - } - - deleteItem = async (key: String|Number): Promise => { - try { - if (!key) return false - - const index = this.store.findIndex((i: any) => i.key === key) - if (index === -1) return false - - this.store.splice(index, 1) - } catch { - return false - } - - return true - } - - prune = async (): Promise => { - try { - this.store = this.store.filter((i: any) => Date.now() - i.createdAt < this.maxAge) - } catch { - return false - } - - return true - } - - purge = async (): Promise => { - this.store = [] - - return true - } - - getItems = async (): Promise => { - try { - return this.store.map((i: any) => delete i.createdAt) - } catch { - return null - } - } - - getAmountOfItems = async (): Promise => { - return this.store.length - } - - getItemsByCondition = async (condition: Function): Promise => { - if (!condition) return null - - return this.store.filter(i => !condition(i)) - } - - purgeItemsByCondition = async (condition: Function): Promise => { - if (!condition) return false - - this.store = this.store.filter(i => !condition(i)) - - return true - } - - getValuesOfItems = async (): Promise => { - if (this.store.length === 0) return null - - return this.store.map((i: any) => { - delete i.key - delete i.createdAt - }) - } - - getKeysOfItems = async () => { - if (this.store.length === 0) return null - - return this.store.map((i: any) => { - delete i.value - delete i.createdAt - }) - } -} \ No newline at end of file