Skip to content

loom4k/kv

Repository files navigation

KV Store (Key-Value Store)

An efficient in-memory Least Recently Used (LRU) cache implementation in TypeScript. This library helps manage memory by keeping the most frequently accessed items and evicting the least recently used ones when the cache reaches its capacity.

Features

  • Efficient: O(1) operations for insertion, deletion, and access.
  • Configurable: Set maximum cache size to control memory usage.
  • TypeScript Support: Written in TypeScript for strong typing and editor support.

Installation

To install the pacakge, use npm:

npm install @loom4k/kv

Usage

Importing the library

import CacheManager, { Cache } from "@loom4k/kv";

Creating and registering a new cache

const manager = new CacheManager();
// Create a cache of numbers accessed through a string
const cache = new Cache<string, number>(100);
manager.add("my-cache", cache);

Adding items to the cache

const cache = manager.get<string, number>("my-cache");
cache.set('key1', 'value1');
cache.set('key2', 'value2');

Retrieving values from the cache

const value = cache.get('key1'); // Returns 'value1'
const missingValue = cache.get('key3'); // Returns undefined

Removing items from the cache

cache.delete('key1');

Clearing the cache

cache.clear();

Example

Here's a complete example demonstrating the use of the LRU cache:

import CacheManager, { Cache } from "@loom4k/kv";

const manager = new CacheManager();
const cache = new Cache<string, string>(3);

cache.set('a', '1');
cache.set('b', '2');
cache.set('c', '3');

console.log(cache.get('a')); // Outputs: '1'

cache.set('d', '4'); // Evicts 'b' since the cache size is 3

console.log(cache.get('b')); // Outputs: undefined
console.log(cache.get('c')); // Outputs: '3'
console.log(cache.get('d')); // Outputs: '4'