Socket
Socket
Sign inDemoInstall

@zero65tech/cache

Package Overview
Dependencies
1
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @zero65tech/cache

Simple In-memory Cache


Version published
Weekly downloads
18
decreased by-10%
Maintainers
2
Created
Weekly downloads
 

Readme

Source

Cache Utils

This repository provides a set of utility functions for implementing caching mechanisms, including Time-To-Live (TTL) and Least Recently Used (LRU) caches. These utilities can be useful for optimizing the performance of functions by caching their results and minimizing redundant computations.

Installation

npm install @zero65tech/cache

Usage

TTL Cache

The TTL cache implements a simple key-value store with a time-to-live mechanism. The stored values are automatically evicted after a specified duration.

const { ttl } = require('@zero65tech/cache');

const cache = new ttl(60); // Set TTL to 60 seconds

cache.put('key', 'value');
const result = cache.get('key');
console.log(result); // Output: 'value'

LRU Cache

The LRU cache implements a least recently used algorithm to evict the least recently accessed items when the cache size exceeds a specified limit.

const { lru } = require('@zero65tech/cache');

const cache = new lru(2); // Set cache size to 2

cache.put('key1', 'value1');
cache.put('key2', 'value2');
cache.put('key3', 'value3'); // Evicts 'key1' due to size limit

console.log(cache.get('key1')); // Output: undefined
console.log(cache.get('key2')); // Output: 'value2'
console.log(cache.get('key3')); // Output: 'value3'

Function Caching

The repository also provides utility functions for caching the results of asynchronous and synchronous functions.

Asynchronous Function Caching
const { fn, ttl } = require('@zero65tech/cache');
const cache = new ttl();

const asyncFunction = async (param) => {
  // ... some asynchronous computation
  return result;
};

const cachedAsyncFunction = fn(asyncFunction, cache, (param) => param);

const result = await cachedAsyncFunction('someParam');
Synchronous Function Caching
const { fnSync, lru } = require('@zero65tech/cache');
const cache = new lru();

const syncFunction = (param) => {
  // ... some synchronous computation
  return result;
};

const cachedSyncFunction = fnSync(syncFunction, cache, (param) => param);

const result = cachedSyncFunction('someParam');

Keywords

FAQs

Last updated on 05 Feb 2024

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc