🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

cache-ts

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cache-ts

Simple and Efficient Cache Implementation for TypeScript

1.0.0
latest
Source
npm
Version published
Weekly downloads
2.2K
65.45%
Maintainers
1
Weekly downloads
 
Created
Source

cache-ts

Simple and Efficient Cache Implementation for TypeScript

npm version License

cache-ts is a straightforward cache implementation for TypeScript, extending the native Map object. It provides a simple and efficient way to manage key-value pairs with a specified capacity, automatically handling entries based on their usage.

Features

  • Capacity Control: Define the maximum number of entries the cache can hold.
  • Automatic Eviction: Older entries are automatically removed when the cache reaches its capacity.
  • Efficient Usage: Promotes recently used entries to enhance retrieval speed.

Installation

npm install cache-ts
# or
yarn add cache-ts

Usage

import { Cache } from 'cache-ts';

// Create a cache with a maximum capacity of 100 entries
const myCache = new Cache<string, number>(100);

// Set a key-value pair in the cache
myCache.set('key1', 42);

// Retrieve the value associated with a key
const value = myCache.get('key1');

API

constructor(capacity: number)

Creates a new Cache instance with a specified capacity.

const myCache = new Cache<string, number>(100);

set(key: TKey, value: TValue): Cache<TKey, TValue>

Adds or updates a key-value pair in the cache. Automatically handles eviction if the cache reaches its capacity.

myCache.set('key1', 42);

get(key: TKey): TValue | undefined

Retrieves the value associated with a key from the cache. If the key exists, it is promoted to the most recently used entry.

const value = myCache.get('key1');

License

cache-ts is licensed under the MIT License. Feel free to use and contribute!

If you encounter any issues or have suggestions, please open an issue.

Happy caching! 🚀

Authors

Keywords

cache

FAQs

Package last updated on 12 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts