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

cachu

Package Overview
Dependencies
Maintainers
1
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 5.0.0-canary.4 to 5.0.0

types/caches/MemoryCache/types.d.ts

2

dist/index.js

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

var z=Object.create;var p=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var O=Object.getPrototypeOf,E=Object.prototype.hasOwnProperty;var G=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports),U=(n,e)=>{for(var t in e)p(n,t,{get:e[t],enumerable:!0})},v=(n,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let d of R(e))!E.call(n,d)&&d!==t&&p(n,d,{get:()=>e[d],enumerable:!(i=C(e,d))||i.enumerable});return n};var L=(n,e,t)=>(t=n!=null?z(O(n)):{},v(e||!n||!n.__esModule?p(t,"default",{value:n,enumerable:!0}):t,n)),B=n=>v(p({},"__esModule",{value:!0}),n);var D=G((de,b)=>{var f=1e3,y=f*60,m=y*60,u=m*24,F=u*7,H=u*365.25;b.exports=function(n,e){e=e||{};var t=typeof n;if(t==="string"&&n.length>0)return I(n);if(t==="number"&&isFinite(n))return e.long?V(n):K(n);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(n))};function I(n){if(n=String(n),!(n.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(n);if(!!e){var t=parseFloat(e[1]),i=(e[2]||"ms").toLowerCase();switch(i){case"years":case"year":case"yrs":case"yr":case"y":return t*H;case"weeks":case"week":case"w":return t*F;case"days":case"day":case"d":return t*u;case"hours":case"hour":case"hrs":case"hr":case"h":return t*m;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*f;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return t;default:return}}}}function K(n){var e=Math.abs(n);return e>=u?Math.round(n/u)+"d":e>=m?Math.round(n/m)+"h":e>=y?Math.round(n/y)+"m":e>=f?Math.round(n/f)+"s":n+"ms"}function V(n){var e=Math.abs(n);return e>=u?w(n,e,u,"day"):e>=m?w(n,e,m,"hour"):e>=y?w(n,e,y,"minute"):e>=f?w(n,e,f,"second"):n+" ms"}function w(n,e,t,i){var d=e>=t*1.5;return Math.round(n/t)+" "+i+(d?"s":"")}});var J={};U(J,{MemoryCache:()=>S});module.exports=B(J);var M=L(D()),_=(n={})=>{let e=new Map,t={},i,d=typeof n.maxAge=="number"?n.maxAge*1e3:typeof n.maxAge=="string"?(0,M.default)(n.maxAge):6e5,l=n.maxAmount??1e4,h=async()=>{e.forEach(async(a,r)=>{Date.now()-a.age>d&&e.delete(r)})},A=async()=>{let a=0,r=0;e.forEach(async(s,o)=>{s.age>a&&(a=s.age,r=o)}),e.delete(r)};return{set:async(a,r,s)=>{if(t.set&&await t.set(a,r,s),e.has(a))return;d!==1/0&&await h(),e.size>l&&await A();let o={value:r,age:Date.now(),...typeof s=="string"?{maxAge:(0,M.default)(s)}:typeof s=="number"&&{maxAge:s*1e3}};return e.set(a,o),i={key:a,...o},{key:a,...o}},setMany:async a=>{t.setMany&&await t.setMany(a),d!==1/0&&await h();let r=[];for(let s of a){let o=s[0],c=s[1],g=s[2];if(e.has(o)){r.push(void 0);continue}let x={value:c,age:Date.now(),...typeof g=="string"?{maxAge:(0,M.default)(g)}:typeof g=="number"&&{maxAge:g*1e3}};e.size>l&&await A(),e.set(o,x),i={key:o,...x},r.push({key:o,...x})}return r},get:async(a,r={})=>{t.get&&await t.get(a,r);let s=e.get(a);if(!!s){if(r.validate!==!1&&(s.maxAge?Date.now()-s.age>s.maxAge:Date.now()-s.age>d)){e.delete(a);return}return r.delete&&e.delete(a),{key:a,...s}}},getMany:async(a,r={})=>{t.getMany&&await t.getMany(a,r);let s=[];for(let o of a){let c=e.get(o);if(!c){s.push(void 0);continue}if(r.validate!==!1&&(c.maxAge?Date.now()-c.age>c.maxAge:Date.now()-c.age>d)){e.delete(o),s.push(void 0);continue}r.delete&&e.delete(o),s.push({key:o,...c})}return r.reverse?s.reverse():s},update:async(a,r,s)=>{t.update&&await t.update(a,r,s??{});let o=e.get(a);if(!o)return;let c={value:r,age:s&&s.updateAge?Date.now():o.age,...o.maxAge&&{maxAge:o.maxAge}};e.set(a,c),i={key:a,...c}},updateMany:async(a,r)=>{t.updateMany&&await t.updateMany(a,r??{});for(let s of a){let o=e.get(s[0]);if(!o)return;let c={value:s[1],age:r&&r.updateAge?Date.now():o.age,...o.maxAge&&{maxAge:o.maxAge}};e.set(s[0],c),i={key:s[0],...c}}},delete:async a=>{t.delete&&await t.delete(a),e.delete(a)},deleteMany:async a=>{t.deleteMany&&await t.deleteMany(a),a.length===0&&e.clear();for(let r of a)e.delete(r)},has:async a=>(t.has&&await t.has(a),e.has(a)),size:async()=>(t.size&&await t.size(),e.size),keys:async()=>(t.keys&&await t.keys(),[...e.keys()]),values:async()=>{t.values&&await t.values();let a=[];for(let r of[...e.values()])a.push(r.value);return a},clear:async()=>(t.clear&&await t.clear(),await h()),memory:async()=>{t.memory&&await t.memory();let a=[...e.keys()].toString()+[...e.values()].toString(),r=Buffer.from(a);return Buffer.byteLength(r)},recent:async()=>(t.recent&&await t.recent(),i),maxAge:async a=>(t.maxAge&&await t.maxAge(a),a&&(d=typeof a=="number"?a*1e3:typeof a=="string"?(0,M.default)(a):6e5,await h()),d),maxAmount:async a=>(t.maxAmount&&await t.maxAmount(a),a&&(l=a??1e4,await h()),l),on:async(a,r)=>{t[a]=r}}},S=_;
var z=Object.create;var p=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var O=Object.getPrototypeOf,E=Object.prototype.hasOwnProperty;var G=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports),U=(n,e)=>{for(var t in e)p(n,t,{get:e[t],enumerable:!0})},v=(n,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let d of C(e))!E.call(n,d)&&d!==t&&p(n,d,{get:()=>e[d],enumerable:!(i=R(e,d))||i.enumerable});return n};var L=(n,e,t)=>(t=n!=null?z(O(n)):{},v(e||!n||!n.__esModule?p(t,"default",{value:n,enumerable:!0}):t,n)),B=n=>v(p({},"__esModule",{value:!0}),n);var D=G((de,b)=>{var f=1e3,y=f*60,m=y*60,u=m*24,F=u*7,H=u*365.25;b.exports=function(n,e){e=e||{};var t=typeof n;if(t==="string"&&n.length>0)return I(n);if(t==="number"&&isFinite(n))return e.long?V(n):K(n);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(n))};function I(n){if(n=String(n),!(n.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(n);if(!!e){var t=parseFloat(e[1]),i=(e[2]||"ms").toLowerCase();switch(i){case"years":case"year":case"yrs":case"yr":case"y":return t*H;case"weeks":case"week":case"w":return t*F;case"days":case"day":case"d":return t*u;case"hours":case"hour":case"hrs":case"hr":case"h":return t*m;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*f;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return t;default:return}}}}function K(n){var e=Math.abs(n);return e>=u?Math.round(n/u)+"d":e>=m?Math.round(n/m)+"h":e>=y?Math.round(n/y)+"m":e>=f?Math.round(n/f)+"s":n+"ms"}function V(n){var e=Math.abs(n);return e>=u?w(n,e,u,"day"):e>=m?w(n,e,m,"hour"):e>=y?w(n,e,y,"minute"):e>=f?w(n,e,f,"second"):n+" ms"}function w(n,e,t,i){var d=e>=t*1.5;return Math.round(n/t)+" "+i+(d?"s":"")}});var J={};U(J,{MemoryCache:()=>S});module.exports=B(J);var M=L(D()),_=(n={})=>{let e=new Map,t={},i,d=typeof n.maxAge=="number"?n.maxAge*1e3:typeof n.maxAge=="string"?(0,M.default)(n.maxAge):6e5,l=n.maxAmount??1e4,h=async()=>{e.forEach(async(a,s)=>{Date.now()-a.age>d&&e.delete(s)})},A=async()=>{let a=0,s=0;e.forEach(async(r,o)=>{r.age>a&&(a=r.age,s=o)}),e.delete(s)};return{set:async(a,s,r)=>{if(t.set&&await t.set(a,s,r),e.has(a))return;d!==1/0&&await h(),e.size>l&&await A();let o={value:s,age:Date.now(),...typeof r=="string"?{maxAge:(0,M.default)(r)}:typeof r=="number"&&{maxAge:r*1e3}};return e.set(a,o),i={key:a,...o},{key:a,...o}},setMany:async a=>{t.setMany&&await t.setMany(a),d!==1/0&&await h();let s=[];for(let r of a){let o=r[0],c=r[1],g=r[2];if(e.has(o)){s.push(void 0);continue}let x={value:c,age:Date.now(),...typeof g=="string"?{maxAge:(0,M.default)(g)}:typeof g=="number"&&{maxAge:g*1e3}};e.size>l&&await A(),e.set(o,x),i={key:o,...x},s.push({key:o,...x})}return s},get:async(a,s={})=>{t.get&&await t.get(a,s);let r=e.get(a);if(!!r){if(s.validate!==!1&&(r.maxAge?Date.now()-r.age>r.maxAge:Date.now()-r.age>d)){e.delete(a);return}return s.delete&&e.delete(a),{key:a,...r}}},getMany:async(a,s={})=>{t.getMany&&await t.getMany(a,s);let r=[];for(let o of a){let c=e.get(o);if(!c){r.push(void 0);continue}if(s.validate!==!1&&(c.maxAge?Date.now()-c.age>c.maxAge:Date.now()-c.age>d)){e.delete(o),r.push(void 0);continue}s.delete&&e.delete(o),r.push({key:o,...c})}return s.reverse?r.reverse():r},update:async(a,s,r)=>{t.update&&await t.update(a,s,r??{});let o=e.get(a);if(!o)return;let c={value:s,age:r&&r.updateAge?Date.now():o.age,...o.maxAge&&{maxAge:o.maxAge}};e.set(a,c),i={key:a,...c}},updateMany:async(a,s)=>{t.updateMany&&await t.updateMany(a,s??{});for(let r of a){let o=e.get(r[0]);if(!o)return;let c={value:r[1],age:s&&s.updateAge?Date.now():o.age,...o.maxAge&&{maxAge:o.maxAge}};e.set(r[0],c),i={key:r[0],...c}}},delete:async a=>{t.delete&&await t.delete(a),e.delete(a)},deleteMany:async a=>{t.deleteMany&&await t.deleteMany(a),a.length===0&&e.clear();for(let s of a)e.delete(s)},has:async a=>(t.has&&await t.has(a),e.has(a)),size:async()=>(t.size&&await t.size(),e.size),keys:async()=>(t.keys&&await t.keys(),[...e.keys()]),values:async()=>{t.values&&await t.values();let a=[];for(let s of[...e.values()])a.push(s.value);return a},clear:async()=>(t.clear&&await t.clear(),await h()),memory:async()=>{t.memory&&await t.memory();let a=[...e.keys()].toString()+[...e.values()].toString(),s=Buffer.from(a);return Buffer.byteLength(s)},recent:async()=>(t.recent&&await t.recent(),i),maxAge:async a=>(t.maxAge&&await t.maxAge(a),a&&(d=typeof a=="number"?a*1e3:typeof a=="string"?(0,M.default)(a):6e5,await h()),d),maxAmount:async a=>(t.maxAmount&&await t.maxAmount(a),a&&(l=a??1e4,await h()),l),on:async(a,s)=>{t[a]=s}}},S=_;
{
"name": "cachu",
"version": "5.0.0-canary.4",
"version": "5.0.0",
"description": "🦝 Simple, Minimalistic KV Cache",

@@ -39,3 +39,2 @@ "types": "./types/index.d.ts",

"ms": "^2.1.3",
"redis": "^4.0.6",
"ts-jest": "^27.1.4",

@@ -42,0 +41,0 @@ "typescript": "^4.6.3"

@@ -28,9 +28,9 @@ # cachu

(async () => {
const cache = await MemoryCache({
maxAmount: 10000, // cache can contain up to 10000 records
maxAge: 600 // keep records for up to 10 minutes
})
const cache = MemoryCache()
await cache.set(69, 'Hello World')
console.log((await cache.get(69)).value) // gives out 'Hello World'
// add a new record
await cache.set('one', 'Hello Github')
// retrieve the value of a record
const { value } = await cache.get('one')
})()

@@ -42,4 +42,2 @@ ```

- [`MemoryCache`](/guide/caches/MemoryCache.md)
- [`BrowserCache`](/guide/caches/BrowserCache.md)
- [`DiskCache`](/guide/caches/DiskCache.md)

@@ -50,28 +48,23 @@ ## API

- [`maxAge`](/guide/configuration/maxAge.md) to set the **maximum age** for each record in the cache
- [`maxAmount`](/guide/configuration/maxAmount.md) to set the **maximum size** for the cache
- [`maxAge`](/guide/config/maxAge.md) to set the **maximum age** for each record in the cache
- [`maxAmount`](/guide/config/maxAmount.md) to set the **maximum size** for the cache
- ### Features
- [`set(key, value, age)`](/guide/features/set.md)
- [`setMany([[key, value, age]])`](/guide/features/setMany.md)
- [`get(key, options)`](/guide/features/get.md)
- [`getMany([key], options)`](/guide/features/getMany.md)
- [`update(key, value, age)`](/guide/features/update.md)
- [`updateMany([[key, value, age]])`](/guide/features/updateMany.md)
- [`set(key, value, maxAge)`](/guide/features/set.md)
- [`setMany([[key, value, maxAge]])`](/guide/features/setMany.md)
- [`get(key, config)`](/guide/features/get.md)
- [`getMany([keys], config)`](/guide/features/getMany.md)
- [`update(key, value, config)`](/guide/features/update.md)
- [`updateMany([[key, value]], config)`](/guide/features/updateMany.md)
- [`delete(key)`](/guide/features/delete.md)
- [`deleteMany([key])`](/guide/features/deleteMany.md)
- [`deleteMany([keys])`](/guide/features/deleteMany.md)
- [`has(key)`](/guide/features/has.md)
- [`clear()`](/guide/features/clear.md)
- [`size()`](/guide/features/size.md)
- [`keys()`](/guide/features/keys.md)
- [`values()`](/guide/features/values.md)
- [`keys()`](/guide/features/keys.md)
- ### Hooks
- [`preWriting`](/guide/hooks/preWriting.md)
- [`preReading`](/guide/hooks/preReading.md)
- [`preUpdating`](/guide/hooks/preUpdating.md)
- [`preDeleting`](/guide/hooks/preDeleting.md)
- [`preClearing`](/guide/hooks/preClearing.md)
- [`prePruning`](/guide/hooks/prePruning.md)
- [`clear()`](/guide/features/clear.md)
- [`recent()`](/guide/features/recent.md)
- [`maxAge(age)`](/guide/features/maxAge.md)
- [`maxAmount(amount)`](/guide/features/maxAmount.md)
- [`on(event, action)`](/guide/features/on.md)

@@ -1,120 +0,3 @@

/**
* Create a new `MemoryCache`
*/
declare type MemoryCache = (config: {
maxAge?: string | number;
maxAmount?: number;
}) => {
set: SetMethod;
setMany: SetManyMethod;
get: GetMethod;
getMany: GetManyMethod;
update: UpdateMethod;
updateMany: UpdateManyMethod;
delete: DeleteMethod;
deleteMany: DeleteManyMethod;
has: HasMethod;
size: SizeMethod;
keys: KeysMethod;
values: ValuesMethod;
clear: ClearMethod;
memory: MemoryMethod;
recent: RecentMethod;
maxAge: MaxAgeMethod;
maxAmount: MaxAmountMethod;
on: OnMethod;
};
declare type Key = {};
declare type Value = {};
declare type Record = {
key: Key;
value: Value;
age: number;
maxAge?: number;
};
declare type Event = 'set' | 'setMany' | 'get' | 'getMany' | 'update' | 'updateMany' | 'delete' | 'deleteMany' | 'has' | 'size' | 'keys' | 'values' | 'clear' | 'memory' | 'recent' | 'maxAge' | 'maxAmount' | 'on';
/**
* Add a event handler.
*/
declare type OnMethod = (event: Event, action: Function) => Promise<void>;
/**
* Change or get the maximum age of records.
*/
declare type MaxAgeMethod = (newAge?: string | number) => Promise<number>;
/**
* Change or get the limit of records.
*/
declare type MaxAmountMethod = (newAmount?: number) => Promise<number>;
/**
* Add a new record to the cache.
*/
declare type SetMethod = (key: Key, value: Value, maxAge?: number | string) => Promise<Record | undefined>;
/**
* Add many new records to the cache.
*/
declare type SetManyMethod = (records: [Key, Value, number | string | undefined][]) => Promise<(Record | undefined)[]>;
/**
* Read a record from the cache.
*/
declare type GetMethod = (key: Key, config?: {
validate?: boolean;
delete?: boolean;
}) => Promise<Record | undefined>;
/**
* Read many records from the cache.
*/
declare type GetManyMethod = (keys: Key[], config?: {
reverse?: boolean;
validate?: boolean;
delete?: boolean;
}) => Promise<(Record | undefined)[]>;
/**
* Delete all stale records manually.
*/
declare type ClearMethod = () => Promise<void>;
/**
* Get an array of all record keys.
*/
declare type KeysMethod = () => Promise<any[]>;
/**
* Get an array of all record values.
*/
declare type ValuesMethod = () => Promise<any[]>;
/**
* Get the amount of memory in bytes consumed by the cache.
*/
declare type MemoryMethod = () => Promise<number>;
/**
* Get the most recent updated or added record.
*/
declare type RecentMethod = () => Promise<Record | undefined>;
/**
* Get the amount of records in the cache.
*/
declare type SizeMethod = () => Promise<number>;
/**
* Check whether the cache has a specific record.
*/
declare type HasMethod = (key: Key) => Promise<boolean>;
/**
* Delete a record from the cache.
*/
declare type DeleteMethod = (key: Key) => Promise<void>;
/**
* Delete many records from the cache.
*/
declare type DeleteManyMethod = (keys: Key[]) => Promise<void>;
/**
* Update the value of a record.
*/
declare type UpdateMethod = (key: Key, value: Value, config?: {
updateAge: boolean;
}) => Promise<void>;
/**
* Update the values of many records.
*/
declare type UpdateManyMethod = (records: [Key, Value][], config: {
updateAge: boolean;
}) => Promise<void>;
import { MemoryCache } from './types';
declare const memoryCache: MemoryCache;
export default memoryCache;
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