npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

runtime-memcache

v3.1.0

Published

A no dependency javascript runtime key-value cache store for small chunks of arbitrary data (strings, objects, numbers)

Downloads

1,598

Readme

runtime-memcache is a caching library to store key-value cache store for small chunks of arbitrary data (strings, objects, numbers) from results of database calls, API calls, or etc. It is entirely written using Typescript and supports many commonly used caching policies.

When creating a new cache store, you can specify the policy to evict items from the store. The default policy is lru (Least Recently Used)

runtime-memcache provides flexible construction to create a cache with a combination of the following features:

  • size-based eviction when a maximum is exceeded based on frequency and recency
  • time-based expiration of entries, measured since last access or last write

Installation

npm install --save runtime-memcache
# or using yarn
yarn add runtime-memcache

Node Environment (ES6+ import/export)

import createStore from 'runtime-memcache';

Node Environment (CJS)

const createStore = require('runtime-memcache');

Browser (use as a script tag)

<script src="https://unpkg.com/[email protected]/dist/umd/index.js"></script>
<!-- OR JUST -->
<script src="https://unpkg.com/[email protected]"></script>
<script>
  // RMStore is globaly set
  const store = new RMStore();
</script>

API

Calling the createStore function returns an object with the following properties.

| Property | Description | | --------------- | ------------------------------------------------ | | get(id) | Retrieves an item from the store | | has(id) | Check if an item exists in the store | | set(id, data) | Sets an item in the store | | remove(id) | Removes an item from the store | | size() | Returns the size of the item cache store | | keys() | Returns all the keys of the cache store as array |

Config

createStore takes an optional config object as an argument with the following properties.

| Property | Description | Type | Default | | ------------- | ----------------------------------------------------------------------------------------------------- | ------------------------------- | ------- | | timeToClear | Time in milliseconds for which the store will keep an item when the policy is timeout or tlru | Number | 7200000 | | policy | A Policy to evict items from the store | timeout, lru, mru, tlru | lru | | lruSize | Size of the cache store when the policy is lru or tlru | Number | 500 | | mruSize | Size of the cache store when the policy is mru | Number | 500 |

Caching Policies

Following caching policies are supported.

| Policy | Name | Description | | --------- | ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | timeout | Timeout | The items in the cache store will be automatically evicted after a fixed amount of time has elapsed since that item was set | | lru | Least Recently Used | This policy evicts the least recently used items when the store size is full | | tlru | Time Aware Least Recently Used | This policy evicts the least recently used items when the store size is full and also evict untouched items after a fixed amount of time has elapsed since that item was set | | mru | Most Recently Used | This policy evicts the most recently used items when the store size is full |

Time Complexity

| Policy | Method | Complexity | | ------- | ---------------------- | ---------------- | | timeout | set, get, remove | O(1), O(1), O(1) | | lru | set, get, remove | O(1), O(1), O(1) | | tlru | set, get, remove | O(1), O(1), O(1) | | mru | set, get, remove | O(1), O(1), O(1) |

Usage

Typescript

import createStore, { Config } from 'runtime-memcache';

const config: Config = {
  policy: 'lru',
  lruSize: 300, // cache a maximum of 300 users at a given time
};

interface User {
  name: string;
}

const userCache = createStore<User>(config);

async function loginUser(userId: string) {
  if (userCache.has(userId)) {
    return userCache.get(userId);
  }

  const user: User = await UserService.getUser(userId);

  userCache.set(userId, user);

  return user;
}

Javascript

import createStore from 'runtime-memcache';

const config = {
  policy: 'timeout',
  timeToClear: 7200000, // 2 hours
};

type Keys = 'key1' | 'key2';

const store = createStore(config);

store.set('key1', { name: 'name' }); // store the object and associate it with the provided key

store.get('key1'); // retrieves the object associated with this key

store.has('key1'); // returns true

store.size(); // returns 1

store.keys(); // returns ['key1']

store.remove('key1'); // deletes the object associated with this key

NPM Script Commands

  • npm run test -- Runs tests, lint and build.
  • npm run lint -- Runs ESLint.
  • npm run format -- Reformats all of the .ts and .tsx files with Prettier.
  • npm run build -- Regenerates dist folder that gets included into NPM module.

Under The Hood

runtime-memcache uses a combination of modified doubly-linked lists and hashmap data structures to achieve O(1) search-time complexity for all the methods.

Todos

  • Timeout Policy (TR)
  • Least Recently Used Policy (LRU)
  • Most Recently Used Policy (MRU)
  • Least Frequently Used Policy (LFU)
  • Time Aware Least Recently Used Policy (TLRU)
  • Random Eviction Policy (RR)
  • Add a warmup period for new items

For more information on caching policies read this