Skip to content

ehmpathy/simple-in-memory-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-in-memory-cache

test publish

A simple, typed, in-memory cache for nodejs and the browser with time-based expiration policies.

install

npm install --save simple-in-memory-cache

usage

set and get

import { createCache } from 'simple-in-memory-cache';

const { set, get } = createCache();
set('purpose of life', 42);
const purpose = get('purpose of life'); // returns 42

expiration

items in the cache expire after 5 minutes by default.

change the default expiration on cache creation:

const { set, get } = createCache({ expiration: { minutes: 10 } });

override expiration per item:

set('ice cream state', 'solid', { expiration: { seconds: 30 } });

set an item to never expire:

set('speed of light', 299792458, { expiration: null });

invalidation

invalidate a cached item with set(key, undefined):

set('purpose of life', 42);
get('purpose of life'); // returns 42

set('purpose of life', undefined);
get('purpose of life'); // returns undefined

keys

list all non-expired keys in the cache:

const { set, keys } = createCache();
set('a', 1);
set('b', 2);
keys(); // returns ['a', 'b']

types

the cache is fully typed:

import { createCache, SimpleInMemoryCache } from 'simple-in-memory-cache';

const cache: SimpleInMemoryCache<number> = createCache<number>();
cache.set('answer', 42);
const answer: number | undefined = cache.get('answer');

api

createCache<T>(options?)

creates a new cache instance.

options:

  • expiration?: IsoDuration | null — default expiration for items (default: { minutes: 5 })

returns: SimpleInMemoryCache<T>

SimpleInMemoryCache<T>

  • get(key: string): T | undefined — retrieve an item (returns undefined if absent or expired)
  • set(key: string, value: T, options?): void — store an item (or invalidate with undefined)
  • keys(): string[] — list all non-expired keys

About

A simple in-memory cache, for nodejs and the browser, with time based expiration policies.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors