3.1.0 • Published 2 years ago

runtime-memcache v3.1.0

Weekly downloads
120
License
MIT
Repository
github
Last release
2 years ago

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/runtime-memcache@3.0.0/dist/umd/index.js"></script>
<!-- OR JUST -->
<script src="https://unpkg.com/runtime-memcache@3.0.0"></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)

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

3.1.0

2 years ago

3.0.0

2 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0-beta.6

5 years ago

2.0.0-beta.5

5 years ago

2.0.0

5 years ago

2.0.0-beta.4

5 years ago

2.0.0-beta.3

5 years ago

2.0.0-beta.2

5 years ago

2.0.0-beta.1

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.2.3

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.2.2

5 years ago

0.1.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago