Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cachu

Package Overview
Dependencies
Maintainers
2
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cachu - npm Package Compare versions

Comparing version 6.0.6 to 7.0.0

2

dist/index.js

@@ -1,1 +0,1 @@

var M=Object.create;var p=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var b=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var z=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var k=(e,t,s,c)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of A(t))!D.call(e,n)&&n!==s&&p(e,n,{get:()=>t[n],enumerable:!(c=x(t,n))||c.enumerable});return e};var C=(e,t,s)=>(s=e!=null?M(b(e)):{},k(t||!e||!e.__esModule?p(s,"default",{value:e,enumerable:!0}):s,e));var w=z((N,v)=>{var f=1e3,y=f*60,d=y*60,o=d*24,O=o*7,S=o*365.25;v.exports=function(e,t){t=t||{};var s=typeof e;if(s==="string"&&e.length>0)return F(e);if(s==="number"&&isFinite(e))return t.long?J(e):I(e);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(e))};function F(e){if(e=String(e),!(e.length>100)){var t=/^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(e);if(!!t){var s=parseFloat(t[1]),c=(t[2]||"ms").toLowerCase();switch(c){case"years":case"year":case"yrs":case"yr":case"y":return s*S;case"weeks":case"week":case"w":return s*O;case"days":case"day":case"d":return s*o;case"hours":case"hour":case"hrs":case"hr":case"h":return s*d;case"minutes":case"minute":case"mins":case"min":case"m":return s*y;case"seconds":case"second":case"secs":case"sec":case"s":return s*f;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return s;default:return}}}}function I(e){var t=Math.abs(e);return t>=o?Math.round(e/o)+"d":t>=d?Math.round(e/d)+"h":t>=y?Math.round(e/y)+"m":t>=f?Math.round(e/f)+"s":e+"ms"}function J(e){var t=Math.abs(e);return t>=o?m(e,t,o,"day"):t>=d?m(e,t,d,"hour"):t>=y?m(e,t,y,"minute"):t>=f?m(e,t,f,"second"):e+" ms"}function m(e,t,s,c){var n=t>=s*1.5;return Math.round(e/s)+" "+c+(n?"s":"")}});var l=C(w(),1),P=(e={})=>{let t=typeof e.maxAge=="string"?(0,l.default)(e.maxAge):e.maxAge?e.maxAge*1e3:(0,l.default)("10m"),s=e.maxAmount??1e4,c=e.autodelete??!1,n=new Map,h=async r=>{let a=Date.now(),i;for(let u of n)u[1].e>a||(a=u[1].e,i=u[0]);if(i&&n.delete(i),r--,r>0)return await h(r)},g=async()=>{for(let r of n)r[1].e<=Date.now()&&n.delete(r[0])};return{add:async(r,a,i)=>{c&&await g(),n.size+1>s&&await h(1),n.set(r,{v:a,e:Date.now()+(i?typeof i=="string"?(0,l.default)(i):i*1e3:t)})},addMany:async(...r)=>{c&&await g(),n.size+r.length>s&&await h(n.size+r.length-s);for(let[a,i,u]of r)n.set(a,{v:i,e:Date.now()+(u?typeof u=="string"?(0,l.default)(u):u:t)})},get:async r=>{let a=n.get(r);if(!!a){if(a.e>Date.now())return a.v;n.delete(r)}},getMany:async(...r)=>{let a=[];for(let i of r){let u=n.get(i);u?u.e<=Date.now()?(n.delete(i),a.push(void 0)):a.push(u.v):a.push(void 0)}return a},update:async(r,a)=>{let i=n.get(r);!i||n.set(r,{v:a,e:i.e+t*.75})},updateMany:async(...r)=>{for(let[a,i]of r){let u=n.get(a);!u||n.set(a,{v:i,e:u.e+t*.75})}},remove:async r=>{n.delete(r)},removeMany:async(...r)=>{if(r.length===0)return n.clear();for(let a of r)n.delete(a)},has:async r=>{let a=n.get(r);return a?a.e>Date.now()?!0:(n.delete(r),!1):!1},size:async()=>{let r=(a,i)=>i instanceof Map?{dataType:"Map",value:[...i]}:i;return JSON.stringify(n,r).length},keys:async()=>[...n.keys()],values:async()=>{let r=[];for(let a of n.values())r.push(a.v);return r},clear:g}};export{P as useCache};
var O=Object.create;var p=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames;var P=Object.getPrototypeOf,E=Object.prototype.hasOwnProperty;var N=(i,e)=>()=>(e||i((e={exports:{}}).exports,e),e.exports);var T=(i,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of K(e))!E.call(i,n)&&n!==t&&p(i,n,{get:()=>e[n],enumerable:!(r=R(e,n))||r.enumerable});return i};var G=(i,e,t)=>(t=i!=null?O(P(i)):{},T(e||!i||!i.__esModule?p(t,"default",{value:i,enumerable:!0}):t,i));var A=N((U,M)=>{var c=1e3,y=c*60,d=y*60,m=d*24,Z=m*7,D=m*365.25;M.exports=function(i,e){e=e||{};var t=typeof i;if(t==="string"&&i.length>0)return J(i);if(t==="number"&&isFinite(i))return e.long?I(i):$(i);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(i))};function J(i){if(i=String(i),!(i.length>100)){var e=/^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(i);if(!!e){var t=parseFloat(e[1]),r=(e[2]||"ms").toLowerCase();switch(r){case"years":case"year":case"yrs":case"yr":case"y":return t*D;case"weeks":case"week":case"w":return t*Z;case"days":case"day":case"d":return t*m;case"hours":case"hour":case"hrs":case"hr":case"h":return t*d;case"minutes":case"minute":case"mins":case"min":case"m":return t*y;case"seconds":case"second":case"secs":case"sec":case"s":return t*c;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return t;default:return}}}}function $(i){var e=Math.abs(i);return e>=m?Math.round(i/m)+"d":e>=d?Math.round(i/d)+"h":e>=y?Math.round(i/y)+"m":e>=c?Math.round(i/c)+"s":i+"ms"}function I(i){var e=Math.abs(i);return e>=m?l(i,e,m,"day"):e>=d?l(i,e,d,"hour"):e>=y?l(i,e,y,"minute"):e>=c?l(i,e,c,"second"):i+" ms"}function l(i,e,t,r){var n=e>=t*1.5;return Math.round(i/t)+" "+r+(n?"s":"")}});var x=11805916207174113e5,z=x*1024,f=[["B","Byte",1],["KB","Kilobyte",1e3],["MB","Megabyte",1e6],["GB","Gigabyte",1e9],["TB","Terabyte",1e12],["PB","Petabyte",1e15],["EB","Exabyte",1e18],["ZB","Zettabyte",1e21],["YB","Yottabyte",1e24],["KiB","Kibibyte",1024],["MiB","Mebibyte",1048576],["GiB","Gibibyte",1073741824],["TiB","Tebibyte",1099511627776],["PiB","Pebibyte",0x4000000000000],["EiB","Exbibyte",1152921504606847e3],["ZiB","Zebibyte",x],["YiB","Yobibyte",z]];function Y(i,e){let t=e?.long??!1,r=e?.format==="array",n=e?.prefix==="binary";if(typeof i=="number"){let o=i.toString(),a=o.length,u=(g,h)=>Number(Math.round(Number(g+"e"+h))+"e"+-h),s=(g,h,S)=>{let w=u(i/g,2),B=t?`${h}${w>1?"s":""}`:S;return r?[w,B]:`${w} ${B}`};return a>=4&&a<7?n?s(1024,"Kibibyte","KiB"):s(1e3,"Kilobyte","KB"):a>=7&&a<10?n?s(1048576,"Mebibyte","MiB"):s(1e6,"Megabyte","MB"):a>=10&&a<13?n?s(1073741824,"Gibibyte","GiB"):s(1e9,"Gigabyte","GB"):a>=13&&a<16?n?s(1099511627776,"Tebibyte","TiB"):s(1e12,"Terabyte","TB"):a>=16&&a<19?n?s(0x4000000000000,"Pebibyte","PiB"):s(1e15,"Petabyte","PB"):a>=19&&a<22?n?s(1152921504606847e3,"Exbibyte","EiB"):s(1e18,"Exabyte","EB"):a>=22&&a<25?n?s(x,"Zebibyte","ZiB"):s(1e21,"Zettabyte","ZB"):a>=25?n?s(z,"Yobibyte","YiB"):s(1e24,"Yottabyte","YB"):s(1,"Byte","B")}else{let o=i.split(" ");if(o.length!==2)return-1;o[1].endsWith("s")&&(o[1]=o[1].slice(0,-1));let a=f.length,u=0,s=-1;for(;u<a;)(o[1]===f[u][0]||o[1]===f[u][1])&&(s=parseInt(o[0])*f[u][2]),u++;return s}}var v=Y;var b=G(A(),1),k=class{autodelete;maximumAge;maximumAmount;maximumRecordSize;store;async rmOldest(e){let t=Date.now(),r;for(let n of this.store)n[1].e>t||(t=n[1].e,r=n[0]);if(r&&this.store.delete(r),e--,e>0)return await this.rmOldest(e)}async rmOveraged(){for(let e of this.store)e[1].e<=Date.now()&&this.store.delete(e[0])}constructor(e){this.autodelete=e?.autodelete??!1,this.maximumAge=typeof e?.maximumAge=="string"?(0,b.default)(e.maximumAge):e?.maximumAge?e.maximumAge*1e3:6e5,this.maximumAmount=e?.maximumAmount??1e4,this.maximumRecordSize=typeof e?.maximumRecordSize=="string"?v(e.maximumRecordSize):e?.maximumRecordSize?e.maximumRecordSize:v("10 KB"),this.store=new Map}async add(e,t,r){this.autodelete&&await this.rmOveraged(),this.store.size+1>this.maximumAmount&&await this.rmOldest(1);let n={v:t,e:Date.now()+(r?typeof r=="string"?(0,b.default)(r):r*1e3:this.maximumAge)};JSON.stringify({key:e,data:n}).length>this.maximumRecordSize||this.store.set(e,n)}async addMany(...e){this.autodelete&&await this.rmOveraged(),this.store.size+e.length>this.maximumAmount&&await this.rmOldest(this.store.size+e.length-this.maximumAmount);for(let[t,r,n]of e){let o={v:r,e:Date.now()+(n?typeof n=="string"?(0,b.default)(n):n:this.maximumAge)};JSON.stringify({key:t,data:o}).length>this.maximumRecordSize||this.store.set(t,o)}}async get(e){let t=this.store.get(e);if(!!t){if(t.e>Date.now())return t.v;this.store.delete(e)}}async*getMany(...e){for(let t of e){let r=this.store.get(t);r?r.e<=Date.now()?(this.store.delete(t),yield void 0):yield r.v:yield void 0}}async update(e,t){let r=this.store.get(e);if(!r)return;let n={v:t,e:r.e+this.maximumAge*.75};JSON.stringify({key:e,data:n}).length>this.maximumRecordSize||this.store.set(e,{v:t,e:r.e+this.maximumAge*.75})}async updateMany(...e){for(let[t,r]of e){let n=this.store.get(t);if(!n)continue;let o={v:r,e:n.e+this.maximumAge*.75};JSON.stringify({key:t,data:o}).length>this.maximumRecordSize||this.store.set(t,{v:r,e:n.e+this.maximumAge*.75})}}async remove(e){this.store.delete(e)}async removeMany(...e){if(e.length===0)this.store.clear();else for(let t of e)this.store.delete(t)}async has(e){let t=this.store.get(e);return t?t.e>Date.now()?!0:(this.store.delete(e),!1):!1}async size(){let e=(t,r)=>r instanceof Map?{dataType:"Map",value:[...r]}:r;return JSON.stringify(this.store,e).length}async keys(){return[...this.store.keys()]}async*values(){for(let e of this.store.values())yield e.v}async clear(){this.rmOveraged()}};export{k as Cache};
{
"name": "cachu",
"version": "6.0.6",
"description": "A simple key-value cache for Node.js.",
"version": "7.0.0",
"description": "👾 A simple key-value cache for Node.js.",
"author": "Samuel Kopp <opensource@azury.dev> (https://azury.dev)",

@@ -12,3 +12,3 @@ "main": "dist/index.js",

"format": "eslint src --fix",
"build": "empty dist && esbuild src/index.ts --bundle --minify --format=esm --legal-comments=none --outfile=dist/index.js",
"build": "empty dist types && tsc && esbuild src/index.ts --bundle --minify --format=esm --legal-comments=none --outfile=dist/index.js",
"test": "jest --config jest.config.json"

@@ -20,17 +20,15 @@ },

"homepage": "https://github.com/azurystudio/cachu#readme",
"dependencies": {
"@azury/bytes": "^2.0.0"
},
"devDependencies": {
"@azury/eslint-config": "^1.0.6",
"@azury/eslint-config": "^1.0.7",
"@azury/style": "^1.0.0",
"@changesets/changelog-github": "^0.4.6",
"@changesets/cli": "^2.24.3",
"@types/jest": "^28.1.7",
"@types/ms": "^0.7.31",
"@types/node": "^18.7.8",
"@unvented/empty": "^1.0.3",
"esbuild": "^0.15.5",
"eslint": "^8.22.0",
"jest": "^28.1.3",
"@types/node": "^18.11.7",
"@unvented/empty": "^1.1.0",
"esbuild": "^0.15.12",
"eslint": "^8.26.0",
"ms": "^2.1.3",
"ts-jest": "^28.0.8",
"typescript": "^4.7.4"
"typescript": "^4.8.4"
},

@@ -37,0 +35,0 @@ "engines": {

@@ -12,16 +12,14 @@ # cachu

> **Note** - Need an introduction to v6? [Here](https://gist.github.com/unvented/dab8d3e987cfdd79f68e715d29c1ee17) you go!
```js
import { useCache } from 'cachu'
import { Cache } from 'cachu'
const cache = useCache({
maxAge: '10m', // 10 minutes, can be specified as number (in seconds) or readable string
const cache = new Cache({
maximumAge: '10m', // 10 minutes, can be specified as number (in seconds) or readable string
...
})
// Add a new entry.
// Add a new record.
await cache.add('one', 'Hello World')
const entry = await cache.get('one') // 'Hello World'
const record = await cache.get('one') // 'Hello World'
```

@@ -33,5 +31,6 @@

* `maxAge` to set the **maximum age** for each record in the cache *(defaults to 600s)*
* `maxAmount` to set the **maximum size** for the cache *(defaults to 10000)*
* `autodelete` to **delete overaged entries** on adding/updating *(disabled by default)*
* `maximumAge` to set the **maximum age** for each record in the cache *(defaults to 600s)*
* `maximumAmount` to set the **maximum size** for the cache *(defaults to 10000)*
* `maximumRecordSize` to set the **maximum size** for each record *(defaults to 10 KB)*

@@ -52,2 +51,2 @@ * #### Features

* `values()`
* `clear()` *(delete all overaged entries manually)*
* `clear()` *(delete all overaged records manually)*

@@ -1,21 +0,29 @@

export type InitializeCache = (config?: {
maxAge?: number | string
maxAmount?: number
autodelete?: boolean
}) => {
add: (key: any, value: any, maxAge?: number | string) => Promise<void>
addMany: (...entries: ([any, any, number | string] | [any, any])[]) => Promise<void>
get: (key: any) => Promise<any>
getMany: (...keys: any[]) => Promise<any[]>
update: (key: any, value: any) => Promise<void>
updateMany: (...entries: [any, any][]) => Promise<void>
remove: (key: any) => Promise<void>
removeMany: (...keys: any[]) => Promise<void>
has: (key: any) => Promise<boolean>
size: () => Promise<number>
keys: () => Promise<any[]>
values: () => Promise<any[]>
clear: () => Promise<void>
import type { Unit } from '@azury/bytes/types/unit';
export declare class Cache {
private autodelete;
private maximumAge;
private maximumAmount;
private maximumRecordSize;
private store;
private rmOldest;
private rmOveraged;
constructor(options?: {
autodelete?: boolean;
maximumAge?: number | string;
maximumAmount?: number;
maximumRecordSize?: number | Unit;
});
add(key: unknown, value: unknown, maxAge?: number | string): Promise<void>;
addMany(...records: ([unknown, unknown] | [unknown, unknown, number | string])[]): Promise<void>;
get(key: unknown): Promise<any>;
getMany(...keys: unknown[]): AsyncIterable<any | undefined>;
update(key: unknown, value: unknown): Promise<void>;
updateMany(...records: [unknown, unknown][]): Promise<void>;
remove(key: unknown): Promise<void>;
removeMany(...keys: unknown[]): Promise<void>;
has(key: unknown): Promise<boolean>;
size(): Promise<number>;
keys(): Promise<any[]>;
values(): AsyncIterable<any>;
clear(): Promise<void>;
}
export const useCache: InitializeCache

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc