Socket
Socket
Sign inDemoInstall

a-simple-cache

Package Overview
Dependencies
0
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    a-simple-cache

Simple in-memory cache with additional utilities


Version published
Weekly downloads
2
Maintainers
1
Created
Weekly downloads
 

Readme

Source

a-simple-cache

Build Status codecov


Simple in-memory cache with additional utilities.



Cache

Cache is a low-level interface for storing values to and retrieving values from a global object.

Importing

import { Cache } from 'a-simple-cache';

Caching a value

// for one minute
Cache.set('key', 'value', 60000);

// for three minutes
Cache.set('my:id', { 
    'number': 42,
    'string': 'Hello!',
}, 180000);

Retrieving a value

Cache.get('key');
'value'

Cache.get('my:id');
{ 'number': 42, 'string': 'Hello!' }

Checking if an entry is valid

Cache.isValid('key');
true

// one minute passes

Cache.isValid('key');
false

Cache.isValid('my:id');
true

// another two minutes pass

Cache.isValid('my:id');
false

Listing keys of entries

Cache.keys();
[ 'key', 'my:id' ]

// or use a filter

Cache.keys(k => k.startsWith('my:'));
[ 'my:id' ]

Deleting entries

Cache.clear();

// or 

for (const key of Cache.keys()) {
    if (!Cache.isValid(key)) {
        Cache.delete(key);
    }
}

Cache.keys();
[ ]

Utilities

time

Caching time is expressed as a number of milliseconds, but the library provides a few convenience constants.

import { time } from 'a-simple-cache';

time.second, time.minute, time.hour
1000, 60000, 3600000

time.day, time.week, time.month
86400000, 604800000, 2592000000

Memoize

Memoize is a function wrapper that automatically caches values returned by the function for any given argument combination.

Memoizing a function with it
import { Memoize } from 'a-simple-cache';

function expensive(arg) { ... }

const memoizedExpensive = Memoize.it(expensive, time.hour);

// runs function
memoizedExpensive(0);

// gets value from cache
memoizedExpensive(0);

// one hour passes
// runs function again
memoizedExpensive(0); 
Invalidating a function's cache with invalidate

Sometimes you need to invalidate the cache prematurely so that the function will start to run again for all argument combinations.

// runs function
memoizedExpensive(1);

// gets value from cache
memoizedExpensive(1);

Memoize.invalidate(expensive);
// or
Memoize.invalidate(memoizedExpensive);

// one hour has not passed
// runs function again
memoizedExpensive(1);

Tests

npm test
npm run coverage

Keywords

FAQs

Last updated on 08 Feb 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc