Exciting news!Announcing our $4.6M Series Seed. Learn more
Socket
LoveBlogFAQ
Install
Log in

runtime-memcache

Package Overview
Dependencies
0
Maintainers
1
Versions
23
Issues
File Explorer

Advanced tools

runtime-memcache

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

    2.1.1latest

Version published
Maintainers
1
Yearly downloads
7,783
increased by112.71%

Weekly downloads

Changelog

Source

v2.1.1

  • Remove debug statements

Readme

Source

javascript cache

runtime-memcache

A no dependency, high performance, near optimal javascript caching library



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

Usage

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.

PropertyDescription
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.

PropertyDescriptionTypeDefault
timeToClearTime in milliseconds for which the store will keep an item when the policy is timeout or tlruNumber7200000
policyA Policy to evict items from the storetimeout, lru, mru, tlrulru
lruSizeSize of the cache store when the policy is lru or tlruNumber500
mruSizeSize of the cache store when the policy is mruNumber500

Caching Policies

Following caching policies are supported.

PolicyNameDescription
timeoutTimeoutThe items in the cache store will be automatically evicted after a fixed amount of time has elapsed since that item was set
lruLeast Recently UsedThis policy evicts the least recently used items when the store size is full
tlruTime Aware Least Recently UsedThis 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
mruMost Recently UsedThis policy evicts the most recently used items when the store size is full

Time Complexity

PolicyMethodComplexity
timeoutset, get, removeO(1), O(1), O(1)
lruset, get, removeO(1), O(1), O(1)
tlruset, get, removeO(1), O(1), O(1)
mruset, get, removeO(1), O(1), O(1)

Example

import createStore from 'runtime-memcache'; const config = { policy: 'timeout', timeToClear: 7200000, // 2 hours }; interface Response { name: string; } type Keys = 'key1' | 'key2'; const store = createStore<Keys, Response>(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
import createStore from 'runtime-memcache'; const config = { policy: 'lru', lruSize: 300, // cache a maximum of 300 users at a given time }; const userCache = createStore(config); async function loginUser(userId: string) { if (userCache.has(id)) { return userCache.get(id); } const user = await UserService.getUser(id); userCache.set(id, user); return user; }

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)

For more information on caching policies read this

Keywords

FAQs

What is runtime-memcache?

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

Is runtime-memcache popular?

The npm package runtime-memcache receives a total of 186 weekly downloads. As such, runtime-memcache popularity was classified as not popular.

Is runtime-memcache well maintained?

We found that runtime-memcache demonstrated a not healthy version release cadence and project activity. It has 1 open source maintainer collaborating on the project.

Last updated on 21 Nov 2020
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc